ElasticSearch, multi-match with filter?

后端 未结 4 387
情深已故
情深已故 2021-01-30 08:30

I have a multi-match query in ES, and wish to add a filter.

{
  \"multi_match\" : {
    \"query\" : \"this is a test\",
    \"fields\" : [ \"subject^2\", \"messa         


        
4条回答
  •  我在风中等你
    2021-01-30 09:13

    Depending on what you need you have to put the filter in the proper position. You have two options:

    Use a top-level filter and apply the filter only to the search results but not to the facets

    {
        "query" : {
            "multi_match" : {
                "query" : "this is a test",
                "fields" : [ "subject^2", "message" ]
            }
        },
        "filter" : {
            "term" : { "username": "slimkicker" }
        }
    } 
    

    Use a filtered query and apply the filter to both the search results and the facets

    {
        "query" : {
            "filtered" : {
                "query" : {
                    "multi_match" : {
                        "query" : "this is a test",
                        "fields" : [ "subject^2", "message" ]
                    }
                },
                "filter" : {
                    "term" : { "username": "slimkicker" }
                }
            }
        }
    }
    

提交回复
热议问题