Search code examples
elasticsearchfacetsearchkick

Searchkick - Query String Query


From this page, I am trying this:

[15] pry(main)> puts Post.search("price:>600").size
  Post Search (18.7ms)  curl http://localhost:9200/posts_development/_search?pretty -d '{"query":{"dis_max":{"queries":[{"match":{"_all":{"query":"price:\u003e600","operator":"and","boost":10,"analyzer":"searchkick_search"}}},{"match":{"_all":{"query":"price:\u003e600","operator":"and","boost":10,"analyzer":"searchkick_search2"}}},{"match":{"_all":{"query":"price:\u003e600","operator":"and","boost":1,"fuzziness":1,"max_expansions":3,"analyzer":"searchkick_search"}}},{"match":{"_all":{"query":"price:\u003e600","operator":"and","boost":1,"fuzziness":1,"max_expansions":3,"analyzer":"searchkick_search2"}}}]}},"size":100000,"from":0,"fields":[]}'
0
=> nil

While I do have entries with price greater than 600:

[16] pry(main)> puts Post.where("price>600").size
   (0.2ms)  SELECT COUNT(*) FROM "posts" WHERE (price>600)
45
=> nil

Any advice why these two have different outputs? is it maybe the scaped character "query":"price:\u003e600"?

I also tried:

[37] pry(main)> Post.search(where: {price: {gt: 600}}).size
  Post Search (9.8ms)  curl http://localhost:9200/posts_development/_search?pretty -d '{"query":{"match_all":{}},"size":100000,"from":0,"filter":{"and":[{"range":{"price":{"from":600,"include_lower":true}}}]},"fields":[]}'
  Post Load (0.5ms)  SELECT "posts".* FROM "posts" WHERE "posts"."id" IN (4, 9, 11, 16, 28, 35, 42, 5, 12, 17, 24, 29, 31, 36, 43, 48, 50, 1, 6, 13, 20, 25, 32, 37, 44, 2, 7, 14, 19, 21, 26, 33, 38, 40, 45, 3, 8, 10, 15, 22, 27, 34, 39, 41, 46)
=> 45

[38] pry(main)> Post.search(facets: {price: {ranges: [{from: 600}] }  }).size
  Post Search (10.4ms)  curl http://localhost:9200/posts_development/_search?pretty -d '{"query":{"match_all":{}},"size":100000,"from":0,"facets":{"price":{"range":{"price":[{"from":600}]}}},"fields":[]}'
  Post Load (0.7ms)  SELECT "posts".* FROM "posts" WHERE "posts"."id" IN (4, 9, 11, 16, 23, 28, 30, 35, 42, 47, 5, 12, 17, 24, 29, 31, 36, 43, 48, 50, 1, 6, 13, 18, 20, 25, 32, 37, 44, 49, 2, 7, 14, 19, 21, 26, 33, 38, 40, 45, 3, 8, 10, 15, 22, 27, 34, 39, 41, 46)
=> 50

where makes what I expect: 45 posts. But I can't understand facet, could anyone help me with that?

This question is also here.


Solution

  • Figured it out:

    [42] pry(main)> Post.search(query: {query_string: {query: 'price:>600'}}).size
      Post Search (8.8ms)  curl http://localhost:9200/posts_development/_search?pretty -d '{"query":{"query_string":{"query":"price:\u003e600"}},"size":100000,"from":0,"fields":[]}'
      Post Load (0.5ms)  SELECT "posts".* FROM "posts" WHERE "posts"."id" IN (4, 9, 11, 16, 28, 35, 42, 5, 12, 17, 24, 29, 31, 36, 43, 48, 50, 1, 6, 13, 20, 25, 32, 37, 44, 2, 7, 14, 19, 21, 26, 33, 38, 40, 45, 3, 8, 10, 15, 22, 27, 34, 39, 41, 46)
    => 45
    

    Although I am not understanding facet yet.