[MM-26420] Adds wildcard support to Bleve (#14885)

Этот коммит содержится в:
Miguel de la Cruz
2020-09-18 10:49:56 +02:00
коммит произвёл GitHub
родитель 334cf9d84a
Коммит 1d2220141e
2 изменённых файлов: 41 добавлений и 15 удалений

Просмотреть файл

@@ -159,10 +159,23 @@ func (b *BleveEngine) SearchPosts(channels *model.ChannelList, searchParams []*m
} }
} else { } else {
if len(params.Terms) > 0 { if len(params.Terms) > 0 {
messageQ := bleve.NewMatchQuery(params.Terms) terms := []string{}
messageQ.SetField("Message") for _, term := range strings.Split(params.Terms, " ") {
messageQ.SetOperator(termOperator) if strings.HasSuffix(term, "*") {
termQueries = append(termQueries, messageQ) messageQ := bleve.NewWildcardQuery(term)
messageQ.SetField("Message")
termQueries = append(termQueries, messageQ)
} else {
terms = append(terms, term)
}
}
if len(terms) > 0 {
messageQ := bleve.NewMatchQuery(strings.Join(terms, " "))
messageQ.SetField("Message")
messageQ.SetOperator(termOperator)
termQueries = append(termQueries, messageQ)
}
} }
if len(params.ExcludedTerms) > 0 { if len(params.ExcludedTerms) > 0 {

Просмотреть файл

@@ -116,7 +116,7 @@ var searchPostStoreTests = []searchTest{
{ {
Name: "Should support search with wildcards", Name: "Should support search with wildcards",
Fn: testSupportWildcards, Fn: testSupportWildcards,
Tags: []string{ENGINE_POSTGRES, ENGINE_MYSQL, ENGINE_ELASTICSEARCH}, Tags: []string{ENGINE_ALL},
}, },
{ {
Name: "Should not support search with preceding wildcards", Name: "Should not support search with preceding wildcards",
@@ -126,7 +126,7 @@ var searchPostStoreTests = []searchTest{
{ {
Name: "Should discard a wildcard if it's not placed immediately by text", Name: "Should discard a wildcard if it's not placed immediately by text",
Fn: testSearchDiscardWildcardAlone, Fn: testSearchDiscardWildcardAlone,
Tags: []string{ENGINE_POSTGRES, ENGINE_MYSQL, ENGINE_ELASTICSEARCH}, Tags: []string{ENGINE_ALL},
}, },
{ {
Name: "Should support terms with dash", Name: "Should support terms with dash",
@@ -1025,21 +1025,34 @@ func testSupportStemming(t *testing.T, th *SearchTestHelper) {
func testSupportWildcards(t *testing.T, th *SearchTestHelper) { func testSupportWildcards(t *testing.T, th *SearchTestHelper) {
p1, err := th.createPost(th.User.Id, th.ChannelBasic.Id, "search post", "", model.POST_DEFAULT, 0, false) p1, err := th.createPost(th.User.Id, th.ChannelBasic.Id, "search post", "", model.POST_DEFAULT, 0, false)
require.Nil(t, err) require.Nil(t, err)
p2, err := th.createPost(th.User.Id, th.ChannelBasic.Id, "searching post", "", model.POST_DEFAULT, 0, false) p2, err := th.createPost(th.User.Id, th.ChannelBasic.Id, "searching", "", model.POST_DEFAULT, 0, false)
require.Nil(t, err) require.Nil(t, err)
_, err = th.createPost(th.User.Id, th.ChannelBasic.Id, "another post", "", model.POST_DEFAULT, 0, false) _, err = th.createPost(th.User.Id, th.ChannelBasic.Id, "another post", "", model.POST_DEFAULT, 0, false)
require.Nil(t, err) require.Nil(t, err)
defer th.deleteUserPosts(th.User.Id) defer th.deleteUserPosts(th.User.Id)
params := &model.SearchParams{ t.Run("Simple wildcard-only search", func(t *testing.T) {
Terms: "search*", params := &model.SearchParams{
} Terms: "search*",
results, apperr := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20) }
require.Nil(t, apperr) results, apperr := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20)
require.Nil(t, apperr)
require.Len(t, results.Posts, 2) require.Len(t, results.Posts, 2)
th.checkPostInSearchResults(t, p1.Id, results.Posts) th.checkPostInSearchResults(t, p1.Id, results.Posts)
th.checkPostInSearchResults(t, p2.Id, results.Posts) th.checkPostInSearchResults(t, p2.Id, results.Posts)
})
t.Run("Wildcard search with another term placed after", func(t *testing.T) {
params := &model.SearchParams{
Terms: "sear* post",
}
results, apperr := th.Store.Post().SearchPostsInTeamForUser([]*model.SearchParams{params}, th.User.Id, th.Team.Id, 0, 20)
require.Nil(t, apperr)
require.Len(t, results.Posts, 1)
th.checkPostInSearchResults(t, p1.Id, results.Posts)
})
} }
func testNotSupportPrecedingWildcards(t *testing.T, th *SearchTestHelper) { func testNotSupportPrecedingWildcards(t *testing.T, th *SearchTestHelper) {