49

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

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

What is the syntax for adding this filter?

I tried:

{
  "multi_match" => {
    "query" => "list",
    "fields" => [ "username" ]

  },
"filter" => {
        "term" => { "username" => "slimkicker"}
    }
}
4

4 回答 4

77

根据您的需要,您必须将过滤器放在正确的位置。你有两个选择:

使用顶级过滤器并将过滤器仅应用于搜索结果而不应用于构面

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

使用过滤查询并将过滤器应用于搜索结果和构面

{
    "query" : {
        "filtered" : {
            "query" : {
                "multi_match" : {
                    "query" : "this is a test",
                    "fields" : [ "subject^2", "message" ]
                }
            },
            "filter" : {
                "term" : { "username": "slimkicker" }
            }
        }
    }
}
于 2013-05-27T17:32:58.763 回答
66

在 Elasticsearch 5 中,语法已更改为使用bool 查询,例如

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

文档可以在这里找到。

于 2016-12-04T08:12:16.193 回答
8

根据 Elasticsearch 的新文档,格式略有改变,现在您必须使用bool并且must可以filter与查询分开应用,如下所示,

{
    '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
                }
              }
            }
          }
        }
}
于 2018-09-24T06:17:50.383 回答
0

尝试这个:

[
        "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
                    ]
                ]
            ]

      ],
];
于 2019-10-20T08:46:01.220 回答