0
0

Merge pull request #525 from mschoch/refactor-phrase

refactor phrase search into seprate methods
This commit is contained in:
Marty Schoch 2017-01-31 15:06:42 -05:00 committed by GitHub
commit a3ee71ddbb

View File

@ -96,20 +96,73 @@ func (s *PhraseSearcher) Next(ctx *search.SearchContext) (*search.DocumentMatch,
}
}
var rv *search.DocumentMatch
for s.currMust != nil {
rv := s.checkCurrMustMatch(ctx)
if rv != nil {
// prepare for next iteration
err := s.advanceNextMust(ctx)
if err != nil {
return nil, err
}
return rv, nil
}
err := s.advanceNextMust(ctx)
if err != nil {
return nil, err
}
}
return nil, nil
}
// checkCurrMustMatch is soley concerned with determining if the DocumentMatch
// pointed to by s.currMust (which satisifies the pre-condition searcher)
// also satisfies the phase constraints. if so, it returns a DocumentMatch
// for this document, otherwise nil
func (s *PhraseSearcher) checkCurrMustMatch(ctx *search.SearchContext) *search.DocumentMatch {
rvftlm := make(search.FieldTermLocationMap, 0)
freq := 0
// typically we would expect there to only actually be results in
// one field, but we allow for this to not be the case
// but, we note that phrase constraints can only be satisfied within
// a single field, so we can check them each independently
for field := range s.currMust.Locations {
f, rvtlm := s.checkCurrMustMatchField(ctx, field)
if f > 0 {
freq += f
rvftlm[field] = rvtlm
}
}
if freq > 0 {
// return match
rv := s.currMust
rv.Locations = rvftlm
return rv
}
return nil
}
// checkCurrMustMatchField is soley concerned with determining if one particular
// field within the currMust DocumentMatch Locations satisfies the phase
// constraints (possibly more than once). if so, the number of times it was
// satisfied, and these locations are returned. otherwise 0 and either
// a nil or empty TermLocationMap
func (s *PhraseSearcher) checkCurrMustMatchField(ctx *search.SearchContext, field string) (int, search.TermLocationMap) {
firstTerm := s.terms[0]
for field, termLocMap := range s.currMust.Locations {
freq := 0
termLocMap := s.currMust.Locations[field]
locations, ok := termLocMap[firstTerm]
if !ok {
continue
return 0, nil
}
rvtlm := make(search.TermLocationMap, 0)
OUTER:
OUTER:
for _, location := range locations {
crvtlm := make(search.TermLocationMap, 0)
crvtlm.AddLocation(firstTerm, location)
@ -138,28 +191,9 @@ func (s *PhraseSearcher) Next(ctx *search.SearchContext) (*search.DocumentMatch,
// if we got here all the terms matched
freq++
search.MergeTermLocationMaps(rvtlm, crvtlm)
rvftlm[field] = rvtlm
}
}
if freq > 0 {
// return match
rv = s.currMust
rv.Locations = rvftlm
err := s.advanceNextMust(ctx)
if err != nil {
return nil, err
}
return rv, nil
}
err := s.advanceNextMust(ctx)
if err != nil {
return nil, err
}
}
return nil, nil
return freq, rvtlm
}
func (s *PhraseSearcher) Advance(ctx *search.SearchContext, ID index.IndexInternalID) (*search.DocumentMatch, error) {