A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.
feat(search): highlight search results
Octobug5e477482982c
diff --git a/internal/repo/search_common/search_repo.go b/internal/repo/search_common/search_repo.goindex 0443bfe9..cd7de696 100644--- a/internal/repo/search_common/search_repo.go+++ b/internal/repo/search_common/search_repo.go@@ -22,12 +22,13 @@ package search_commonimport ("context""fmt"- tagcommon "github.com/apache/incubator-answer/internal/service/tag_common"- "github.com/apache/incubator-answer/plugin""strconv""strings""time"+ tagcommon "github.com/apache/incubator-answer/internal/service/tag_common"+ "github.com/apache/incubator-answer/plugin"+"github.com/apache/incubator-answer/pkg/htmltext""github.com/apache/incubator-answer/internal/base/data"@@ -234,7 +235,7 @@ func (sr *searchRepo) SearchContents(ctx context.Context, words []string, tagIDserr = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()return} else {- resp, err = sr.parseResult(ctx, res)+ resp, err = sr.parseResult(ctx, res, words)return}}@@ -342,7 +343,7 @@ func (sr *searchRepo) SearchQuestions(ctx context.Context, words []string, tagIDif len(tr) != 0 {total = converter.StringToInt64(string(tr[0]["total"]))}- resp, err = sr.parseResult(ctx, res)+ resp, err = sr.parseResult(ctx, res, words)if err != nil {err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()}@@ -436,7 +437,7 @@ func (sr *searchRepo) SearchAnswers(ctx context.Context, words []string, tagIDs}total = converter.StringToInt64(string(tr[0]["total"]))- resp, err = sr.parseResult(ctx, res)+ resp, err = sr.parseResult(ctx, res, words)if err != nil {err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()}@@ -460,7 +461,7 @@ func (sr *searchRepo) parseOrder(ctx context.Context, order string) (res string)}// ParseSearchPluginResult parse search plugin result-func (sr *searchRepo) ParseSearchPluginResult(ctx context.Context, sres []plugin.SearchResult) (resp []*schema.SearchResult, err error) {+func (sr *searchRepo) ParseSearchPluginResult(ctx context.Context, sres []plugin.SearchResult, words []string) (resp []*schema.SearchResult, err error) {var (qres []map[string][]byteres = make([]map[string][]byte, 0)@@ -483,11 +484,11 @@ func (sr *searchRepo) ParseSearchPluginResult(ctx context.Context, sres []plugin}res = append(res, qres[0])}- return sr.parseResult(ctx, res)+ return sr.parseResult(ctx, res, words)}// parseResult parse search result, return the data structure-func (sr *searchRepo) parseResult(ctx context.Context, res []map[string][]byte) (resp []*schema.SearchResult, err error) {+func (sr *searchRepo) parseResult(ctx context.Context, res []map[string][]byte, words []string) (resp []*schema.SearchResult, err error) {questionIDs := make([]string, 0)userIDs := make([]string, 0)resultList := make([]*schema.SearchResult, 0)@@ -508,7 +509,7 @@ func (sr *searchRepo) parseResult(ctx context.Context, res []map[string][]byte)QuestionID: QuestionID,Title: string(r["title"]),UrlTitle: htmltext.UrlTitle(string(r["title"])),- Excerpt: htmltext.FetchExcerpt(string(r["parsed_text"]), "...", 240),+ Excerpt: htmltext.FetchMatchedExcerpt(string(r["parsed_text"]), words, "...", 100),CreatedAtParsed: tp.Unix(),UserInfo: &schema.SearchObjectUser{ID: string(r["user_id"]),diff --git a/internal/service/search_common/search.go b/internal/service/search_common/search.goindex c19fdc49..ba6f6295 100644--- a/internal/service/search_common/search.go+++ b/internal/service/search_common/search.go@@ -21,6 +21,7 @@ package search_commonimport ("context"+"github.com/apache/incubator-answer/internal/schema""github.com/apache/incubator-answer/plugin")@@ -29,5 +30,5 @@ type SearchRepo interface {SearchContents(ctx context.Context, words []string, tagIDs [][]string, userID string, votes, page, size int, order string) (resp []*schema.SearchResult, total int64, err error)SearchQuestions(ctx context.Context, words []string, tagIDs [][]string, notAccepted bool, views, answers int, page, size int, order string) (resp []*schema.SearchResult, total int64, err error)SearchAnswers(ctx context.Context, words []string, tagIDs [][]string, accepted bool, questionID string, page, size int, order string) (resp []*schema.SearchResult, total int64, err error)- ParseSearchPluginResult(ctx context.Context, sres []plugin.SearchResult) (resp []*schema.SearchResult, err error)+ ParseSearchPluginResult(ctx context.Context, sres []plugin.SearchResult, words []string) (resp []*schema.SearchResult, err error)}diff --git a/internal/service/search_service.go b/internal/service/search_service.goindex 46d71374..430fa6cf 100644--- a/internal/service/search_service.go+++ b/internal/service/search_service.go@@ -21,6 +21,7 @@ package serviceimport ("context"+"github.com/apache/incubator-answer/internal/schema""github.com/apache/incubator-answer/internal/service/search_common""github.com/apache/incubator-answer/internal/service/search_parser"@@ -93,6 +94,6 @@ func (ss *SearchService) searchByPlugin(ctx context.Context, finder plugin.Searcres, resp.Total, err = finder.SearchAnswers(ctx, cond.Convert2PluginSearchCond(dto.Page, dto.Size, dto.Order))}- resp.SearchResults, err = ss.searchRepo.ParseSearchPluginResult(ctx, res)+ resp.SearchResults, err = ss.searchRepo.ParseSearchPluginResult(ctx, res, cond.Words)return resp, err}diff --git a/pkg/htmltext/htmltext.go b/pkg/htmltext/htmltext.goindex 792063f4..df8866ca 100644--- a/pkg/htmltext/htmltext.go+++ b/pkg/htmltext/htmltext.go@@ -20,18 +20,36 @@package htmltextimport (- "github.com/Chain-Zhang/pinyin"- "github.com/apache/incubator-answer/pkg/checker""io""net/http""net/url""regexp""strings"+ "unicode/utf8"+ "github.com/Chain-Zhang/pinyin""github.com/Machiel/slugify"strip "github.com/grokify/html-strip-tags-go"++ "github.com/apache/incubator-answer/pkg/checker"+ "github.com/apache/incubator-answer/pkg/converter")+// min() and max() can be removed starting from Go1.21+func max(a, b int) int {+ if a > b {+ return a+ }+ return b+}++func min(a, b int) int {+ if a < b {+ return a+ }+ return b+}+// ClearText clear HTML, get the clear textfunc ClearText(html string) (text string) {if len(html) == 0 {@@ -123,6 +141,82 @@ func FetchExcerpt(html, trimMarker string, limit int) (text string) {return}+// findFirstMatchedWord returns the first matched word and its index+func findFirstMatchedWord(text string, words []string) (string, int) {+ if len(text) == 0 || len(words) == 0 {+ return "", 0+ }++ words = converter.UniqueArray(words)+ firstWord := ""+ firstIndex := len(text)++ for _, word := range words {+ if idx := strings.Index(text, word); idx != -1 && idx < firstIndex {+ firstIndex = idx+ firstWord = word+ }+ }++ if firstIndex != len(text) {+ return firstWord, firstIndex+ }++ return "", 0+}++// getRuneRange returns the valid begin and end indexes of the runeText+func getRuneRange(runeText []rune, offset, limit int) (begin, end int) {+ runeLen := len(runeText)++ limit = min(runeLen, max(0, limit))+ begin = min(runeLen, max(0, offset))+ end = min(runeLen, begin+limit)++ return+}++// FetchRangedExcerpt returns a ranged excerpt from the HTML string.+// Note: offset is a rune index, not a byte index+func FetchRangedExcerpt(html, trimMarker string, offset int, limit int) (text string) {+ if len(html) == 0 {+ text = html+ return+ }++ runeText := []rune(ClearText(html))+ begin, end := getRuneRange(runeText, offset, limit)+ text = string(runeText[begin:end])++ if begin > 0 {+ text = trimMarker + text+ }+ if end < len(runeText) {+ text = text + trimMarker+ }++ return+}++// FetchMatchedExcerpt returns the matched excerpt according to the words+func FetchMatchedExcerpt(html string, words []string, trimMarker string, trimLength int) string {+ text := ClearText(html)+ matchedWord, matchedIndex := findFirstMatchedWord(text, words)+ runeIndex := utf8.RuneCountInString(text[0:matchedIndex])++ trimLength = max(0, trimLength)+ runeOffset := runeIndex - trimLength+ runeLimit := trimLength + trimLength + utf8.RuneCountInString(matchedWord)++ textRuneCount := utf8.RuneCountInString(text)+ if runeOffset+runeLimit > textRuneCount {+ // Reserved extra chars before the matched word+ runeOffset = textRuneCount - runeLimit+ }++ return FetchRangedExcerpt(html, trimMarker, runeOffset, runeLimit)+}+func GetPicByUrl(Url string) string {res, err := http.Get(Url)if err != nil {diff --git a/pkg/htmltext/htmltext_test.go b/pkg/htmltext/htmltext_test.goindex 4c520d7a..d549d887 100644--- a/pkg/htmltext/htmltext_test.go+++ b/pkg/htmltext/htmltext_test.go@@ -85,3 +85,122 @@ func TestUrlTitle(t *testing.T) {fmt.Println(formatTitle)}}++func TestFindFirstMatchedWord(t *testing.T) {+ var (+ expectedWord,+ actualWord string+ expectedIndex,+ actualIndex int+ )++ text := "Hello, I have 中文 and 😂 and I am supposed to work fine."++ // test find nothing+ expectedWord, expectedIndex = "", 0+ actualWord, actualIndex = findFirstMatchedWord(text, []string{"youcantfindme"})+ assert.Equal(t, expectedWord, actualWord)+ assert.Equal(t, expectedIndex, actualIndex)++ // test find one word+ expectedWord, expectedIndex = "文", 17+ actualWord, actualIndex = findFirstMatchedWord(text, []string{"文"})+ assert.Equal(t, expectedWord, actualWord)+ assert.Equal(t, expectedIndex, actualIndex)++ // test find multiple matched words+ expectedWord, expectedIndex = "Hello", 0+ actualWord, actualIndex = findFirstMatchedWord(text, []string{"Hello", "文"})+ assert.Equal(t, expectedWord, actualWord)+ assert.Equal(t, expectedIndex, actualIndex)+}++func TestGetRuneRange(t *testing.T) {+ var (+ expectedBegin,+ expectedEnd,+ actualBegin,+ actualEnd int+ )++ runeText := []rune("Hello, I have 中文 and 😂.")+ runeLen := len(runeText)++ // test get range of negative offset and negative limit+ expectedBegin, expectedEnd = 0, 0+ actualBegin, actualEnd = getRuneRange(runeText, -1, -1)+ assert.Equal(t, expectedBegin, actualBegin)+ assert.Equal(t, expectedEnd, actualEnd)++ // test get range of exceeding offset and exceeding limit+ expectedBegin, expectedEnd = runeLen, runeLen+ actualBegin, actualEnd = getRuneRange(runeText, runeLen+1, runeLen+1)+ assert.Equal(t, expectedBegin, actualBegin)+ assert.Equal(t, expectedEnd, actualEnd)++ // test get range of normal offset and exceeding limit+ expectedBegin, expectedEnd = 3, runeLen+ actualBegin, actualEnd = getRuneRange(runeText, 3, runeLen)+ assert.Equal(t, expectedBegin, actualBegin)+ assert.Equal(t, expectedEnd, actualEnd)++ // test get range of normal offset and normal limit+ expectedBegin, expectedEnd = 3, 10+ actualBegin, actualEnd = getRuneRange(runeText, 3, 7)+ assert.Equal(t, expectedBegin, actualBegin)+ assert.Equal(t, expectedEnd, actualEnd)+}++func TestFetchRangedExcerpt(t *testing.T) {+ var (+ expected,+ actual string+ )++ // test english string+ expected = "hello..."+ actual = FetchRangedExcerpt("<p>hello world</p>", "...", 0, 5)+ assert.Equal(t, expected, actual)++ // test string with offset+ expected = "...llo你好..."+ actual = FetchRangedExcerpt("<p>hello你好world</p>", "...", 2, 5)+ assert.Equal(t, expected, actual)++ // test mixed string with emoticon with offset+ expected = "...你好😂..."+ actual = FetchRangedExcerpt("<p>hello你好😂world</p>", "...", 5, 3)+ assert.Equal(t, expected, actual)++ // test mixed string with offset and exceeding limit+ expected = "...你好😂world"+ actual = FetchRangedExcerpt("<p>hello你好😂world</p>", "...", 5, 100)+ assert.Equal(t, expected, actual)+}++func TestFetchMatchedExcerpt(t *testing.T) {+ var (+ expected,+ actual string+ )++ html := "<p>Hello, I have 中文 and 😂 and I am supposed to work fine</p>"++ // test find nothing+ // it should return from the begin with double trimLength text+ expected = "Hello, I h..."+ actual = FetchMatchedExcerpt(html, []string{"youcantfindme"}, "...", 5)+ assert.Equal(t, expected, actual)++ // test find the word at the end+ // it should return the word beginning with double trimLenth plus len(word)+ expected = "... work fine"+ actual = FetchMatchedExcerpt(html, []string{"youcant", "fine"}, "...", 3)+ assert.Equal(t, expected, actual)++ // test find multiple words+ // it should return the first matched word with trimmedText+ expected = "... have 中文 and 😂..."+ actual = FetchMatchedExcerpt(html, []string{"中文", "😂"}, "...", 6)+ assert.Equal(t, expected, actual)+}diff --git a/ui/src/common/pattern.ts b/ui/src/common/pattern.tsindex 847c6d76..133228b1 100644--- a/ui/src/common/pattern.ts+++ b/ui/src/common/pattern.ts@@ -20,6 +20,8 @@const pattern = {email:/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+\.)+[a-zA-Z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]{2,}))$/,+ search:+ /(\[.*\])|(is:answer)|(is:question)|(score:\d*)|(user:\S*)|(answers:\d*)/g,uaWeChat: /micromessenger/i,uaWeCom: /wxwork/i,uaDingTalk: /dingtalk/i,diff --git a/ui/src/components/HighlightText/index.scss b/ui/src/components/HighlightText/index.scssnew file mode 100644index 00000000..da0468e6--- /dev/null+++ b/ui/src/components/HighlightText/index.scss@@ -0,0 +1,22 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one+ * or more contributor license agreements. See the NOTICE file+ * distributed with this work for additional information+ * regarding copyright ownership. The ASF licenses this file+ * to you under the Apache License, Version 2.0 (the+ * "License"); you may not use this file except in compliance+ * with the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing,+ * software distributed under the License is distributed on an+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY+ * KIND, either express or implied. See the License for the+ * specific language governing permissions and limitations+ * under the License.+ */++.highlight-text > mark {+ padding: 0;+}diff --git a/ui/src/components/HighlightText/index.tsx b/ui/src/components/HighlightText/index.tsxnew file mode 100644index 00000000..b5acacc1--- /dev/null+++ b/ui/src/components/HighlightText/index.tsx@@ -0,0 +1,46 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one+ * or more contributor license agreements. See the NOTICE file+ * distributed with this work for additional information+ * regarding copyright ownership. The ASF licenses this file+ * to you under the Apache License, Version 2.0 (the+ * "License"); you may not use this file except in compliance+ * with the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing,+ * software distributed under the License is distributed on an+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY+ * KIND, either express or implied. See the License for the+ * specific language governing permissions and limitations+ * under the License.+ */++import { memo, FC } from 'react';++import './index.scss';++interface IProps {+ text: string;+ keywords: string[];+}++const Index: FC<IProps> = ({ text = '', keywords = [] }) => {+ const regex = new RegExp(`(${keywords.join('|')})`, 'gi');++ return (+ <span className="highlight-text">+ {text.split(regex).map((piece: string, index: number) => {+ const key = `${piece.substring(0, 6)}_${index}`;+ return keywords.find((kw: string) => kw === piece) ? (+ <mark key={key}>{piece}</mark>+ ) : (+ piece+ );+ })}+ </span>+ );+};++export default memo(Index);diff --git a/ui/src/components/index.ts b/ui/src/components/index.tsindex 7725cf43..fdf052d3 100644--- a/ui/src/components/index.ts+++ b/ui/src/components/index.ts@@ -59,6 +59,7 @@ import CustomSidebar from './CustomSidebar';import ImgViewer from './ImgViewer';import SideNav from './SideNav';import PluginRender from './PluginRender';+import HighlightText from './HighlightText';export {Avatar,@@ -105,5 +106,6 @@ export {ImgViewer,SideNav,PluginRender,+ HighlightText,};export type { EditorRef, JSONSchema, UISchema };diff --git a/ui/src/pages/Search/components/SearchItem/index.tsx b/ui/src/pages/Search/components/SearchItem/index.tsxindex b1ebca56..a31dab56 100644--- a/ui/src/pages/Search/components/SearchItem/index.tsx+++ b/ui/src/pages/Search/components/SearchItem/index.tsx@@ -18,12 +18,19 @@*/import { memo, FC } from 'react';-import { Link } from 'react-router-dom';+import { Link, useSearchParams } from 'react-router-dom';import { ListGroupItem } from 'react-bootstrap';import { useTranslation } from 'react-i18next';import { pathFactory } from '@/router/pathFactory';-import { Tag, FormatTime, BaseUserCard, Counts } from '@/components';+import {+ Tag,+ FormatTime,+ BaseUserCard,+ Counts,+ HighlightText,+} from '@/components';+import Pattern from '@/common/pattern';import type { SearchResItem } from '@/common/interface';import { escapeRemove } from '@/utils';@@ -47,6 +54,10 @@ const Index: FC<Props> = ({ data }) => {});}+ const [searchParams] = useSearchParams();+ const q = searchParams.get('q');+ const keywords = q?.replace(Pattern.search, '').split(' ') || [];+return (<ListGroupItem className="py-3 px-0 border-start-0 border-end-0 bg-transparent"><div className="mb-2 clearfix">@@ -56,7 +67,7 @@ const Index: FC<Props> = ({ data }) => {{data.object_type === 'question' ? 'Q' : 'A'}</span><Link className="h5 mb-0 link-dark text-break" to={itemUrl}>- {data.object.title}+ <HighlightText text={data.object.title} keywords={keywords} />{data.object.status === 'closed'? ` [${t('closed', { keyPrefix: 'question' })}]`: null}@@ -88,7 +99,10 @@ const Index: FC<Props> = ({ data }) => {{data.object?.excerpt && (<p className="small text-truncate-2 mb-2 last-p text-break">- {escapeRemove(data.object.excerpt)}+ <HighlightText+ text={escapeRemove(data.object.excerpt) || ''}+ keywords={keywords}+ /></p>)}