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

fix Fuzziiness in Match query #258

Merged
merged 2 commits into from
Jul 4, 2022
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
102 changes: 102 additions & 0 deletions pkg/core/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,108 @@ func TestIndex_Search(t *testing.T) {
},
},
},
{
name: "Search Query - fuzzy fuzziness AUTO",
args: args{
iQuery: &meta.ZincQuery{
Query: &meta.Query{
Fuzzy: map[string]*meta.FuzzyQuery{
"_all": {
Value: "fransisco", // note the wrong spelling,
Fuzziness: "AUTO",
},
},
},
Size: 10,
},
},
data: []map[string]interface{}{
{
"name": "Prabhat Sharma",
"address": map[string]interface{}{
"city": "San Francisco",
"state": "California",
},
"hobby": "chess",
},
{
"name": "Leonardo DiCaprio",
"address": map[string]interface{}{
"city": "Los angeles",
"state": "California",
},
"hobby": "chess",
},
},
},
{
name: "Search Query - fuzzy fuzziness AUTO",
args: args{
iQuery: &meta.ZincQuery{
Query: &meta.Query{
Fuzzy: map[string]*meta.FuzzyQuery{
"_all": {
Value: "fransisco", // note the wrong spelling,
Fuzziness: "AUTO:3,6",
},
},
},
Size: 10,
},
},
data: []map[string]interface{}{
{
"name": "Prabhat Sharma",
"address": map[string]interface{}{
"city": "San Francisco",
"state": "California",
},
"hobby": "chess",
},
{
"name": "Leonardo DiCaprio",
"address": map[string]interface{}{
"city": "Los angeles",
"state": "California",
},
"hobby": "chess",
},
},
},
{
name: "Search Query - fuzzy fuzziness 2",
args: args{
iQuery: &meta.ZincQuery{
Query: &meta.Query{
Fuzzy: map[string]*meta.FuzzyQuery{
"_all": {
Value: "fransisco", // note the wrong spelling,
Fuzziness: 2,
},
},
},
Size: 10,
},
},
data: []map[string]interface{}{
{
"name": "Prabhat Sharma",
"address": map[string]interface{}{
"city": "San Francisco",
"state": "California",
},
"hobby": "chess",
},
{
"name": "Leonardo DiCaprio",
"address": map[string]interface{}{
"city": "Los angeles",
"state": "California",
},
"hobby": "chess",
},
},
},
{
name: "Search Query - querystring",
args: args{
Expand Down
48 changes: 39 additions & 9 deletions pkg/uquery/query/fuzzy.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/zinclabs/zinc/pkg/errors"
"github.com/zinclabs/zinc/pkg/meta"
"github.com/zinclabs/zinc/pkg/zutils"
)

func FuzzyQuery(query map[string]interface{}) (bluge.Query, error) {
Expand All @@ -43,13 +44,13 @@ func FuzzyQuery(query map[string]interface{}) (bluge.Query, error) {
k := strings.ToLower(k)
switch k {
case "value":
value.Value = v.(string)
value.Value, _ = zutils.ToString(v)
case "fuzziness":
value.Fuzziness = v.(string)
value.Fuzziness = v
case "prefix_length":
value.PrefixLength = v.(float64)
value.PrefixLength, _ = zutils.ToFloat64(v)
case "boost":
value.Boost = v.(float64)
value.Boost, _ = zutils.ToFloat64(v)
default:
return nil, errors.New(errors.ErrorTypeParsingException, fmt.Sprintf("[fuzzy] unknown field [%s]", k))
}
Expand All @@ -61,11 +62,9 @@ func FuzzyQuery(query map[string]interface{}) (bluge.Query, error) {

subq := bluge.NewFuzzyQuery(value.Value).SetField(field)
if value.Fuzziness != nil {
switch v := value.Fuzziness.(type) {
case string:
// TODO: support other fuzziness: AUTO
case float64:
subq.SetFuzziness(int(v))
v := ParseFuzziness(value.Fuzziness, len(value.Value))
if v > 0 {
subq.SetFuzziness(v)
}
}
if value.PrefixLength > 0 {
Expand All @@ -77,3 +76,34 @@ func FuzzyQuery(query map[string]interface{}) (bluge.Query, error) {

return subq, nil
}

func ParseFuzziness(fuzziness interface{}, n int) int {
val, _ := zutils.ToString(fuzziness)
val = strings.ToUpper(val)
if !strings.HasPrefix(val, "AUTO") {
v, _ := zutils.ToInt(val)
return v
}

n1 := 3
n2 := 6
if strings.Contains(val, ":") && strings.Contains(val, ",") {
val := strings.TrimPrefix(val, "AUTO:")
vals := strings.Split(val, ",")
if len(vals) == 2 {
n1, _ = zutils.ToInt(vals[0])
n2, _ = zutils.ToInt(vals[1])
if n1 < 2 || n1 >= n2 {
return 0
}
}
}

v := 0
if n >= n2 {
v = 2
} else if n >= n1 {
v = 1
}
return v
}
23 changes: 12 additions & 11 deletions pkg/uquery/query/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/zinclabs/zinc/pkg/errors"
"github.com/zinclabs/zinc/pkg/meta"
zincanalysis "github.com/zinclabs/zinc/pkg/uquery/analysis"
"github.com/zinclabs/zinc/pkg/zutils"
)

func MatchQuery(query map[string]interface{}, mappings *meta.Mappings, analyzers map[string]*analysis.Analyzer) (bluge.Query, error) {
Expand All @@ -45,17 +46,17 @@ func MatchQuery(query map[string]interface{}, mappings *meta.Mappings, analyzers
k := strings.ToLower(k)
switch k {
case "query":
value.Query = v.(string)
value.Query, _ = zutils.ToString(v)
case "analyzer":
value.Analyzer = v.(string)
value.Analyzer, _ = zutils.ToString(v)
case "operator":
value.Operator = v.(string)
value.Operator, _ = zutils.ToString(v)
case "fuzziness":
value.Fuzziness = v.(string)
value.Fuzziness = v
case "prefix_length":
value.PrefixLength = v.(float64)
value.PrefixLength, _ = zutils.ToFloat64(v)
case "boost":
value.Boost = v.(float64)
value.Boost, _ = zutils.ToFloat64(v)
default:
return nil, errors.New(errors.ErrorTypeParsingException, fmt.Sprintf("[match] unknown field [%s]", k))
}
Expand Down Expand Up @@ -98,11 +99,11 @@ func MatchQuery(query map[string]interface{}, mappings *meta.Mappings, analyzers
}
}
if value.Fuzziness != nil {
switch v := value.Fuzziness.(type) {
case string:
// TODO: support other fuzziness: AUTO
case float64:
subq.SetFuzziness(int(v))
if value.Fuzziness != nil {
v := ParseFuzziness(value.Fuzziness, len(value.Query))
if v > 0 {
subq.SetFuzziness(v)
}
}
}
if value.PrefixLength > 0 {
Expand Down