Skip to content

Commit

Permalink
feat: enable empty text search
Browse files Browse the repository at this point in the history
  • Loading branch information
anjaliagg9791 committed Oct 4, 2023
1 parent ec9201e commit d0568cf
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 31 deletions.
3 changes: 0 additions & 3 deletions internal/server/v1beta1/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ func (server *APIServer) SearchAssets(ctx context.Context, req *compassv1beta1.S
}

text := strings.TrimSpace(req.GetText())
if text == "" {
return nil, status.Error(codes.InvalidArgument, "'text' must be specified")
}

cfg := asset.SearchConfig{
Text: text,
Expand Down
5 changes: 0 additions & 5 deletions internal/server/v1beta1/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ func TestSearch(t *testing.T) {
}

testCases := []testCase{
{
Description: "should return invalid argument if 'text' parameter is empty or missing",
ExpectStatus: codes.InvalidArgument,
Request: &compassv1beta1.SearchAssetsRequest{},
},
{
Description: "should report internal server if asset searcher fails",
Request: &compassv1beta1.SearchAssetsRequest{
Expand Down
14 changes: 8 additions & 6 deletions internal/store/elasticsearch/discovery_search_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ var returnedAssetFieldsResult = []string{"id", "urn", "type", "service", "name",

// Search the asset store
func (repo *DiscoveryRepository) Search(ctx context.Context, cfg asset.SearchConfig) (results []asset.SearchResult, err error) {
if strings.TrimSpace(cfg.Text) == "" {
return nil, asset.DiscoveryError{Op: "Search", Err: errors.New("search text cannot be empty")}
}
maxResults := cfg.MaxResults
if maxResults <= 0 {
maxResults = defaultMaxResults
Expand Down Expand Up @@ -233,6 +230,10 @@ func buildSuggestQuery(cfg asset.SearchConfig) (io.Reader, error) {
}

func buildTextQuery(q *elastic.BoolQuery, cfg asset.SearchConfig) {
if strings.TrimSpace(cfg.Text) == "" {
q.Should(elastic.NewMatchAllQuery())
}

boostedFields := []string{"urn^10", "name^5"}
q.Should(
// Phrase query cannot have `FUZZINESS`
Expand Down Expand Up @@ -305,12 +306,13 @@ func buildFilterExistsQueries(q *elastic.BoolQuery, fields []string) {

func buildFunctionScoreQuery(query elastic.Query, rankBy, text string) elastic.Query {
// Added exact match term query here so that exact match gets higher priority.
fsQuery := elastic.NewFunctionScoreQuery().
Add(
fsQuery := elastic.NewFunctionScoreQuery()
if text != "" {
fsQuery.Add(
elastic.NewTermQuery("name.keyword", text),
elastic.NewWeightFactorFunction(2),
)

}
if rankBy != "" {
fsQuery.AddScoreFunc(
elastic.NewFieldValueFactorFunction().
Expand Down
52 changes: 35 additions & 17 deletions internal/store/elasticsearch/discovery_search_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package elasticsearch_test
import (
"context"
"encoding/json"
"fmt"
"os"
"sort"
"testing"
Expand All @@ -21,23 +22,6 @@ type searchTestData struct {

func TestSearcherSearch(t *testing.T) {
ctx := context.TODO()
t.Run("should return an error if search string is empty", func(t *testing.T) {
cli, err := esTestServer.NewClient()
require.NoError(t, err)
esClient, err := store.NewClient(
log.NewNoop(),
store.Config{},
store.WithClient(cli),
)
require.NoError(t, err)

repo := store.NewDiscoveryRepository(esClient, log.NewNoop())
_, err = repo.Search(ctx, asset.SearchConfig{
Text: "",
})

assert.Error(t, err)
})

t.Run("fixtures", func(t *testing.T) {
cli, err := esTestServer.NewClient()
Expand Down Expand Up @@ -67,6 +51,38 @@ func TestSearcherSearch(t *testing.T) {
}

tests := []searchTest{
{
Description: "should fetch assets when text is empty",
Config: asset.SearchConfig{
Text: "",
Filters: map[string][]string{
"service": {"bigquery"},
},
},
Expected: []expectedRow{
{Type: "table", AssetID: "tablename-1"},
{Type: "table", AssetID: "tablename-common"},
{Type: "table", AssetID: "tablename-abc-common-test"},
{Type: "table", AssetID: "tablename-mid"},
{Type: "table", AssetID: "abc-tablename-mid"},
{Type: "table", AssetID: "test"},
},
},
{
Description: "should fetch assets when text is empty and rank by RankBy: data.profile.usage_count",
Config: asset.SearchConfig{
Text: "",
RankBy: "data.profile.usage_count",
},
Expected: []expectedRow{
{Type: "table", AssetID: "tablename-common"},
{Type: "table", AssetID: "tablename-mid"},
{Type: "table", AssetID: "test"},
{Type: "table", AssetID: "tablename-1"},
{Type: "table", AssetID: "tablename-abc-common-test"},
{Type: "table", AssetID: "abc-tablename-mid"},
},
},
{
Description: "should fetch assets which has text in any of its fields",
Config: asset.SearchConfig{
Expand Down Expand Up @@ -299,6 +315,8 @@ func TestSearcherSearch(t *testing.T) {
results, err := repo.Search(ctx, test.Config)
require.NoError(t, err)
require.Equal(t, len(test.Expected), len(results))
fmt.Println(test.Expected)

Check failure on line 318 in internal/store/elasticsearch/discovery_search_repository_test.go

View workflow job for this annotation

GitHub Actions / golangci

use of `fmt.Println` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)
fmt.Println(results)

Check failure on line 319 in internal/store/elasticsearch/discovery_search_repository_test.go

View workflow job for this annotation

GitHub Actions / golangci

use of `fmt.Println` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$` (forbidigo)
for i, res := range test.Expected {
assert.Equal(t, res.Type, results[i].Type)
assert.Equal(t, res.AssetID, results[i].ID)
Expand Down

0 comments on commit d0568cf

Please sign in to comment.