You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

multi_phrase.go 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright (c) 2014 Couchbase, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package query
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "github.com/blevesearch/bleve/index"
  19. "github.com/blevesearch/bleve/mapping"
  20. "github.com/blevesearch/bleve/search"
  21. "github.com/blevesearch/bleve/search/searcher"
  22. )
  23. type MultiPhraseQuery struct {
  24. Terms [][]string `json:"terms"`
  25. Field string `json:"field,omitempty"`
  26. BoostVal *Boost `json:"boost,omitempty"`
  27. }
  28. // NewMultiPhraseQuery creates a new Query for finding
  29. // term phrases in the index.
  30. // It is like PhraseQuery, but each position in the
  31. // phrase may be satisfied by a list of terms
  32. // as opposed to just one.
  33. // At least one of the terms must exist in the correct
  34. // order, at the correct index offsets, in the
  35. // specified field. Queried field must have been indexed with
  36. // IncludeTermVectors set to true.
  37. func NewMultiPhraseQuery(terms [][]string, field string) *MultiPhraseQuery {
  38. return &MultiPhraseQuery{
  39. Terms: terms,
  40. Field: field,
  41. }
  42. }
  43. func (q *MultiPhraseQuery) SetBoost(b float64) {
  44. boost := Boost(b)
  45. q.BoostVal = &boost
  46. }
  47. func (q *MultiPhraseQuery) Boost() float64 {
  48. return q.BoostVal.Value()
  49. }
  50. func (q *MultiPhraseQuery) Searcher(i index.IndexReader, m mapping.IndexMapping, options search.SearcherOptions) (search.Searcher, error) {
  51. return searcher.NewMultiPhraseSearcher(i, q.Terms, q.Field, options)
  52. }
  53. func (q *MultiPhraseQuery) Validate() error {
  54. if len(q.Terms) < 1 {
  55. return fmt.Errorf("phrase query must contain at least one term")
  56. }
  57. return nil
  58. }
  59. func (q *MultiPhraseQuery) UnmarshalJSON(data []byte) error {
  60. type _mphraseQuery MultiPhraseQuery
  61. tmp := _mphraseQuery{}
  62. err := json.Unmarshal(data, &tmp)
  63. if err != nil {
  64. return err
  65. }
  66. q.Terms = tmp.Terms
  67. q.Field = tmp.Field
  68. q.BoostVal = tmp.BoostVal
  69. return nil
  70. }