Elasticsearch multi term filter

前端 未结 2 1765
伪装坚强ぢ
伪装坚强ぢ 2021-01-30 08:57

I\'m quite new to Elasticsearch, so here\'s my question. I wanna do a search query with elasticsearch and wanna filter with multiple terms.

If I want to search for a use

2条回答
  •  [愿得一人]
    2021-01-30 09:12

    You should use bool filter to AND all your terms:

    "query":{
        "filtered": {
            "query": {
                "query_string": {
                    "query":"*tom*",
                    "default_operator": "OR",
                    "fields": ["username"]
                }
            },
            "filter": {
                "bool" : {
                    "must" : [
                        {"term" : { "isActive" : "1" } },
                        {"term" : { "isPrivate" : "0" } },
                        {"term" : { "isOwner" : "1" } }
                    ]
                 }
             }
         }
    }   
    

    For version 2.x+ you can use bool query instead of filtered query with some simple replacement: https://www.elastic.co/guide/en/elasticsearch/reference/7.4/query-dsl-filtered-query.html

提交回复
热议问题