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",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"text2": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"text3": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"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",
"_type": "_doc",
"_id": "3",
"_score": 0.50632054,
"_source": {
"name": "b",
"user": "john doe is great"
}
}
]
推荐阅读
- mysql - 在 3 个表中使用匹配优化查询。mysql
- javascript - 如何使用 javascript 将 ["abc","efg"] 数组转换为 "["abc","efg"]" (字符串)?
- python - pandas:仅当另一列中的值匹配时才计算行之间的重叠单词(多个实例的问题)
- javascript - 我可以摆脱画布
- javascript - 在函数开始运行之前设置计时器
- amazon-web-services - 是否可以在不使用 lambda 函数的情况下为 cloudformation 中的 S3 对象创建时间戳前缀?
- php - 用itunes解析xml标签:来自url的东西
- spring - Spring JPA - 在 JpaRepository 或 CrudRepository 中使用本机查询插入带有 batch_size 的列表
- python - 如何将一个字符串解析为两个字符串
- node.js - 如何从nodejs调用SIP?