examples_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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 bleve
  15. import (
  16. "fmt"
  17. "os"
  18. "testing"
  19. "time"
  20. "github.com/blevesearch/bleve/mapping"
  21. "github.com/blevesearch/bleve/search"
  22. "github.com/blevesearch/bleve/search/highlight/highlighter/ansi"
  23. )
  24. var indexMapping mapping.IndexMapping
  25. var exampleIndex Index
  26. var err error
  27. func TestMain(m *testing.M) {
  28. err = os.RemoveAll("path_to_index")
  29. if err != nil {
  30. panic(err)
  31. }
  32. toRun := m.Run()
  33. if exampleIndex != nil {
  34. err = exampleIndex.Close()
  35. if err != nil {
  36. panic(err)
  37. }
  38. }
  39. err = os.RemoveAll("path_to_index")
  40. if err != nil {
  41. panic(err)
  42. }
  43. os.Exit(toRun)
  44. }
  45. func ExampleNew() {
  46. indexMapping = NewIndexMapping()
  47. exampleIndex, err = New("path_to_index", indexMapping)
  48. if err != nil {
  49. panic(err)
  50. }
  51. count, err := exampleIndex.DocCount()
  52. if err != nil {
  53. panic(err)
  54. }
  55. fmt.Println(count)
  56. // Output:
  57. // 0
  58. }
  59. func ExampleIndex_indexing() {
  60. data := struct {
  61. Name string
  62. Created time.Time
  63. Age int
  64. }{Name: "named one", Created: time.Now(), Age: 50}
  65. data2 := struct {
  66. Name string
  67. Created time.Time
  68. Age int
  69. }{Name: "great nameless one", Created: time.Now(), Age: 25}
  70. // index some data
  71. err = exampleIndex.Index("document id 1", data)
  72. if err != nil {
  73. panic(err)
  74. }
  75. err = exampleIndex.Index("document id 2", data2)
  76. if err != nil {
  77. panic(err)
  78. }
  79. // 2 documents have been indexed
  80. count, err := exampleIndex.DocCount()
  81. if err != nil {
  82. panic(err)
  83. }
  84. fmt.Println(count)
  85. // Output:
  86. // 2
  87. }
  88. // Examples for query related functions
  89. func ExampleNewMatchQuery() {
  90. // finds documents with fields fully matching the given query text
  91. query := NewMatchQuery("named one")
  92. searchRequest := NewSearchRequest(query)
  93. searchResults, err := exampleIndex.Search(searchRequest)
  94. if err != nil {
  95. panic(err)
  96. }
  97. fmt.Println(searchResults.Hits[0].ID)
  98. // Output:
  99. // document id 1
  100. }
  101. func ExampleNewMatchAllQuery() {
  102. // finds all documents in the index
  103. query := NewMatchAllQuery()
  104. searchRequest := NewSearchRequest(query)
  105. searchResults, err := exampleIndex.Search(searchRequest)
  106. if err != nil {
  107. panic(err)
  108. }
  109. fmt.Println(len(searchResults.Hits))
  110. // Output:
  111. // 2
  112. }
  113. func ExampleNewMatchNoneQuery() {
  114. // matches no documents in the index
  115. query := NewMatchNoneQuery()
  116. searchRequest := NewSearchRequest(query)
  117. searchResults, err := exampleIndex.Search(searchRequest)
  118. if err != nil {
  119. panic(err)
  120. }
  121. fmt.Println(len(searchResults.Hits))
  122. // Output:
  123. // 0
  124. }
  125. func ExampleNewMatchPhraseQuery() {
  126. // finds all documents with the given phrase in the index
  127. query := NewMatchPhraseQuery("nameless one")
  128. searchRequest := NewSearchRequest(query)
  129. searchResults, err := exampleIndex.Search(searchRequest)
  130. if err != nil {
  131. panic(err)
  132. }
  133. fmt.Println(searchResults.Hits[0].ID)
  134. // Output:
  135. // document id 2
  136. }
  137. func ExampleNewNumericRangeQuery() {
  138. value1 := float64(11)
  139. value2 := float64(100)
  140. data := struct{ Priority float64 }{Priority: float64(15)}
  141. data2 := struct{ Priority float64 }{Priority: float64(10)}
  142. err = exampleIndex.Index("document id 3", data)
  143. if err != nil {
  144. panic(err)
  145. }
  146. err = exampleIndex.Index("document id 4", data2)
  147. if err != nil {
  148. panic(err)
  149. }
  150. query := NewNumericRangeQuery(&value1, &value2)
  151. searchRequest := NewSearchRequest(query)
  152. searchResults, err := exampleIndex.Search(searchRequest)
  153. if err != nil {
  154. panic(err)
  155. }
  156. fmt.Println(searchResults.Hits[0].ID)
  157. // Output:
  158. // document id 3
  159. }
  160. func ExampleNewNumericRangeInclusiveQuery() {
  161. value1 := float64(10)
  162. value2 := float64(100)
  163. v1incl := false
  164. v2incl := false
  165. query := NewNumericRangeInclusiveQuery(&value1, &value2, &v1incl, &v2incl)
  166. searchRequest := NewSearchRequest(query)
  167. searchResults, err := exampleIndex.Search(searchRequest)
  168. if err != nil {
  169. panic(err)
  170. }
  171. fmt.Println(searchResults.Hits[0].ID)
  172. // Output:
  173. // document id 3
  174. }
  175. func ExampleNewPhraseQuery() {
  176. // finds all documents with the given phrases in the given field in the index
  177. query := NewPhraseQuery([]string{"nameless", "one"}, "Name")
  178. searchRequest := NewSearchRequest(query)
  179. searchResults, err := exampleIndex.Search(searchRequest)
  180. if err != nil {
  181. panic(err)
  182. }
  183. fmt.Println(searchResults.Hits[0].ID)
  184. // Output:
  185. // document id 2
  186. }
  187. func ExampleNewPrefixQuery() {
  188. // finds all documents with terms having the given prefix in the index
  189. query := NewPrefixQuery("name")
  190. searchRequest := NewSearchRequest(query)
  191. searchResults, err := exampleIndex.Search(searchRequest)
  192. if err != nil {
  193. panic(err)
  194. }
  195. fmt.Println(len(searchResults.Hits))
  196. // Output:
  197. // 2
  198. }
  199. func ExampleNewQueryStringQuery() {
  200. query := NewQueryStringQuery("+one -great")
  201. searchRequest := NewSearchRequest(query)
  202. searchResults, err := exampleIndex.Search(searchRequest)
  203. if err != nil {
  204. panic(err)
  205. }
  206. fmt.Println(searchResults.Hits[0].ID)
  207. // Output:
  208. // document id 1
  209. }
  210. func ExampleNewTermQuery() {
  211. query := NewTermQuery("great")
  212. searchRequest := NewSearchRequest(query)
  213. searchResults, err := exampleIndex.Search(searchRequest)
  214. if err != nil {
  215. panic(err)
  216. }
  217. fmt.Println(searchResults.Hits[0].ID)
  218. // Output:
  219. // document id 2
  220. }
  221. func ExampleNewFacetRequest() {
  222. facet := NewFacetRequest("Name", 1)
  223. query := NewMatchAllQuery()
  224. searchRequest := NewSearchRequest(query)
  225. searchRequest.AddFacet("facet name", facet)
  226. searchResults, err := exampleIndex.Search(searchRequest)
  227. if err != nil {
  228. panic(err)
  229. }
  230. // total number of terms
  231. fmt.Println(searchResults.Facets["facet name"].Total)
  232. // numer of docs with no value for this field
  233. fmt.Println(searchResults.Facets["facet name"].Missing)
  234. // term with highest occurrences in field name
  235. fmt.Println(searchResults.Facets["facet name"].Terms[0].Term)
  236. // Output:
  237. // 5
  238. // 2
  239. // one
  240. }
  241. func ExampleFacetRequest_AddDateTimeRange() {
  242. facet := NewFacetRequest("Created", 1)
  243. facet.AddDateTimeRange("range name", time.Unix(0, 0), time.Now())
  244. query := NewMatchAllQuery()
  245. searchRequest := NewSearchRequest(query)
  246. searchRequest.AddFacet("facet name", facet)
  247. searchResults, err := exampleIndex.Search(searchRequest)
  248. if err != nil {
  249. panic(err)
  250. }
  251. // dates in field Created since starting of unix time till now
  252. fmt.Println(searchResults.Facets["facet name"].DateRanges[0].Count)
  253. // Output:
  254. // 2
  255. }
  256. func ExampleFacetRequest_AddNumericRange() {
  257. value1 := float64(11)
  258. facet := NewFacetRequest("Priority", 1)
  259. facet.AddNumericRange("range name", &value1, nil)
  260. query := NewMatchAllQuery()
  261. searchRequest := NewSearchRequest(query)
  262. searchRequest.AddFacet("facet name", facet)
  263. searchResults, err := exampleIndex.Search(searchRequest)
  264. if err != nil {
  265. panic(err)
  266. }
  267. // number documents with field Priority in the given range
  268. fmt.Println(searchResults.Facets["facet name"].NumericRanges[0].Count)
  269. // Output:
  270. // 1
  271. }
  272. func ExampleNewHighlight() {
  273. query := NewMatchQuery("nameless")
  274. searchRequest := NewSearchRequest(query)
  275. searchRequest.Highlight = NewHighlight()
  276. searchResults, err := exampleIndex.Search(searchRequest)
  277. if err != nil {
  278. panic(err)
  279. }
  280. fmt.Println(searchResults.Hits[0].Fragments["Name"][0])
  281. // Output:
  282. // great <mark>nameless</mark> one
  283. }
  284. func ExampleNewHighlightWithStyle() {
  285. query := NewMatchQuery("nameless")
  286. searchRequest := NewSearchRequest(query)
  287. searchRequest.Highlight = NewHighlightWithStyle(ansi.Name)
  288. searchResults, err := exampleIndex.Search(searchRequest)
  289. if err != nil {
  290. panic(err)
  291. }
  292. fmt.Println(searchResults.Hits[0].Fragments["Name"][0])
  293. // Output:
  294. // great nameless one
  295. }
  296. func ExampleSearchRequest_AddFacet() {
  297. facet := NewFacetRequest("Name", 1)
  298. query := NewMatchAllQuery()
  299. searchRequest := NewSearchRequest(query)
  300. searchRequest.AddFacet("facet name", facet)
  301. searchResults, err := exampleIndex.Search(searchRequest)
  302. if err != nil {
  303. panic(err)
  304. }
  305. // total number of terms
  306. fmt.Println(searchResults.Facets["facet name"].Total)
  307. // numer of docs with no value for this field
  308. fmt.Println(searchResults.Facets["facet name"].Missing)
  309. // term with highest occurrences in field name
  310. fmt.Println(searchResults.Facets["facet name"].Terms[0].Term)
  311. // Output:
  312. // 5
  313. // 2
  314. // one
  315. }
  316. func ExampleNewSearchRequest() {
  317. // finds documents with fields fully matching the given query text
  318. query := NewMatchQuery("named one")
  319. searchRequest := NewSearchRequest(query)
  320. searchResults, err := exampleIndex.Search(searchRequest)
  321. if err != nil {
  322. panic(err)
  323. }
  324. fmt.Println(searchResults.Hits[0].ID)
  325. // Output:
  326. // document id 1
  327. }
  328. func ExampleNewBooleanQuery() {
  329. must := NewMatchQuery("one")
  330. mustNot := NewMatchQuery("great")
  331. query := NewBooleanQuery()
  332. query.AddMust(must)
  333. query.AddMustNot(mustNot)
  334. searchRequest := NewSearchRequest(query)
  335. searchResults, err := exampleIndex.Search(searchRequest)
  336. if err != nil {
  337. panic(err)
  338. }
  339. fmt.Println(searchResults.Hits[0].ID)
  340. // Output:
  341. // document id 1
  342. }
  343. func ExampleNewConjunctionQuery() {
  344. conjunct1 := NewMatchQuery("great")
  345. conjunct2 := NewMatchQuery("one")
  346. query := NewConjunctionQuery(conjunct1, conjunct2)
  347. searchRequest := NewSearchRequest(query)
  348. searchResults, err := exampleIndex.Search(searchRequest)
  349. if err != nil {
  350. panic(err)
  351. }
  352. fmt.Println(searchResults.Hits[0].ID)
  353. // Output:
  354. // document id 2
  355. }
  356. func ExampleNewDisjunctionQuery() {
  357. disjunct1 := NewMatchQuery("great")
  358. disjunct2 := NewMatchQuery("named")
  359. query := NewDisjunctionQuery(disjunct1, disjunct2)
  360. searchRequest := NewSearchRequest(query)
  361. searchResults, err := exampleIndex.Search(searchRequest)
  362. if err != nil {
  363. panic(err)
  364. }
  365. fmt.Println(len(searchResults.Hits))
  366. // Output:
  367. // 2
  368. }
  369. func ExampleSearchRequest_SortBy() {
  370. // find docs containing "one", order by Age instead of score
  371. query := NewMatchQuery("one")
  372. searchRequest := NewSearchRequest(query)
  373. searchRequest.SortBy([]string{"Age"})
  374. searchResults, err := exampleIndex.Search(searchRequest)
  375. if err != nil {
  376. panic(err)
  377. }
  378. fmt.Println(searchResults.Hits[0].ID)
  379. fmt.Println(searchResults.Hits[1].ID)
  380. // Output:
  381. // document id 2
  382. // document id 1
  383. }
  384. func ExampleSearchRequest_SortByCustom() {
  385. // find all docs, order by Age, with docs missing Age field first
  386. query := NewMatchAllQuery()
  387. searchRequest := NewSearchRequest(query)
  388. searchRequest.SortByCustom(search.SortOrder{
  389. &search.SortField{
  390. Field: "Age",
  391. Missing: search.SortFieldMissingFirst,
  392. },
  393. })
  394. searchResults, err := exampleIndex.Search(searchRequest)
  395. if err != nil {
  396. panic(err)
  397. }
  398. fmt.Println(searchResults.Hits[0].ID)
  399. fmt.Println(searchResults.Hits[1].ID)
  400. fmt.Println(searchResults.Hits[2].ID)
  401. fmt.Println(searchResults.Hits[3].ID)
  402. // Output:
  403. // document id 3
  404. // document id 4
  405. // document id 2
  406. // document id 1
  407. }