0
0
Fork 0

fix incorrect results returned by phrase search

previously phrase searcher would not validate that consecutive
terms were actually occurring in the same array position

fixes #292
This commit is contained in:
Marty Schoch 2015-12-06 15:55:00 -05:00
parent 6e9da3bab7
commit b4d4ee2fff
4 changed files with 68 additions and 1 deletions

View File

@ -16,6 +16,20 @@ type Location struct {
ArrayPositions []float64 `json:"array_positions"`
}
// SameArrayElement returns true if two locations are point to
// the same array element
func (l *Location) SameArrayElement(other *Location) bool {
if len(l.ArrayPositions) != len(other.ArrayPositions) {
return false
}
for i, elem := range l.ArrayPositions {
if other.ArrayPositions[i] != elem {
return false
}
}
return true
}
type Locations []*Location
type TermLocationMap map[string]Locations

View File

@ -119,7 +119,7 @@ func (s *PhraseSearcher) Next() (*search.DocumentMatch, error) {
nextLocations, ok := termLocMap[nextTerm]
if ok {
for _, nextLocation := range nextLocations {
if nextLocation.Pos == location.Pos+float64(i) {
if nextLocation.Pos == location.Pos+float64(i) && nextLocation.SameArrayElement(location) {
// found a location match for this term
crvtlm.AddLocation(nextTerm, nextLocation)
continue INNER

View File

@ -0,0 +1,3 @@
{
"body": ["bad call", "defenseless receiver"]
}

View File

@ -322,5 +322,55 @@
}
]
}
},
{
"search": {
"from": 0,
"size": 10,
"query": {
"field": "body",
"match_phrase": "bad call"
}
},
"result": {
"total_hits": 1,
"hits": [
{
"id": "b"
}
]
}
},
{
"search": {
"from": 0,
"size": 10,
"query": {
"field": "body",
"match_phrase": "defenseless receiver"
}
},
"result": {
"total_hits": 1,
"hits": [
{
"id": "b"
}
]
}
},
{
"search": {
"from": 0,
"size": 10,
"query": {
"field": "body",
"match_phrase": "bad receiver"
}
},
"result": {
"total_hits": 0,
"hits": []
}
}
]