versus_score_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright (c) 2018 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 test
  15. import (
  16. "fmt"
  17. "os"
  18. "strconv"
  19. "testing"
  20. "github.com/blevesearch/bleve"
  21. "github.com/blevesearch/bleve/document"
  22. "github.com/blevesearch/bleve/index/scorch"
  23. "github.com/blevesearch/bleve/index/upsidedown"
  24. "github.com/blevesearch/bleve/mapping"
  25. "github.com/blevesearch/bleve/search"
  26. )
  27. func TestDisjunctionSearchScoreIndexWithCompositeFields(t *testing.T) {
  28. upHits := disjunctionQueryiOnIndexWithCompositeFields(upsidedown.Name, t)
  29. scHits := disjunctionQueryiOnIndexWithCompositeFields(scorch.Name, t)
  30. if upHits[0].ID != scHits[0].ID || upHits[1].ID != scHits[1].ID {
  31. t.Errorf("upsidedown, scorch returned different docs;\n"+
  32. "upsidedown: (%s, %s), scorch: (%s, %s)\n",
  33. upHits[0].ID, upHits[1].ID, scHits[0].ID, scHits[1].ID)
  34. }
  35. if scHits[0].Score != upHits[0].Score || scHits[1].Score != upHits[1].Score {
  36. t.Errorf("upsidedown, scorch showing different scores;\n"+
  37. "upsidedown: (%+v, %+v), scorch: (%+v, %+v)\n",
  38. *upHits[0].Expl, *upHits[1].Expl, *scHits[0].Expl, *scHits[1].Expl)
  39. }
  40. }
  41. func disjunctionQueryiOnIndexWithCompositeFields(indexName string,
  42. t *testing.T) []*search.DocumentMatch {
  43. // create an index
  44. idxMapping := mapping.NewIndexMapping()
  45. idx, err := bleve.NewUsing("testidx", idxMapping, indexName,
  46. bleve.Config.DefaultKVStore, nil)
  47. if err != nil {
  48. t.Error(err)
  49. }
  50. defer func() {
  51. err = idx.Close()
  52. if err != nil {
  53. t.Error(err)
  54. }
  55. err = os.RemoveAll("testidx")
  56. if err != nil {
  57. t.Error(err)
  58. }
  59. }()
  60. // create and insert documents as a batch
  61. batch := idx.NewBatch()
  62. docs := []struct {
  63. field1 string
  64. field2 int
  65. }{
  66. {
  67. field1: "one",
  68. field2: 1,
  69. },
  70. {
  71. field1: "two",
  72. field2: 2,
  73. },
  74. }
  75. for i := 0; i < len(docs); i++ {
  76. doc := document.NewDocument(strconv.Itoa(docs[i].field2))
  77. doc.Fields = []document.Field{
  78. document.NewTextField("field1", []uint64{}, []byte(docs[i].field1)),
  79. document.NewNumericField("field2", []uint64{}, float64(docs[i].field2)),
  80. }
  81. doc.CompositeFields = []*document.CompositeField{
  82. document.NewCompositeFieldWithIndexingOptions(
  83. "_all", true, []string{"field1"}, []string{},
  84. document.IndexField|document.IncludeTermVectors),
  85. }
  86. if err = batch.IndexAdvanced(doc); err != nil {
  87. t.Error(err)
  88. }
  89. }
  90. if err = idx.Batch(batch); err != nil {
  91. t.Error(err)
  92. }
  93. /*
  94. Query:
  95. DISJ
  96. / \
  97. CONJ TERM(two)
  98. /
  99. TERM(one)
  100. */
  101. tq1 := bleve.NewTermQuery("one")
  102. tq1.SetBoost(2)
  103. tq2 := bleve.NewTermQuery("two")
  104. tq2.SetBoost(3)
  105. cq := bleve.NewConjunctionQuery(tq1)
  106. cq.SetBoost(4)
  107. q := bleve.NewDisjunctionQuery(tq1, tq2)
  108. sr := bleve.NewSearchRequestOptions(q, 2, 0, true)
  109. res, err := idx.Search(sr)
  110. if err != nil {
  111. t.Error(err)
  112. }
  113. if len(res.Hits) != 2 {
  114. t.Errorf(fmt.Sprintf("indexType: %s Expected 2 hits, "+
  115. "but got: %v", indexName, len(res.Hits)))
  116. }
  117. return res.Hits
  118. }