0
0
Fork 0

address code review comments from @steveyen

This commit is contained in:
Marty Schoch 2016-09-23 15:16:18 -04:00
parent 073c4d0ebd
commit b863add129
7 changed files with 55 additions and 118 deletions

View File

@ -18,7 +18,6 @@ import (
"github.com/blevesearch/bleve/mapping"
"github.com/blevesearch/bleve/search"
"github.com/blevesearch/bleve/search/highlight/highlighters/ansi"
"github.com/blevesearch/bleve/search/query"
)
var indexMapping mapping.IndexMapping
@ -404,19 +403,6 @@ func ExampleNewConjunctionQuery() {
// document id 2
}
func ExampleNewMatchQueryOperator() {
query := NewMatchQueryOperator("great one", query.MatchQueryOperatorAnd)
searchRequest := NewSearchRequest(query)
searchResults, err := example_index.Search(searchRequest)
if err != nil {
panic(err)
}
fmt.Println(searchResults.Hits[0].ID)
// Output:
// document id 2
}
func ExampleNewDisjunctionQuery() {
disjunct1 := NewMatchQuery("great")
disjunct2 := NewMatchQuery("named")
@ -432,21 +418,6 @@ func ExampleNewDisjunctionQuery() {
// 2
}
func ExampleNewDisjunctionQueryMin() {
disjunct1 := NewMatchQuery("great")
disjunct2 := NewMatchQuery("named")
query := NewDisjunctionQueryMin(2, disjunct1, disjunct2)
searchRequest := NewSearchRequest(query)
searchResults, err := example_index.Search(searchRequest)
if err != nil {
panic(err)
}
fmt.Println(len(searchResults.Hits))
// Output:
// 0
}
func ExampleSearchRequest_SortBy() {
// find docs containing "one", order by Age instead of score
query := NewMatchQuery("one")

View File

@ -65,12 +65,6 @@ func NewDisjunctionQuery(disjuncts ...query.Query) *query.DisjunctionQuery {
return query.NewDisjunctionQuery(disjuncts)
}
// NewDisjunctionQueryMin creates a new compound Query.
// Result documents satisfy at least min Queries.
func NewDisjunctionQueryMin(min float64, disjuncts ...query.Query) *query.DisjunctionQuery {
return query.NewDisjunctionQueryMin(disjuncts, min)
}
// NewDocIDQuery creates a new Query object returning indexed documents among
// the specified set. Combine it with ConjunctionQuery to restrict the scope of
// other queries output.
@ -123,16 +117,6 @@ func NewMatchQuery(match string) *query.MatchQuery {
return query.NewMatchQuery(match)
}
// NewMatchQueryOperator creates a Query for matching text.
// An Analyzer is chosen based on the field.
// Input text is analyzed using this analyzer.
// Token terms resulting from this analysis are
// used to perform term searches. Result documents
// must satisfy term searches according to given operator.
func NewMatchQueryOperator(match string, operator query.MatchQueryOperator) *query.MatchQuery {
return query.NewMatchQueryOperator(match, operator)
}
// NewNumericRangeQuery creates a new Query for ranges
// of numeric values.
// Either, but not both endpoints can be nil.

View File

@ -35,21 +35,13 @@ type BooleanQuery struct {
// Result documents that ALSO satisfy any of the should
// Queries will score higher.
func NewBooleanQuery(must []Query, should []Query, mustNot []Query) *BooleanQuery {
return NewBooleanQueryMinShould(must, should, mustNot, 0.0)
}
// NewBooleanQueryMinShould is the same as
// NewBooleanQuery, only it offers control of the
// minimum number of should queries that must be
// satisfied.
func NewBooleanQueryMinShould(must []Query, should []Query, mustNot []Query, minShould float64) *BooleanQuery {
rv := BooleanQuery{}
if len(must) > 0 {
rv.Must = NewConjunctionQuery(must)
}
if len(should) > 0 {
rv.Should = NewDisjunctionQueryMin(should, minShould)
rv.Should = NewDisjunctionQuery(should)
}
if len(mustNot) > 0 {
rv.MustNot = NewDisjunctionQuery(mustNot)

View File

@ -36,9 +36,10 @@ func (q *ConjunctionQuery) SetBoost(b float64) {
q.Boost = &boost
}
func (q *ConjunctionQuery) AddQuery(aq Query) *ConjunctionQuery {
q.Conjuncts = append(q.Conjuncts, aq)
return q
func (q *ConjunctionQuery) AddQuery(aq ...Query) {
for _, aaq := range aq {
q.Conjuncts = append(q.Conjuncts, aaq)
}
}
func (q *ConjunctionQuery) Searcher(i index.IndexReader, m mapping.IndexMapping, explain bool) (search.Searcher, error) {

View File

@ -33,22 +33,15 @@ func NewDisjunctionQuery(disjuncts []Query) *DisjunctionQuery {
}
}
// NewDisjunctionQueryMin creates a new compound Query.
// Result documents satisfy at least min Queries.
func NewDisjunctionQueryMin(disjuncts []Query, min float64) *DisjunctionQuery {
return &DisjunctionQuery{
Disjuncts: disjuncts,
Min: min,
}
}
func (q *DisjunctionQuery) SetBoost(b float64) {
boost := Boost(b)
q.Boost = &boost
}
func (q *DisjunctionQuery) AddQuery(aq Query) {
q.Disjuncts = append(q.Disjuncts, aq)
func (q *DisjunctionQuery) AddQuery(aq ...Query) {
for _, aaq := range aq {
q.Disjuncts = append(q.Disjuncts, aaq)
}
}
func (q *DisjunctionQuery) SetMin(m float64) {

View File

@ -80,19 +80,6 @@ func NewMatchQuery(match string) *MatchQuery {
}
}
// NewMatchQueryOperator creates a Query for matching text.
// An Analyzer is chosen based on the field.
// Input text is analyzed using this analyzer.
// Token terms resulting from this analysis are
// used to perform term searches. Result documents
// must satisfy term searches according to given operator.
func NewMatchQueryOperator(match string, operator MatchQueryOperator) *MatchQuery {
return &MatchQuery{
Match: match,
Operator: operator,
}
}
func (q *MatchQuery) SetBoost(b float64) {
boost := Boost(b)
q.Boost = &boost
@ -157,7 +144,8 @@ func (q *MatchQuery) Searcher(i index.IndexReader, m mapping.IndexMapping, expla
switch q.Operator {
case MatchQueryOperatorOr:
shouldQuery := NewDisjunctionQueryMin(tqs, 1)
shouldQuery := NewDisjunctionQuery(tqs)
shouldQuery.SetMin(1)
shouldQuery.SetBoost(q.Boost.Value())
return shouldQuery.Searcher(i, m, explain)

View File

@ -70,7 +70,8 @@ func TestParseQuery(t *testing.T) {
{
input: []byte(`{"match":"beer","field":"desc","operator":"and"}`),
output: func() Query {
q := NewMatchQueryOperator("beer", MatchQueryOperatorAnd)
q := NewMatchQuery("beer")
q.SetOperator(MatchQueryOperatorAnd)
q.SetField("desc")
return q
}(),
@ -78,7 +79,8 @@ func TestParseQuery(t *testing.T) {
{
input: []byte(`{"match":"beer","field":"desc","operator":"or"}`),
output: func() Query {
q := NewMatchQueryOperator("beer", MatchQueryOperatorOr)
q := NewMatchQuery("beer")
q.SetOperator(MatchQueryOperatorOr)
q.SetField("desc")
return q
}(),
@ -98,23 +100,26 @@ func TestParseQuery(t *testing.T) {
},
{
input: []byte(`{"must":{"conjuncts": [{"match":"beer","field":"desc"}]},"should":{"disjuncts": [{"match":"water","field":"desc"}],"min":1.0},"must_not":{"disjuncts": [{"match":"devon","field":"desc"}]}}`),
output: NewBooleanQueryMinShould(
[]Query{func() Query {
q := NewMatchQuery("beer")
q.SetField("desc")
return q
}()},
[]Query{func() Query {
q := NewMatchQuery("water")
q.SetField("desc")
return q
}()},
[]Query{func() Query {
q := NewMatchQuery("devon")
q.SetField("desc")
return q
}()},
1.0),
output: func() Query {
q := NewBooleanQuery(
[]Query{func() Query {
q := NewMatchQuery("beer")
q.SetField("desc")
return q
}()},
[]Query{func() Query {
q := NewMatchQuery("water")
q.SetField("desc")
return q
}()},
[]Query{func() Query {
q := NewMatchQuery("devon")
q.SetField("desc")
return q
}()})
q.SetMinShould(1)
return q
}(),
},
{
input: []byte(`{"terms":["watered","down"],"field":"desc"}`),
@ -311,23 +316,26 @@ func TestQueryValidate(t *testing.T) {
err: true,
},
{
query: NewBooleanQueryMinShould(
[]Query{func() Query {
q := NewMatchQuery("beer")
q.SetField("desc")
return q
}()},
[]Query{func() Query {
q := NewMatchQuery("water")
q.SetField("desc")
return q
}()},
[]Query{func() Query {
q := NewMatchQuery("devon")
q.SetField("desc")
return q
}()},
2.0),
query: func() Query {
q := NewBooleanQuery(
[]Query{func() Query {
q := NewMatchQuery("beer")
q.SetField("desc")
return q
}()},
[]Query{func() Query {
q := NewMatchQuery("water")
q.SetField("desc")
return q
}()},
[]Query{func() Query {
q := NewMatchQuery("devon")
q.SetField("desc")
return q
}()})
q.SetMinShould(2)
return q
}(),
err: true,
},
{