ElasticSearch,与过滤器多重匹配?

时间:2013-05-27 15:27:04

标签: elasticsearch

我在ES中有一个多匹配查询,并希望添加一个过滤器。

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

添加此过滤器的语法是什么?

我试过了:

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

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

4 个答案:

答案 0 :(得分:61)

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

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

{
    "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" }
            }
        }
    }
}

答案 1 :(得分:53)

使用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"
        }
      }
    }
  }
}

可以找到文档here

答案 2 :(得分:5)

根据Elasticsearch的新文档,格式几乎没有变化,现在您必须使用boolmust,并且可以像以下查询一样分别应用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
                }
              }
            }
          }
        }
}

答案 3 :(得分: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
                    ]
                ]
            ]

      ],
];