People: Remove query length threshold when searching for names #22

This commit is contained in:
Michael Mayer 2021-10-09 11:35:36 +02:00
parent 54a096e901
commit f3e8d379b4
2 changed files with 25 additions and 28 deletions

View file

@ -127,7 +127,7 @@ func LikeAllWords(col, s string) (wheres []string) {
// LikeAllNames returns a list of where conditions matching all names. // LikeAllNames returns a list of where conditions matching all names.
func LikeAllNames(cols Cols, s string) (wheres []string) { func LikeAllNames(cols Cols, s string) (wheres []string) {
if len(cols) == 0 || len(s) < 2 { if len(cols) == 0 || len(s) < 1 {
return wheres return wheres
} }
@ -137,19 +137,15 @@ func LikeAllNames(cols Cols, s string) (wheres []string) {
for _, w := range strings.Split(k, txt.Or) { for _, w := range strings.Split(k, txt.Or) {
w = strings.TrimSpace(w) w = strings.TrimSpace(w)
if w == txt.Empty || len(w) < 2 && txt.IsLatin(w) { if w == txt.Empty {
continue continue
} }
for _, c := range cols { for _, c := range cols {
if len(w) > 4 { if strings.Contains(w, txt.Space) {
if strings.Contains(w, txt.Space) { orWheres = append(orWheres, fmt.Sprintf("%s LIKE '%s%%'", c, w))
orWheres = append(orWheres, fmt.Sprintf("%s LIKE '%s%%'", c, w))
} else {
orWheres = append(orWheres, fmt.Sprintf("%s LIKE '%%%s%%'", c, w))
}
} else { } else {
orWheres = append(orWheres, fmt.Sprintf("%s LIKE '%s' OR %s LIKE '%s %%'", c, w, c, w)) orWheres = append(orWheres, fmt.Sprintf("%s LIKE '%%%s%%'", c, w))
} }
} }
} }

View file

@ -153,8 +153,7 @@ func TestLikeAllKeywords(t *testing.T) {
assert.Equal(t, "k.keyword LIKE 'mander%'", w[0]) assert.Equal(t, "k.keyword LIKE 'mander%'", w[0])
assert.Equal(t, "k.keyword LIKE '李'", w[1]) assert.Equal(t, "k.keyword LIKE '李'", w[1])
} else { } else {
t.Logf("wheres: %#v", w) t.Fatalf("unexpected result: %#v", w)
t.Fatal("two where conditions expected")
} }
}) })
} }
@ -166,8 +165,7 @@ func TestLikeAllWords(t *testing.T) {
assert.Equal(t, "k.name LIKE 'mander%'", w[1]) assert.Equal(t, "k.name LIKE 'mander%'", w[1])
assert.Equal(t, "k.name LIKE '王%'", w[2]) assert.Equal(t, "k.name LIKE '王%'", w[2])
} else { } else {
t.Logf("wheres: %#v", w) t.Fatalf("unexpected result: %#v", w)
t.Fatal("two where conditions expected")
} }
}) })
} }
@ -177,53 +175,56 @@ func TestLikeAllNames(t *testing.T) {
if w := LikeAllNames(Cols{"k.name"}, "j Mander 王"); len(w) == 1 { if w := LikeAllNames(Cols{"k.name"}, "j Mander 王"); len(w) == 1 {
assert.Equal(t, "k.name LIKE 'j Mander 王%'", w[0]) assert.Equal(t, "k.name LIKE 'j Mander 王%'", w[0])
} else { } else {
t.Fatalf("one where conditions expected: %#v", w) t.Fatalf("unexpected result: %#v", w)
} }
}) })
t.Run("MultipleColumns", func(t *testing.T) { t.Run("MultipleColumns", func(t *testing.T) {
if w := LikeAllNames(Cols{"a.col1", "b.col2"}, "Mo Mander"); len(w) == 1 { if w := LikeAllNames(Cols{"a.col1", "b.col2"}, "Mo Mander"); len(w) == 1 {
assert.Equal(t, "a.col1 LIKE 'Mo Mander%' OR b.col2 LIKE 'Mo Mander%'", w[0]) assert.Equal(t, "a.col1 LIKE 'Mo Mander%' OR b.col2 LIKE 'Mo Mander%'", w[0])
} else { } else {
t.Fatalf("one where conditions expected: %#v", w) t.Fatalf("unexpected result: %#v", w)
} }
}) })
t.Run("EmptyName", func(t *testing.T) { t.Run("EmptyName", func(t *testing.T) {
w := LikeAllNames(Cols{"k.name"}, "") w := LikeAllNames(Cols{"k.name"}, "")
assert.Empty(t, w) assert.Empty(t, w)
}) })
t.Run("NoWords", func(t *testing.T) { t.Run("SingleCharacter", func(t *testing.T) {
w := LikeAllNames(Cols{"k.name"}, "a") if w := LikeAllNames(Cols{"k.name"}, "a"); len(w) == 1 {
assert.Empty(t, w) assert.Equal(t, "k.name LIKE '%a%'", w[0])
} else {
t.Fatalf("unexpected result: %#v", w)
}
}) })
t.Run("FullNames", func(t *testing.T) { t.Run("FullNames", func(t *testing.T) {
if w := LikeAllNames(Cols{"j.name", "j.alias"}, "Bill & Melinda Gates"); len(w) == 2 { if w := LikeAllNames(Cols{"j.name", "j.alias"}, "Bill & Melinda Gates"); len(w) == 2 {
assert.Equal(t, "j.name LIKE 'Bill' OR j.name LIKE 'Bill %' OR j.alias LIKE 'Bill' OR j.alias LIKE 'Bill %'", w[0]) assert.Equal(t, "j.name LIKE '%Bill%' OR j.alias LIKE '%Bill%'", w[0])
assert.Equal(t, "j.name LIKE 'Melinda Gates%' OR j.alias LIKE 'Melinda Gates%'", w[1]) assert.Equal(t, "j.name LIKE 'Melinda Gates%' OR j.alias LIKE 'Melinda Gates%'", w[1])
} else { } else {
t.Fatalf("two where conditions expected: %#v", w) t.Fatalf("unexpected result: %#v", w)
} }
}) })
t.Run("Plus", func(t *testing.T) { t.Run("Plus", func(t *testing.T) {
if w := LikeAllNames(Cols{"name"}, txt.NormalizeQuery("Paul + Paula")); len(w) == 2 { if w := LikeAllNames(Cols{"name"}, txt.NormalizeQuery("Paul + Paula")); len(w) == 2 {
assert.Equal(t, "name LIKE 'paul' OR name LIKE 'paul %'", w[0]) assert.Equal(t, "name LIKE '%paul%'", w[0])
assert.Equal(t, "name LIKE '%paula%'", w[1]) assert.Equal(t, "name LIKE '%paula%'", w[1])
} else { } else {
t.Fatalf("two where conditions expected: %#v", w) t.Fatalf("unexpected result: %#v", w)
} }
}) })
t.Run("Ane", func(t *testing.T) { t.Run("And", func(t *testing.T) {
if w := LikeAllNames(Cols{"name"}, txt.NormalizeQuery("Paul and Paula")); len(w) == 2 { if w := LikeAllNames(Cols{"name"}, txt.NormalizeQuery("P and Paula")); len(w) == 2 {
assert.Equal(t, "name LIKE 'paul' OR name LIKE 'paul %'", w[0]) assert.Equal(t, "name LIKE '%p%'", w[0])
assert.Equal(t, "name LIKE '%paula%'", w[1]) assert.Equal(t, "name LIKE '%paula%'", w[1])
} else { } else {
t.Fatalf("two where conditions expected: %#v", w) t.Fatalf("unexpected result: %#v", w)
} }
}) })
t.Run("Or", func(t *testing.T) { t.Run("Or", func(t *testing.T) {
if w := LikeAllNames(Cols{"name"}, txt.NormalizeQuery("Paul or Paula")); len(w) == 1 { if w := LikeAllNames(Cols{"name"}, txt.NormalizeQuery("Paul or Paula")); len(w) == 1 {
assert.Equal(t, "name LIKE 'paul' OR name LIKE 'paul %' OR name LIKE '%paula%'", w[0]) assert.Equal(t, "name LIKE '%paul%' OR name LIKE '%paula%'", w[0])
} else { } else {
t.Fatalf("one where conditions expected: %#v", w) t.Fatalf("unexpected result: %#v", w)
} }
}) })
} }