【问题标题】:ElasticSearch with multi_match AND boolElasticSearch 与 multi_match AND bool
【发布时间】:2020-03-24 00:45:30
【问题描述】:

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

let 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 AND BOOL ?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

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

    【讨论】:

    • 完美答案!谢谢!
    • 不幸的是,答案对于 ElasticSearch 5.x 不再有效,更新的文档页面也没有多大帮助。您知道如何将 multi_match 查询转换为带有过滤器的新“bool”查询吗?
    • 我已经用一个可以与 Elasticserarch 5.x 一起使用的查询更新了答案。更多信息请查看 bool 查询文档:elastic.co/guide/en/elasticsearch/reference/current/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-31
    相关资源
    最近更新 更多