ElasticSearch with multi_match AND bool

时间:2015-01-08 08:19:00

标签: elasticsearch

我尝试学习Elasticsearch以将其添加到我的Rails应用程序中。 我想在2个字段中执行multi_match查询(好像它们只是一个字段),并且还要对另一个字段(状态)进行过滤,该字段必须等于1.

response = Wine.search({
            query: {
                multi_match: {
            query: "test",
            fields: ["winery", "name"]
            },
        bool: {
          must: {
            term: { status: 1 }
            },
          should: [],
          minimum_should_match: 1
        }
      }     
        })

错误是:

"fields\":[\"winery\",\"name\"]},\"bool\":{\"must\":{\"term\":{\"status\":1}},\"should\":[],\"minimum_should_match\":1}}}]]]; nested: ElasticsearchParseException[Expected field name but got START_OBJECT \"bool\"]; }]","status":400}

请求中有什么问题?如何一起执行multi_match和BOOL?

1 个答案:

答案 0 :(得分:21)

使用filtered query

{
    "query": {
        "filtered": {
            "query": {
                "multi_match": {
                    "query": "test",
                    "fields": [
                        "winery",
                        "name"
                    ]
                }
            },
            "filter": {
                "term": {
                    "status": "1"
                }
            }
        }
    }
}

Elasticsearch 5的相同查询:

{
    "query": {
        "bool": {
            "must": {
                "multi_match": {
                    "query": "test",
                    "fields": [
                        "winery",
                        "name"
                    ]
                }
            },
            "filter": {
                "term": {
                    "status": "1"
                }
            }
        }
    }
}