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

Retrieve PartialMatch Only for Match Queries #2103

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions search/query/disjunction.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,18 @@ type DisjunctionQuery struct {
BoostVal *Boost `json:"boost,omitempty"`
Min float64 `json:"min"`
retrieveScoreBreakdown bool
retrievePartialMatch bool
queryStringMode bool
}

func (q *DisjunctionQuery) RetrieveScoreBreakdown(b bool) {
q.retrieveScoreBreakdown = b
}

func (q *DisjunctionQuery) RetrievePartialMatch(b bool) {
q.retrievePartialMatch = b
}

// NewDisjunctionQuery creates a new compound Query.
// Result documents satisfy at least one Query.
func NewDisjunctionQuery(disjuncts []Query) *DisjunctionQuery {
Expand Down Expand Up @@ -92,6 +97,7 @@ func (q *DisjunctionQuery) Searcher(ctx context.Context, i index.IndexReader, m
}

nctx := context.WithValue(ctx, search.IncludeScoreBreakdownKey, q.retrieveScoreBreakdown)
nctx = context.WithValue(nctx, search.IncludePartialMatchKey, q.retrievePartialMatch)

return searcher.NewDisjunctionSearcher(nctx, i, ss, q.Min, options)
}
Expand Down
1 change: 1 addition & 0 deletions search/query/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ func (q *MatchQuery) Searcher(ctx context.Context, i index.IndexReader, m mappin
shouldQuery := NewDisjunctionQuery(tqs)
shouldQuery.SetMin(1)
shouldQuery.SetBoost(q.BoostVal.Value())
shouldQuery.RetrievePartialMatch(true)
return shouldQuery.Searcher(ctx, i, m, options)

case MatchQueryOperatorAnd:
Expand Down
3 changes: 3 additions & 0 deletions search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ type DocumentMatch struct {
// this means that the match is partial because
// not all sub-queries matched
// if false, all the sub-queries matched
// used in match query which is internally a disjunction query
// and indicates that whether the document matched all the terms
// in the match query or not
PartialMatch bool `json:"partial_match,omitempty"`

// used to indicate the sub-scores that combined to form the
Expand Down
8 changes: 7 additions & 1 deletion search/searcher/search_disjunction_heap.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type DisjunctionHeapSearcher struct {
min int
queryNorm float64
retrieveScoreBreakdown bool
retrievePartialMatch bool
initialized bool
searchers []search.Searcher
heap []*SearcherCurr
Expand All @@ -71,8 +72,10 @@ func newDisjunctionHeapSearcher(ctx context.Context, indexReader index.IndexRead
return nil, tooManyClausesErr("", len(searchers))
}
var retrieveScoreBreakdown bool
var retrievePartialMatch bool
if ctx != nil {
retrieveScoreBreakdown, _ = ctx.Value(search.IncludeScoreBreakdownKey).(bool)
retrievePartialMatch, _ = ctx.Value(search.IncludePartialMatchKey).(bool)
}

// build our searcher
Expand All @@ -86,6 +89,7 @@ func newDisjunctionHeapSearcher(ctx context.Context, indexReader index.IndexRead
matchingCurrs: make([]*SearcherCurr, len(searchers)),
matchingIdxs: make([]int, len(searchers)),
retrieveScoreBreakdown: retrieveScoreBreakdown,
retrievePartialMatch: retrievePartialMatch,
heap: make([]*SearcherCurr, 0, len(searchers)),
}
rv.computeQueryNorm()
Expand Down Expand Up @@ -220,7 +224,9 @@ func (s *DisjunctionHeapSearcher) Next(ctx *search.SearchContext) (
// score this match
partialMatch := len(s.matching) != len(s.searchers)
rv = s.scorer.Score(ctx, s.matching, len(s.matching), s.numSearchers)
rv.PartialMatch = partialMatch
if s.retrievePartialMatch {
rv.PartialMatch = partialMatch
}
}
}

Expand Down
8 changes: 7 additions & 1 deletion search/searcher/search_disjunction_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type DisjunctionSliceSearcher struct {
numSearchers int
queryNorm float64
retrieveScoreBreakdown bool
retrievePartialMatch bool
currs []*search.DocumentMatch
scorer *scorer.DisjunctionQueryScorer
min int
Expand All @@ -60,8 +61,10 @@ func newDisjunctionSliceSearcher(ctx context.Context, indexReader index.IndexRea
var searchers OrderedSearcherList
var originalPos []int
var retrieveScoreBreakdown bool
var retrievePartialMatch bool
if ctx != nil {
retrieveScoreBreakdown, _ = ctx.Value(search.IncludeScoreBreakdownKey).(bool)
retrievePartialMatch, _ = ctx.Value(search.IncludePartialMatchKey).(bool)
}

if retrieveScoreBreakdown {
Expand Down Expand Up @@ -94,6 +97,7 @@ func newDisjunctionSliceSearcher(ctx context.Context, indexReader index.IndexRea
scorer: scorer.NewDisjunctionQueryScorer(options),
min: int(min),
retrieveScoreBreakdown: retrieveScoreBreakdown,
retrievePartialMatch: retrievePartialMatch,

matching: make([]*search.DocumentMatch, len(searchers)),
matchingIdxs: make([]int, len(searchers)),
Expand Down Expand Up @@ -232,7 +236,9 @@ func (s *DisjunctionSliceSearcher) Next(ctx *search.SearchContext) (
// score this match
partialMatch := len(s.matching) != len(s.searchers)
rv = s.scorer.Score(ctx, s.matching, len(s.matching), s.numSearchers)
rv.PartialMatch = partialMatch
if s.retrievePartialMatch {
rv.PartialMatch = partialMatch
}
}
}

Expand Down
1 change: 1 addition & 0 deletions search/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ const SearchIncrementalCostKey = "_search_incremental_cost_key"
const QueryTypeKey = "_query_type_key"
const FuzzyMatchPhraseKey = "_fuzzy_match_phrase_key"
const IncludeScoreBreakdownKey = "_include_score_breakdown_key"
const IncludePartialMatchKey = "_include_partial_match_key"

func RecordSearchCost(ctx context.Context,
msg SearchIncrementalCostCallbackMsg, bytes uint64) {
Expand Down
Loading