search_queries_more_like_this_test.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2012-present Oliver Eilhard. All rights reserved.
  2. // Use of this source code is governed by a MIT-license.
  3. // See http://olivere.mit-license.org/license.txt for details.
  4. package elastic
  5. import (
  6. "context"
  7. "encoding/json"
  8. "testing"
  9. )
  10. func TestMoreLikeThisQuerySourceWithLikeText(t *testing.T) {
  11. q := NewMoreLikeThisQuery().LikeText("Golang topic").Field("message")
  12. src, err := q.Source()
  13. if err != nil {
  14. t.Fatal(err)
  15. }
  16. data, err := json.Marshal(src)
  17. if err != nil {
  18. t.Fatal(err)
  19. }
  20. got := string(data)
  21. expected := `{"more_like_this":{"fields":["message"],"like":["Golang topic"]}}`
  22. if got != expected {
  23. t.Fatalf("expected\n%s\n,got:\n%s", expected, got)
  24. }
  25. }
  26. func TestMoreLikeThisQuerySourceWithLikeAndUnlikeItems(t *testing.T) {
  27. q := NewMoreLikeThisQuery()
  28. q = q.LikeItems(
  29. NewMoreLikeThisQueryItem().Id("1"),
  30. NewMoreLikeThisQueryItem().Index(testIndexName2).Type("comment").Id("2").Routing("routing_id"),
  31. )
  32. q = q.IgnoreLikeItems(NewMoreLikeThisQueryItem().Id("3"))
  33. src, err := q.Source()
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. data, err := json.Marshal(src)
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. got := string(data)
  42. expected := `{"more_like_this":{"like":[{"_id":"1"},{"_id":"2","_index":"elastic-test2","_routing":"routing_id","_type":"comment"}],"unlike":[{"_id":"3"}]}}`
  43. if got != expected {
  44. t.Fatalf("expected\n%s\n,got:\n%s", expected, got)
  45. }
  46. }
  47. func TestMoreLikeThisQuery(t *testing.T) {
  48. client := setupTestClientAndCreateIndex(t)
  49. tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."}
  50. tweet2 := tweet{User: "olivere", Message: "Another Golang topic."}
  51. tweet3 := tweet{User: "sandrae", Message: "Cycling is fun."}
  52. // Add all documents
  53. _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO())
  54. if err != nil {
  55. t.Fatal(err)
  56. }
  57. _, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO())
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. _, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO())
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. _, err = client.Flush().Index(testIndexName).Do(context.TODO())
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. // Common query
  70. mltq := NewMoreLikeThisQuery().LikeText("Golang topic").Field("message")
  71. res, err := client.Search().
  72. Index(testIndexName).
  73. Query(mltq).
  74. Do(context.TODO())
  75. if err != nil {
  76. t.Fatal(err)
  77. }
  78. if res.Hits == nil {
  79. t.Errorf("expected SearchResult.Hits != nil; got nil")
  80. }
  81. }