Merge pull request #265 from avsej/simplify-phrase-api
Simplify JSON API for phrase query
This commit is contained in:
commit
a873c76edc
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -15,3 +15,4 @@
|
||||||
/utils/bleve_query/bleve_query
|
/utils/bleve_query/bleve_query
|
||||||
/utils/bleve_registry/bleve_registry
|
/utils/bleve_registry/bleve_registry
|
||||||
/y.output
|
/y.output
|
||||||
|
*.test
|
||||||
|
|
9
query.go
9
query.go
|
@ -108,11 +108,6 @@ func ParseQuery(input []byte) (Query, error) {
|
||||||
if rv.Boost() == 0 {
|
if rv.Boost() == 0 {
|
||||||
rv.SetBoost(1)
|
rv.SetBoost(1)
|
||||||
}
|
}
|
||||||
for _, tq := range rv.TermQueries {
|
|
||||||
if tq.Boost() == 0 {
|
|
||||||
tq.SetBoost(1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return &rv, nil
|
return &rv, nil
|
||||||
}
|
}
|
||||||
_, hasConjuncts := tmp["conjuncts"]
|
_, hasConjuncts := tmp["conjuncts"]
|
||||||
|
@ -273,11 +268,11 @@ func expandQuery(m *IndexMapping, query Query) (Query, error) {
|
||||||
return &q, nil
|
return &q, nil
|
||||||
case *phraseQuery:
|
case *phraseQuery:
|
||||||
q := *query.(*phraseQuery)
|
q := *query.(*phraseQuery)
|
||||||
children, err := expandSlice(q.TermQueries)
|
children, err := expandSlice(q.termQueries)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
q.TermQueries = children
|
q.termQueries = children
|
||||||
return &q, nil
|
return &q, nil
|
||||||
default:
|
default:
|
||||||
return query, nil
|
return query, nil
|
||||||
|
|
|
@ -11,7 +11,6 @@ package bleve
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/blevesearch/bleve/index"
|
"github.com/blevesearch/bleve/index"
|
||||||
"github.com/blevesearch/bleve/search"
|
"github.com/blevesearch/bleve/search"
|
||||||
|
@ -19,9 +18,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type phraseQuery struct {
|
type phraseQuery struct {
|
||||||
TermQueries []Query `json:"terms"`
|
Terms []string `json:"terms"`
|
||||||
BoostVal float64 `json:"boost,omitempty"`
|
FieldVal string `json:"field,omitempty"`
|
||||||
terms []string
|
BoostVal float64 `json:"boost,omitempty"`
|
||||||
|
termQueries []Query
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewPhraseQuery creates a new Query for finding
|
// NewPhraseQuery creates a new Query for finding
|
||||||
|
@ -37,9 +37,10 @@ func NewPhraseQuery(terms []string, field string) *phraseQuery {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return &phraseQuery{
|
return &phraseQuery{
|
||||||
TermQueries: termQueries,
|
Terms: terms,
|
||||||
|
FieldVal: field,
|
||||||
BoostVal: 1.0,
|
BoostVal: 1.0,
|
||||||
terms: terms,
|
termQueries: termQueries,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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) {
|
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)
|
conjunctionSearcher, err := conjunctionQuery.Searcher(i, m, explain)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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 {
|
func (q *phraseQuery) Validate() error {
|
||||||
if len(q.TermQueries) < 1 {
|
if len(q.termQueries) < 1 {
|
||||||
return ErrorPhraseQueryNoTerms
|
return ErrorPhraseQueryNoTerms
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *phraseQuery) UnmarshalJSON(data []byte) error {
|
func (q *phraseQuery) UnmarshalJSON(data []byte) error {
|
||||||
tmp := struct {
|
type _phraseQuery phraseQuery
|
||||||
Terms []json.RawMessage `json:"terms"`
|
tmp := _phraseQuery{}
|
||||||
BoostVal float64 `json:"boost,omitempty"`
|
|
||||||
}{}
|
|
||||||
err := json.Unmarshal(data, &tmp)
|
err := json.Unmarshal(data, &tmp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
q.TermQueries = make([]Query, len(tmp.Terms))
|
q.Terms = tmp.Terms
|
||||||
q.terms = make([]string, 0)
|
q.FieldVal = tmp.FieldVal
|
||||||
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.BoostVal = tmp.BoostVal
|
q.BoostVal = tmp.BoostVal
|
||||||
if q.BoostVal == 0 {
|
if q.BoostVal == 0 {
|
||||||
q.BoostVal = 1
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -46,7 +46,7 @@ func TestParseQuery(t *testing.T) {
|
||||||
[]Query{NewMatchQuery("devon").SetField("desc")}),
|
[]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"),
|
output: NewPhraseQuery([]string{"watered", "down"}, "desc"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue
Block a user