ElasticSearch, multi-match with filter?

后端 未结 4 344
情深已故
情深已故 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" }
                }
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-30 09:15

    Try this:

    [
            "index" => 'shop_1', //index_name,
            "type" => 'shop', //type_name,
            "from" => 0, //offset
            "size" => 30, //limit
            "body" => [
                "query" => [
                    "bool" => [
                        "must" =>   [
                                        "match" => [
                                            "category" => 'men' // results must contain 'men' in category field
                                        ]
                                    ]
                    ]
                ],
                "_source" => [ // fields to retrieve
                                "id",
                                "product_id",
                                "item_title",
                                "item_slug",
                                "item_sku"
                            ],
                "sort" => [
                    [
                        "_score" => [
                            "order" => "desc" // order by score desc
                        ]
                    ]
                ]
    
          ],
    ];
    
    0 讨论(0)
  • 2021-01-30 09:23

    With Elasticsearch 5 the syntax has changed to the usage of bool query, e.g.

    {
      "from" : 0,
      "size" : 10,
      "sort" : "publishDate",
      "query": {
        "bool": {  
          "must" : {
            "multi_match" : {
              "query":      "wedding",
              "type":       "most_fields",
              "fields":     [ "title", "text" ]
            }
          },
          "filter": {
            "term": {
              "locale": "english"
            }
          }
        }
      }
    }
    

    Documentation can be found here.

    0 讨论(0)
  • 2021-01-30 09:33

    As per the new Documentation of Elasticsearch, the format is little bit changed, now you have to use bool and must and can apply filter separately from query like follows,

    {
        'index' : 'users',
            'type' : 'users',
            'body' : {
              "query" : {
                "bool" : {
                  "must" : {
                    'multi_match' : {
                        'fields' : {'source^1', 'first_name^5', 'last_name^4', 'email^3', 'postcode^2', 'telephone', 'address', 'alternate_address'
                        },
                        'query' : 'Shahrukh Anwar',
                    },
                  },
                  "filter" : {
                    "term" : {
                      'assigned_to' : 125
                    }
                  }
                }
              }
            }
    }
    
    0 讨论(0)
提交回复
热议问题