es同个字段,多个值搜索的案例

search_pdf_query = {"query": {
            "bool":
                {
                "must": [
                            {"bool": {"should":[{"match_phrase": {"pdf_title":"年度报告"}},{"match_phrase":{"pdf_title":"季度报告"}}]}},
                            {"range": {"announce_date": {"gte": "2017-01-01","lte": "2018-12-31"}}},
                            {"terms": {"html_status": ["201", "300", "301", "302"]}},
                            {"terms": {"trade_code.keyword": ["600548","000548"]}}],
               "must_not": [{"wildcard": {"origin_title.keyword": '*摘要'}},{"wildcard": {"origin_title.keyword": '*年度报告书'}}],
               }},
        "sort": [{"announce_date": "asc"}]
        }

查找值包含某些文本的情况:

  1. 完全匹配情况下,对同一个字段的多个值搜索,使用terms查询,举例:
    {"terms": {"html_status": ["201", "300", "301", "302"]}}
  2. 如果不完全匹配,则在must里添加bool查询,再在bool里添加should查询,举例:
    {"bool": {"should":[{"match_phrase": {"pdf_title":"年度报告"}},{"match_phrase":{"pdf_title":"季度报告"}}]}}

如何区分使用match,match_phrase,term可参考此篇
https://blog.csdn.net/camelcanoe/article/details/79544155
这里也涉及到查询的字段是否是 NOT_ANALYZED,ANALYZED字段无法使用term,只能使用match_phrase。
所以在新建index的时候需要把mapping设置好,哪些字段是ANALYZED,哪些是NOT_ANALYZED(感觉是个坑)

你可能感兴趣的:(es同个字段,多个值搜索的案例)