Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(search) : match phrase, multi match with and operator #33

Merged
merged 4 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions internal/store/elasticsearch/discovery_search_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,22 +154,23 @@ func (repo *DiscoveryRepository) buildSuggestQuery(cfg asset.SearchConfig) (io.R

func (repo *DiscoveryRepository) buildTextQuery(q *elastic.BoolQuery, cfg asset.SearchConfig) {
boostedFields := []string{"urn^10", "name^5"}

if cfg.Flags.DisableFuzzy {
q.Should(
elastic.NewMultiMatchQuery(cfg.Text, boostedFields...),
elastic.NewMultiMatchQuery(cfg.Text),
)
return
}

q.Should(
elastic.NewMultiMatchQuery(cfg.Text, boostedFields...),
// Phrase query cannot have `FUZZINESS`
elastic.NewMultiMatchQuery(cfg.Text, boostedFields...).
Fuzziness("AUTO"),
elastic.NewMultiMatchQuery(cfg.Text).
Fuzziness("AUTO"),
Type("phrase"),
)
for _, mq := range []*elastic.MultiMatchQuery{
elastic.NewMultiMatchQuery(cfg.Text, boostedFields...).
Operator("and"),
elastic.NewMultiMatchQuery(cfg.Text, boostedFields...),
elastic.NewMultiMatchQuery(cfg.Text),
} {
if !cfg.Flags.DisableFuzzy {
mq.Fuzziness("AUTO")
}

q.Should(mq)
}
}

func (repo *DiscoveryRepository) buildMustMatchQueries(q *elastic.BoolQuery, cfg asset.SearchConfig) {
Expand Down
55 changes: 43 additions & 12 deletions internal/store/elasticsearch/discovery_search_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,10 @@ func TestSearcherSearch(t *testing.T) {
Expected: []expectedRow{
{Type: "table", AssetID: "bigquery::gcpproject/dataset/tablename-common"},
{Type: "table", AssetID: "bigquery::gcpproject/dataset/tablename-mid"},
{Type: "table", AssetID: "bigquery::gcpproject/dataset/test"},
{Type: "table", AssetID: "bigquery::gcpproject/dataset/tablename-1"},
{Type: "table", AssetID: "bigquery::gcpproject/dataset/tablename-common-test"},
{Type: "table", AssetID: "bigquery::gcpproject/dataset/abc-tablename-mid"},
{Type: "table", AssetID: "bigquery::gcpproject/dataset/tablename-abc-common-test"},
},
},
{
Expand Down Expand Up @@ -233,18 +235,20 @@ func TestSearcherSearch(t *testing.T) {
},
Expected: []expectedRow{
{Type: "table", AssetID: "bigquery::gcpproject/dataset/tablename-common"},
{Type: "table", AssetID: "bigquery::gcpproject/dataset/tablename-common-test"},
{Type: "table", AssetID: "bigquery::gcpproject/dataset/tablename-abc-common-test"},
},
},
{
Description: "should return 'bigquery::gcpproject/dataset/tablename-common-test' resource on top if searched for text 'tablename-common-test'",
Description: "should return 'bigquery::gcpproject/dataset/tablename-abc-common-test' resource on top if searched for text 'tablename-abc-common-test'",
Config: asset.SearchConfig{
Text: "tablename-common-test",
Text: "tablename-abc-common-test",
RankBy: "data.profile.usage_count",
},
Expected: []expectedRow{
{Type: "table", AssetID: "bigquery::gcpproject/dataset/tablename-common-test"},
{Type: "table", AssetID: "bigquery::gcpproject/dataset/tablename-abc-common-test"},
{Type: "table", AssetID: "bigquery::gcpproject/dataset/tablename-common"},
{Type: "table", AssetID: "bigquery::gcpproject/dataset/test"},
{Type: "table", AssetID: "bigquery::gcpproject/dataset/abc-tablename-mid"},
{Type: "table", AssetID: "bigquery::gcpproject/dataset/tablename-mid"},
{Type: "table", AssetID: "bigquery::gcpproject/dataset/tablename-1"},
},
Expand Down Expand Up @@ -275,12 +279,24 @@ func TestSearcherSearch(t *testing.T) {
},
},
},
{
Description: "should return 'bigquery::gcpproject/dataset/tablename-abc-common-test' resource on top if " +
"searched for text 'abc-test' as it has both the keywords searched",
Config: asset.SearchConfig{
Text: "abc-test",
RankBy: "data.profile.usage_count",
},
Expected: []expectedRow{
{Type: "table", AssetID: "bigquery::gcpproject/dataset/tablename-abc-common-test"},
{Type: "table", AssetID: "bigquery::gcpproject/dataset/test"},
{Type: "table", AssetID: "bigquery::gcpproject/dataset/abc-tablename-mid"},
},
},
}
for _, test := range tests {
t.Run(test.Description, func(t *testing.T) {
results, err := repo.Search(ctx, test.Config)
require.NoError(t, err)

require.Equal(t, len(test.Expected), len(results))
for i, res := range test.Expected {
assert.Equal(t, res.Type, results[i].Type)
Expand Down Expand Up @@ -410,6 +426,13 @@ func TestGroupAssets(t *testing.T) {
Size: 15,
},
Expected: []asset.GroupResult{
{
Fields: []asset.GroupField{
{Name: "type", Value: "table"},
{Name: "name", Value: "abc-tablename-mid"},
},
Assets: []asset.Asset{{Name: "abc-tablename-mid"}},
},
{
Fields: []asset.GroupField{
{Name: "type", Value: "table"},
Expand All @@ -429,20 +452,21 @@ func TestGroupAssets(t *testing.T) {
{Name: "type", Value: "table"},
{Name: "name", Value: "tablename-1"},
},
Assets: []asset.Asset{{Name: "tablename-1"}}},
Assets: []asset.Asset{{Name: "tablename-1"}},
},
{
Fields: []asset.GroupField{
{Name: "type", Value: "table"},
{Name: "name", Value: "tablename-common"},
{Name: "name", Value: "tablename-abc-common-test"},
},
Assets: []asset.Asset{{Name: "tablename-common"}},
Assets: []asset.Asset{{Name: "tablename-abc-common-test"}},
},
{
Fields: []asset.GroupField{
{Name: "type", Value: "table"},
{Name: "name", Value: "tablename-common-test"},
{Name: "name", Value: "tablename-common"},
},
Assets: []asset.Asset{{Name: "tablename-common-test"}},
Assets: []asset.Asset{{Name: "tablename-common"}},
},
{
Fields: []asset.GroupField{
Expand All @@ -451,6 +475,13 @@ func TestGroupAssets(t *testing.T) {
},
Assets: []asset.Asset{{Name: "tablename-mid"}},
},
{
Fields: []asset.GroupField{
{Name: "type", Value: "table"},
{Name: "name", Value: "test"},
},
Assets: []asset.Asset{{Name: "test"}},
},
{
Fields: []asset.GroupField{
{Name: "type", Value: "topic"},
Expand Down Expand Up @@ -502,7 +533,7 @@ func TestGroupAssets(t *testing.T) {
Assets: []asset.Asset{
{Name: "tablename-1"},
{Name: "tablename-common"},
{Name: "tablename-common-test"},
{Name: "tablename-abc-common-test"},
},
},
{
Expand Down
197 changes: 193 additions & 4 deletions internal/store/elasticsearch/testdata/search-test-fixture.json
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@
"filter_conditions": [
"WHERE t.column_5 = 'success' AND t.item_id = \"280481a2-2384-4b81-aa3e-214ac60b31db\" AND event_timestamp >= TIMESTAMP(\"2021-10-29\", \"UTC\") AND event_timestamp < TIMESTAMP(\"2021-11-22T02:01:06Z\")"
],
"usage_count": 100
"usage_count": 50
},
"properties": {
"attributes": {
Expand Down Expand Up @@ -337,9 +337,9 @@
"type": "table"
},
{
"id": "bigquery::gcpproject/dataset/tablename-common-test",
"urn": "bigquery::gcpproject/dataset/tablename-common-test",
"name": "tablename-common-test",
"id": "bigquery::gcpproject/dataset/tablename-abc-common-test",
"urn": "bigquery::gcpproject/dataset/tablename-abc-common-test",
"name": "tablename-abc-common-test",
"service": "bigquery",
"description": "A sample of table record with high usage",
"data": {
Expand Down Expand Up @@ -524,6 +524,195 @@
}
],
"type": "table"
},
{
"id": "bigquery::gcpproject/dataset/abc-tablename-mid",
"urn": "bigquery::gcpproject/dataset/abc-tablename-mid",
"name": "abc-tablename-mid",
"service": "bigquery",
"description": "A sample of table record with high usage",
"data": {
"preview": {},
"profile": {
"common_join": [
{
"conditions": [
"ON target.column_1 = source.column_1 and target.column_3 = source.column_3 and DATE(target.event_timestamp) = DATE(source.event_timestamp)"
],
"count": 3,
"urn": "bigquery::gcpproject/dataset/tablename-mid"
}
],
"filter_conditions": [
"WHERE t.column_5 = 'success' AND t.item_id = \"280481a2-2384-4b81-aa3e-214ac60b31db\" AND event_timestamp >= TIMESTAMP(\"2021-10-29\", \"UTC\") AND event_timestamp < TIMESTAMP(\"2021-11-22T02:01:06Z\")"
],
"usage_count": 1
},
"properties": {
"attributes": {
"dataset": "dataset",
"full_qualified_name": "gcpproject:dataset.abc-tablename-mid",
"partition_field": "event_timestamp",
"project": "gcpproject",
"type": "TABLE"
},
"labels": {
"owner": "user_1"
}
},
"schema": {
"columns": [
{
"data_type": "STRING",
"is_nullable": true,
"name": "abc-tablename-mid-column1",
"properties": {
"attributes": {
"mode": "NULLABLE"
}
}
},
{
"data_type": "STRING",
"is_nullable": true,
"name": "abc-tablename-mid-column2",
"properties": {
"attributes": {
"mode": "NULLABLE"
}
}
},
{
"data_type": "BIGNUMERIC",
"is_nullable": true,
"name": "abc-tablename-mid-column3",
"properties": {
"attributes": {
"mode": "NULLABLE"
}
}
}
]
},
"resource": {
"name": "abc-mid-test",
"service": "bigquery",
"urn": "bigquery::gcpproject/dataset/abc-tablename-mid"
}
},
"owners": [
{
"urn": "owner::bq/3",
"name": "Mr.X",
"role": "admin",
"email": "mr.x@email.com"
},
{
"urn": "owner::bq/4",
"name": "Mrs.Y",
"role": "user",
"email": "mr.y@email.com"
}
],
"type": "table"
},
{
"id": "bigquery::gcpproject/dataset/test",
"urn": "bigquery::gcpproject/dataset/test",
"name": "test",
"service": "bigquery",
"description": "A sample of table record with mid usage",
"data": {
"preview": {},
"profile": {
"common_join": [
{
"conditions": [
"ON target.column_1 = source.column_1 and target.column_3 = source.column_3 and DATE(target.event_timestamp) = DATE(source.event_timestamp)"
],
"count": 1,
"urn": "bigquery::gcpproject/dataset/tablename-high"
},
{
"conditions": [
"ON target.column_1 = source.column_1 and target.column_3 = source.column_3 and DATE(target.event_timestamp) = DATE(source.event_timestamp)"
],
"count": 1,
"urn": "bigquery::gcpproject/dataset/tablename-1"
}
],
"filter_conditions": [
"WHERE t.column_5 = 'success' AND t.item_id = \"280481a2-2384-4b81-aa3e-214ac60b31db\" AND event_timestamp >= TIMESTAMP(\"2021-10-29\", \"UTC\") AND event_timestamp < TIMESTAMP(\"2021-11-22T02:01:06Z\")"
],
"usage_count": 2
},
"properties": {
"attributes": {
"dataset": "dataset",
"full_qualified_name": "gcpproject:dataset.test",
"partition_field": "event_timestamp",
"project": "gcpproject",
"type": "TABLE"
},
"labels": {
"owner": "user_1"
}
},
"schema": {
"columns": [
{
"data_type": "STRING",
"is_nullable": true,
"name": "test-column1",
"properties": {
"attributes": {
"mode": "NULLABLE"
}
}
},
{
"data_type": "STRING",
"is_nullable": true,
"name": "test-column2",
"properties": {
"attributes": {
"mode": "NULLABLE"
}
}
},
{
"data_type": "BIGNUMERIC",
"is_nullable": true,
"name": "test-column3",
"properties": {
"attributes": {
"mode": "NULLABLE"
}
}
}
]
},
"resource": {
"name": "tablename-mid",
"service": "bigquery",
"urn": "bigquery::gcpproject/dataset/test"
}
},
"owners": [
{
"urn": "owner::bq/1",
"name": "John Smith",
"role": "user",
"email": "john.smith@email.com"
},
{
"urn": "owner::bq/2",
"name": "Paul Smith",
"role": "user",
"email": "paul.smith@email.com"
}
],
"type": "table"
}
]
}
Expand Down