微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Elasticsearch 多匹配所有字段上的精确短语

如何解决Elasticsearch 多匹配所有字段上的精确短语

我们目前有一个这样的查询

                    "query": {
                        "multi_match": {
                            "query": "john doe",analyzer: "whitespace",type: "most_fields",}
                    }

基本上,这会在文档的所有字段中搜索“john”或“doe”。

我们如何才能在所有字段中准确搜索“john doe”?

这是我目前能做的最好的:

                    "query": {
                        "multi_match": {
                            "query": "john doe","type": "phrase",}
                    }

不幸的是,这会在一个字段中搜索“john”和“doe”。它不会在字段中搜索确切的短语“john doe”。

除了将所有字段复制到一个包含所有值的大字段中之外,还有没有其他方法可以做到这一点?然后在该字段上运行标准 match_phrase 查询

以下是带有虚拟字段名称的索引示例:

{
    "my_index": {
        "mappings": {
            "properties": {
                "@timestamp": {
                    "type": "date"
                },"@version": {
                    "type": "text","fields": {
                        "keyword": {
                            "type": "keyword","ignore_above": 256
                        }
                    }
                },"date1": {
                    "type": "date"
                },"date2": {
                    "type": "date"
                },"text1": {
                    "type": "text","text2": {
                    "type": "text","text3": {
                    "type": "text","ignore_above": 256
                        }
                    }
                }
            }
        }

    }
}

样本数据(真实数据是超标准的扁平化字符串)

{
    "_index": "myIndex","_type": "_doc","_id": "013345","_score": 7.414526,"_source": {
        "date1": "2017-05220T03:59:59.000Z","text1": "Available ","text2": "Hello i am john doe","text3": "Ministry","text4": "Decision",}
}

提前致谢。

解决方法

添加一个工作示例

索引数据:

{
  "name": "b","user": "john doe is great"
}
{
  "name": "john doe","user": "a"
}
{
  "name": "b","user": "john is doe great"
}

搜索查询:

此查询在所有字段中搜索准确的短语 "john doe"

{
  "query": {
    "multi_match": {
      "query": "john doe","type": "phrase"
    }
  }
}

搜索结果:

"hits": [
      {
        "_index": "66129712","_type": "_doc","_id": "1","_score": 2.1784627,"_source": {
          "name": "john doe","user": "a"
        }
      },{
        "_index": "66129712","_id": "3","_score": 0.50632054,"_source": {
          "name": "b","user": "john doe is great"
        }
      }
    ]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。