Elasticsearch difference between MUST and SHOULD bool query

前端 未结 3 1973
难免孤独
难免孤独 2020-12-07 08:27

What is the difference between MUST and SHOULD bool query in ES?

If I ONLY want results that contain my terms should I the

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-07 08:47

    Since this is a popular question, I would like to add that in Elasticsearch version 2 things changed a bit.

    Instead of filtered query, one should use bool query in the top level.

    If you don't care about the score of must parts, then put those parts into filter key. No scoring means faster search. Also, Elasticsearch will automatically figure out, whether to cache them, etc. must_not is equally valid for caching.

    Reference: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html

    Also, mind that "gte": "now" cannot be cached, because of millisecond granularity. Use two ranges in a must clause: one with now/1h and another with now so that the first can be cached for a while and the second for precise filtering accelerated on a smaller result set.

提交回复
热议问题