Skip to content

Commit

Permalink
Simplify JSON API for phrase query
Browse files Browse the repository at this point in the history
New API looks like this:

    {"query":{"terms":["watered","down"],"field":"desc"}}

instead of

    {"query":{"terms":[{"term":"watered","field":"desc"},{"term":"down","field":"desc"}]}}

So that it eliminats accidental errors by supplying terms with different
fields, or different type of query
  • Loading branch information
avsej committed Nov 2, 2015
1 parent 3065106 commit 8609e7a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 35 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
/utils/bleve_query/bleve_query
/utils/bleve_registry/bleve_registry
/y.output
*.test
9 changes: 2 additions & 7 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,6 @@ func ParseQuery(input []byte) (Query, error) {
if rv.Boost() == 0 {
rv.SetBoost(1)
}
for _, tq := range rv.TermQueries {
if tq.Boost() == 0 {
tq.SetBoost(1)
}
}
return &rv, nil
}
_, hasConjuncts := tmp["conjuncts"]
Expand Down Expand Up @@ -273,11 +268,11 @@ func expandQuery(m *IndexMapping, query Query) (Query, error) {
return &q, nil
case *phraseQuery:
q := *query.(*phraseQuery)
children, err := expandSlice(q.TermQueries)
children, err := expandSlice(q.termQueries)
if err != nil {
return nil, err
}
q.TermQueries = children
q.termQueries = children
return &q, nil
default:
return query, nil
Expand Down
45 changes: 18 additions & 27 deletions query_phrase.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ package bleve

import (
"encoding/json"
"fmt"

"github.com/blevesearch/bleve/index"
"github.com/blevesearch/bleve/search"
"github.com/blevesearch/bleve/search/searchers"
)

type phraseQuery struct {
TermQueries []Query `json:"terms"`
BoostVal float64 `json:"boost,omitempty"`
terms []string
Terms []string `json:"terms"`
FieldVal string `json:"field,omitempty"`
BoostVal float64 `json:"boost,omitempty"`
termQueries []Query
}

// NewPhraseQuery creates a new Query for finding
Expand All @@ -37,9 +37,10 @@ func NewPhraseQuery(terms []string, field string) *phraseQuery {
}
}
return &phraseQuery{
TermQueries: termQueries,
Terms: terms,
FieldVal: field,
BoostVal: 1.0,
terms: terms,
termQueries: termQueries,
}
}

Expand All @@ -54,48 +55,38 @@ func (q *phraseQuery) SetBoost(b float64) Query {

func (q *phraseQuery) Searcher(i index.IndexReader, m *IndexMapping, explain bool) (search.Searcher, error) {

conjunctionQuery := NewConjunctionQuery(q.TermQueries)
conjunctionQuery := NewConjunctionQuery(q.termQueries)
conjunctionSearcher, err := conjunctionQuery.Searcher(i, m, explain)
if err != nil {
return nil, err
}
return searchers.NewPhraseSearcher(i, conjunctionSearcher.(*searchers.ConjunctionSearcher), q.terms)
return searchers.NewPhraseSearcher(i, conjunctionSearcher.(*searchers.ConjunctionSearcher), q.Terms)
}

func (q *phraseQuery) Validate() error {
if len(q.TermQueries) < 1 {
if len(q.termQueries) < 1 {
return ErrorPhraseQueryNoTerms
}
return nil
}

func (q *phraseQuery) UnmarshalJSON(data []byte) error {
tmp := struct {
Terms []json.RawMessage `json:"terms"`
BoostVal float64 `json:"boost,omitempty"`
}{}
type _phraseQuery phraseQuery
tmp := _phraseQuery{}
err := json.Unmarshal(data, &tmp)
if err != nil {
return err
}
q.TermQueries = make([]Query, len(tmp.Terms))
q.terms = make([]string, 0)
for i, term := range tmp.Terms {
query, err := ParseQuery(term)
if err != nil {
return err
}
q.TermQueries[i] = query
tq, isTermQuery := query.(*termQuery)
if !isTermQuery {
return fmt.Errorf("phrase query can only contain term queries")
}
q.terms = append(q.terms, tq.Term)
}
q.Terms = tmp.Terms
q.FieldVal = tmp.FieldVal
q.BoostVal = tmp.BoostVal
if q.BoostVal == 0 {
q.BoostVal = 1
}
q.termQueries = make([]Query, len(q.Terms))
for i, term := range q.Terms {
q.termQueries[i] = &termQuery{Term: term, FieldVal: q.FieldVal, BoostVal: q.BoostVal}
}
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestParseQuery(t *testing.T) {
[]Query{NewMatchQuery("devon").SetField("desc")}),
},
{
input: []byte(`{"terms":[{"term":"watered","field":"desc"},{"term":"down","field":"desc"}]}`),
input: []byte(`{"terms":["watered","down"],"field":"desc"}`),
output: NewPhraseQuery([]string{"watered", "down"}, "desc"),
},
{
Expand Down

0 comments on commit 8609e7a

Please sign in to comment.