1

从这个页面,我正在尝试这个:

[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

虽然我确实有价格大于 600 的条目:

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

为什么这两个有不同的输出有什么建议?可能是逃跑的角色"query":"price:\u003e600"吗?

我也试过:

[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达到了我的预期:45 个帖子。但我不明白facet,有没有人能帮帮我?

这个问题也在这里

4

1 回答 1

1

弄清楚了:

[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

虽然我还不明白facet

于 2015-03-26T03:51:32.997 回答