2022-11-14 18:47:22 +08:00
|
|
|
package search_parser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-08-31 14:16:32 +08:00
|
|
|
"fmt"
|
2023-07-20 18:48:35 +08:00
|
|
|
"github.com/answerdev/answer/internal/base/constant"
|
2022-11-19 14:03:16 +08:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
2022-11-14 18:47:22 +08:00
|
|
|
"github.com/answerdev/answer/internal/schema"
|
2022-11-19 14:03:16 +08:00
|
|
|
"github.com/answerdev/answer/internal/service/tag_common"
|
2022-11-14 18:47:22 +08:00
|
|
|
usercommon "github.com/answerdev/answer/internal/service/user_common"
|
|
|
|
"github.com/answerdev/answer/pkg/converter"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SearchParser struct {
|
2022-11-19 14:03:16 +08:00
|
|
|
tagCommonService *tag_common.TagCommonService
|
|
|
|
userCommon *usercommon.UserCommon
|
2022-11-14 18:47:22 +08:00
|
|
|
}
|
|
|
|
|
2022-11-19 14:03:16 +08:00
|
|
|
func NewSearchParser(tagCommonService *tag_common.TagCommonService, userCommon *usercommon.UserCommon) *SearchParser {
|
2022-11-14 18:47:22 +08:00
|
|
|
return &SearchParser{
|
2022-11-19 14:03:16 +08:00
|
|
|
tagCommonService: tagCommonService,
|
|
|
|
userCommon: userCommon,
|
2022-11-14 18:47:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseStructure parse search structure, maybe match one of type all/questions/answers,
|
|
|
|
// but if match two type, it will return false
|
2023-07-20 18:48:35 +08:00
|
|
|
func (sp *SearchParser) ParseStructure(ctx context.Context, dto *schema.SearchDTO) (cond *schema.SearchCondition) {
|
|
|
|
cond = &schema.SearchCondition{}
|
2022-11-14 18:47:22 +08:00
|
|
|
var (
|
2023-07-20 18:48:35 +08:00
|
|
|
query = dto.Query
|
|
|
|
limitWords = 5
|
2022-11-14 18:47:22 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// match tags
|
2023-07-20 18:48:35 +08:00
|
|
|
cond.Tags = sp.parseTags(ctx, &query)
|
2022-11-14 18:47:22 +08:00
|
|
|
|
|
|
|
// match all
|
2023-07-20 18:48:35 +08:00
|
|
|
cond.UserID = sp.parseUserID(ctx, &query, dto.UserID)
|
|
|
|
cond.VoteAmount = sp.parseVotes(&query)
|
|
|
|
cond.Words = sp.parseWithin(&query)
|
2022-11-14 18:47:22 +08:00
|
|
|
|
|
|
|
// match questions
|
2023-07-20 18:48:35 +08:00
|
|
|
cond.NotAccepted = sp.parseNotAccepted(&query)
|
|
|
|
if cond.NotAccepted {
|
|
|
|
cond.TargetType = constant.QuestionObjectType
|
2022-11-14 18:47:22 +08:00
|
|
|
}
|
2023-07-20 18:48:35 +08:00
|
|
|
cond.Views = sp.parseViews(&query)
|
|
|
|
if cond.Views != -1 {
|
|
|
|
cond.TargetType = constant.QuestionObjectType
|
2022-11-14 18:47:22 +08:00
|
|
|
}
|
2023-07-20 18:48:35 +08:00
|
|
|
cond.AnswerAmount = sp.parseAnswers(&query)
|
|
|
|
if cond.AnswerAmount != -1 {
|
|
|
|
cond.TargetType = constant.QuestionObjectType
|
2022-11-14 18:47:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// match answers
|
2023-07-20 18:48:35 +08:00
|
|
|
cond.Accepted = sp.parseAccepted(&query)
|
|
|
|
if cond.Accepted {
|
|
|
|
cond.TargetType = constant.AnswerObjectType
|
2022-12-22 17:00:52 +08:00
|
|
|
}
|
2023-07-20 18:48:35 +08:00
|
|
|
cond.QuestionID = sp.parseQuestionID(&query)
|
|
|
|
if cond.QuestionID != "" {
|
|
|
|
cond.TargetType = constant.AnswerObjectType
|
2022-11-14 18:47:22 +08:00
|
|
|
}
|
|
|
|
|
2023-07-20 18:48:35 +08:00
|
|
|
if sp.parseIsQuestion(&query) {
|
|
|
|
cond.TargetType = constant.QuestionObjectType
|
2022-11-14 18:47:22 +08:00
|
|
|
}
|
2023-07-20 18:48:35 +08:00
|
|
|
if sp.parseIsAnswer(&query) {
|
|
|
|
cond.TargetType = constant.AnswerObjectType
|
2022-11-14 18:47:22 +08:00
|
|
|
}
|
|
|
|
|
2023-07-20 18:48:35 +08:00
|
|
|
if len(strings.TrimSpace(query)) > 0 {
|
|
|
|
words := strings.Split(strings.TrimSpace(query), " ")
|
|
|
|
cond.Words = append(cond.Words, words...)
|
2022-11-14 18:47:22 +08:00
|
|
|
}
|
|
|
|
|
2023-07-20 18:48:35 +08:00
|
|
|
// check limit words
|
|
|
|
if len(cond.Words) > limitWords {
|
|
|
|
cond.Words = cond.Words[:limitWords]
|
2022-11-14 18:47:22 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseTags parse search tags, return tag ids array
|
2023-07-20 18:48:35 +08:00
|
|
|
func (sp *SearchParser) parseTags(ctx context.Context, query *string) (tags []string) {
|
2022-11-14 18:47:22 +08:00
|
|
|
var (
|
|
|
|
// expire tag pattern
|
2023-08-01 14:14:59 +08:00
|
|
|
exprTag = `\[(.*?)\]`
|
2022-11-14 18:47:22 +08:00
|
|
|
q = *query
|
|
|
|
limit = 5
|
|
|
|
)
|
|
|
|
|
|
|
|
re := regexp.MustCompile(exprTag)
|
|
|
|
res := re.FindAllStringSubmatch(q, -1)
|
|
|
|
if len(res) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2022-12-22 17:00:52 +08:00
|
|
|
|
|
|
|
tags = []string{}
|
|
|
|
for _, item := range res {
|
2023-07-20 18:48:35 +08:00
|
|
|
tag, exists, err := sp.tagCommonService.GetTagBySlugName(ctx, item[1])
|
2022-11-14 18:47:22 +08:00
|
|
|
if err != nil || !exists {
|
|
|
|
continue
|
|
|
|
}
|
2023-08-31 14:16:32 +08:00
|
|
|
if tag.MainTagID > 0 {
|
|
|
|
tags = append(tags, fmt.Sprintf("%d", tag.MainTagID))
|
|
|
|
} else {
|
|
|
|
tags = append(tags, tag.ID)
|
|
|
|
}
|
2022-11-14 18:47:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// limit maximum 5 tags
|
|
|
|
if len(tags) > limit {
|
|
|
|
tags = tags[:limit]
|
|
|
|
}
|
|
|
|
|
|
|
|
q = strings.TrimSpace(re.ReplaceAllString(q, ""))
|
|
|
|
*query = q
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseUserID return user id or current login user id
|
2023-07-20 18:48:35 +08:00
|
|
|
func (sp *SearchParser) parseUserID(ctx context.Context, query *string, currentUserID string) (userID string) {
|
2022-11-14 18:47:22 +08:00
|
|
|
var (
|
2023-07-21 17:27:01 +08:00
|
|
|
exprUsername = `user:(\S+)`
|
|
|
|
exprMe = "user:me"
|
|
|
|
q = *query
|
2022-11-14 18:47:22 +08:00
|
|
|
)
|
|
|
|
|
2023-07-21 17:27:01 +08:00
|
|
|
re := regexp.MustCompile(exprUsername)
|
2022-11-14 18:47:22 +08:00
|
|
|
res := re.FindStringSubmatch(q)
|
2022-12-28 17:20:55 +08:00
|
|
|
if strings.Contains(q, exprMe) {
|
2022-12-15 11:42:16 +08:00
|
|
|
userID = currentUserID
|
|
|
|
q = strings.ReplaceAll(q, exprMe, "")
|
2023-07-21 17:27:01 +08:00
|
|
|
} else if len(res) > 1 {
|
2022-11-14 18:47:22 +08:00
|
|
|
name := res[1]
|
2023-07-20 18:48:35 +08:00
|
|
|
user, has, err := sp.userCommon.GetUserBasicInfoByUserName(ctx, name)
|
2022-11-14 18:47:22 +08:00
|
|
|
if err == nil && has {
|
|
|
|
userID = user.ID
|
|
|
|
q = re.ReplaceAllString(q, "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*query = strings.TrimSpace(q)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseVotes return the votes of search query
|
|
|
|
func (sp *SearchParser) parseVotes(query *string) (votes int) {
|
|
|
|
var (
|
2023-07-21 17:27:01 +08:00
|
|
|
expr = `score:(\d+)`
|
2022-11-14 18:47:22 +08:00
|
|
|
q = *query
|
|
|
|
)
|
|
|
|
votes = -1
|
|
|
|
|
|
|
|
re := regexp.MustCompile(expr)
|
|
|
|
res := re.FindStringSubmatch(q)
|
2023-07-21 17:27:01 +08:00
|
|
|
if len(res) > 1 {
|
2022-11-14 18:47:22 +08:00
|
|
|
votes = converter.StringToInt(res[1])
|
|
|
|
q = re.ReplaceAllString(q, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
*query = strings.TrimSpace(q)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseWithin parse quotes within words like: "hello world"
|
|
|
|
func (sp *SearchParser) parseWithin(query *string) (words []string) {
|
|
|
|
var (
|
|
|
|
q = *query
|
|
|
|
expr = `(?U)(".+")`
|
|
|
|
)
|
|
|
|
re := regexp.MustCompile(expr)
|
|
|
|
matches := re.FindAllStringSubmatch(q, -1)
|
|
|
|
words = []string{}
|
|
|
|
for _, match := range matches {
|
2022-12-22 17:00:52 +08:00
|
|
|
if len(match[1]) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2022-11-14 18:47:22 +08:00
|
|
|
words = append(words, match[1])
|
|
|
|
}
|
|
|
|
q = re.ReplaceAllString(q, "")
|
|
|
|
*query = strings.TrimSpace(q)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseNotAccepted return the question has not accepted the answer
|
|
|
|
func (sp *SearchParser) parseNotAccepted(query *string) (notAccepted bool) {
|
|
|
|
var (
|
|
|
|
q = *query
|
|
|
|
expr = `hasaccepted:no`
|
|
|
|
)
|
|
|
|
|
2022-12-16 14:41:20 +08:00
|
|
|
if strings.Contains(q, expr) {
|
2022-11-14 18:47:22 +08:00
|
|
|
q = strings.ReplaceAll(q, expr, "")
|
|
|
|
notAccepted = true
|
|
|
|
}
|
|
|
|
|
|
|
|
*query = strings.TrimSpace(q)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseIsQuestion check the result if only limit question or not
|
|
|
|
func (sp *SearchParser) parseIsQuestion(query *string) (isQuestion bool) {
|
|
|
|
var (
|
|
|
|
q = *query
|
|
|
|
expr = `is:question`
|
|
|
|
)
|
|
|
|
|
2022-12-16 14:41:20 +08:00
|
|
|
if strings.Contains(q, expr) {
|
2022-11-14 18:47:22 +08:00
|
|
|
q = strings.ReplaceAll(q, expr, "")
|
|
|
|
isQuestion = true
|
|
|
|
}
|
|
|
|
|
|
|
|
*query = strings.TrimSpace(q)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseViews check search has views or not
|
|
|
|
func (sp *SearchParser) parseViews(query *string) (views int) {
|
|
|
|
var (
|
|
|
|
q = *query
|
2023-07-21 17:27:01 +08:00
|
|
|
expr = `views:(\d+)`
|
2022-11-14 18:47:22 +08:00
|
|
|
)
|
|
|
|
views = -1
|
|
|
|
|
|
|
|
re := regexp.MustCompile(expr)
|
|
|
|
res := re.FindStringSubmatch(q)
|
2023-07-21 17:27:01 +08:00
|
|
|
if len(res) > 1 {
|
2022-11-14 18:47:22 +08:00
|
|
|
views = converter.StringToInt(res[1])
|
|
|
|
q = re.ReplaceAllString(q, "")
|
|
|
|
}
|
|
|
|
*query = strings.TrimSpace(q)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseAnswers check whether specified answer count for question
|
|
|
|
func (sp *SearchParser) parseAnswers(query *string) (answers int) {
|
|
|
|
var (
|
|
|
|
q = *query
|
2023-07-21 17:27:01 +08:00
|
|
|
expr = `answers:(\d+)`
|
2022-11-14 18:47:22 +08:00
|
|
|
)
|
|
|
|
answers = -1
|
|
|
|
|
|
|
|
re := regexp.MustCompile(expr)
|
|
|
|
res := re.FindStringSubmatch(q)
|
2023-07-21 17:27:01 +08:00
|
|
|
if len(res) > 1 {
|
2022-11-14 18:47:22 +08:00
|
|
|
answers = converter.StringToInt(res[1])
|
|
|
|
q = re.ReplaceAllString(q, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
*query = strings.TrimSpace(q)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseAccepted check the search is limit accepted answer or not
|
|
|
|
func (sp *SearchParser) parseAccepted(query *string) (accepted bool) {
|
|
|
|
var (
|
|
|
|
q = *query
|
|
|
|
expr = `isaccepted:yes`
|
|
|
|
)
|
|
|
|
|
2022-12-16 14:41:20 +08:00
|
|
|
if strings.Contains(q, expr) {
|
2022-11-14 18:47:22 +08:00
|
|
|
accepted = true
|
2022-12-16 16:35:55 +08:00
|
|
|
q = strings.ReplaceAll(q, expr, "")
|
2022-11-14 18:47:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
*query = strings.TrimSpace(q)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseQuestionID check whether specified question's id
|
|
|
|
func (sp *SearchParser) parseQuestionID(query *string) (questionID string) {
|
|
|
|
var (
|
|
|
|
q = *query
|
2023-07-21 17:27:01 +08:00
|
|
|
expr = `inquestion:(\d+)`
|
2022-11-14 18:47:22 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
re := regexp.MustCompile(expr)
|
|
|
|
res := re.FindStringSubmatch(q)
|
|
|
|
if len(res) == 2 {
|
|
|
|
questionID = res[1]
|
|
|
|
q = re.ReplaceAllString(q, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
*query = strings.TrimSpace(q)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseIsAnswer check the result if only limit answer or not
|
|
|
|
func (sp *SearchParser) parseIsAnswer(query *string) (isAnswer bool) {
|
|
|
|
var (
|
|
|
|
q = *query
|
|
|
|
expr = `is:answer`
|
|
|
|
)
|
|
|
|
|
2022-12-16 14:41:20 +08:00
|
|
|
if strings.Contains(q, expr) {
|
2022-11-14 18:47:22 +08:00
|
|
|
isAnswer = true
|
|
|
|
q = strings.ReplaceAll(q, expr, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
*query = strings.TrimSpace(q)
|
|
|
|
return
|
|
|
|
}
|