字词查询没有return任何结果
Term query does not return any results
我正在尝试使用嵌套术语查询在 Elastic 搜索中查找一些实体。如果我执行搜索查询,则没有任何结果。但是,如果我将查询更改为使用 "match" 而不是 "term",我将获得命中。我做错了什么?
我想要实现的是只返回完全匹配。
ES中实体如下
"vehiclesCollection": [
{
"id": "c0163692-69c5-442e-a30c-a3789384904d",
"additionalFields": {
"23698e0d-e5ba-4c0e-9c41-09db40708f09@11b75c17-1f62-4a45-847b- 348f3e6a5485@1a61281d-73b0-41ea-964d-d869a172752d": "HaveNoIdea",
"23698e0d-e5ba-4c0e-9c41-09db40708f09@11b75c17-1f62-4a45-847b-348f3e6a5485@91b5e07e-6e09-4a81-a3ef-d43f1ea99b34": "Strane",
"23698e0d-e5ba-4c0e-9c41-09db40708f09@11b75c17-1f62-4a45-847b-348f3e6a5485@4fc4d50b-5c81-47f7-8c7a-7802c28c0dca": true,
"23698e0d-e5ba-4c0e-9c41-09db40708f09@11b75c17-1f62-4a45-847b-348f3e6a5485@c35b1e06-f918-4f70-a323-804c004ddcbe": "License Plate"
},
"searchableAdditionalFieldValues": [
"License Plate",
"Strane",
"HaveNoIdea"
]
}
]
提示:这只是我执行搜索的部分。
它有以下映射
{
"vehiclesCollection": {
"type": "nested",
"properties": {
"additionalFields": {
"properties": {
"23698e0d-e5ba-4c0e-9c41-09db40708f09@11b75c17-1f62-4a45-847b-348f3e6a5485@1a61281d-73b0-41ea-964d-d869a172752d": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"23698e0d-e5ba-4c0e-9c41-09db40708f09@11b75c17-1f62-4a45-847b-348f3e6a5485@4fc4d50b-5c81-47f7-8c7a-7802c28c0dca": {
"type": "boolean"
},
"23698e0d-e5ba-4c0e-9c41-09db40708f09@11b75c17-1f62-4a45-847b-348f3e6a5485@91b5e07e-6e09-4a81-a3ef-d43f1ea99b34": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"23698e0d-e5ba-4c0e-9c41-09db40708f09@11b75c17-1f62-4a45-847b-348f3e6a5485@c35b1e06-f918-4f70-a323-804c004ddcbe": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"id": {
"type": "keyword"
},
"searchableAdditionalFieldValues": {
"type": "text",
"index_options": "offsets",
"analyzer": "ngram_tokenizer_analyzer",
"search_analyzer": "whitespace_analyzer"
}
}
}
}
和搜索查询(如果将术语与匹配切换,它将起作用,但我只想要完全匹配)
{
"bool": {
"must": [
{
"nested": {
"query": {
"term": {
"vehiclesCollection.additionalFields.23698e0d-e5ba-4c0e-9c41-09db40708f09@11b75c17-1f62-4a45-847b-348f3e6a5485@c35b1e06-f918-4f70-a323-804c004ddcbe": {
"value": "License Plate",
"boost": 1
}
}
},
"path": "vehiclesCollection",
"ignore_unmapped": false,
"score_mode": "avg",
"boost": 1
}
},
{
"nested": {
"query": {
"term": {
"vehiclesCollection.additionalFields.23698e0d-e5ba-4c0e-9c41-09db40708f09@11b75c17-1f62-4a45-847b-348f3e6a5485@1a61281d-73b0-41ea-964d-d869a172752d": {
"value": "HaveNoIdea",
"boost": 1
}
}
},
"path": "vehiclesCollection",
"ignore_unmapped": false,
"score_mode": "avg",
"boost": 1
}
},
{
"nested": {
"query": {
"term": {
"vehiclesCollection.additionalFields.23698e0d-e5ba-4c0e-9c41-09db40708f09@11b75c17-1f62-4a45-847b-348f3e6a5485@4fc4d50b-5c81-47f7-8c7a-7802c28c0dca": {
"value": true,
"boost": 1
}
}
},
"path": "vehiclesCollection",
"ignore_unmapped": false,
"score_mode": "avg",
"boost": 1
}
}
]
}
}
我的第一个想法是它与搜索分析有关。但我不确定如何为这个特定查询设置它们。我正在使用 Elasticsearch Java API.
根据经验,您需要
- 运行 您对
.keyword
子字段的 term
查询
- 运行 您在
text
顶级字段上的匹配查询
因此您的查询将是(前两个嵌套 term
查询使用 ***.keyword
子字段):
{
"bool": {
"must": [
{
"nested": {
"query": {
"term": {
"vehiclesCollection.additionalFields.23698e0d-e5ba-4c0e-9c41-09db40708f09@11b75c17-1f62-4a45-847b-348f3e6a5485@c35b1e06-f918-4f70-a323-804c004ddcbe.keyword": {
"value": "License Plate",
"boost": 1
}
}
},
"path": "vehiclesCollection",
"ignore_unmapped": false,
"score_mode": "avg",
"boost": 1
}
},
{
"nested": {
"query": {
"term": {
"vehiclesCollection.additionalFields.23698e0d-e5ba-4c0e-9c41-09db40708f09@11b75c17-1f62-4a45-847b-348f3e6a5485@1a61281d-73b0-41ea-964d-d869a172752d.keyword": {
"value": "HaveNoIdea",
"boost": 1
}
}
},
"path": "vehiclesCollection",
"ignore_unmapped": false,
"score_mode": "avg",
"boost": 1
}
},
{
"nested": {
"query": {
"term": {
"vehiclesCollection.additionalFields.23698e0d-e5ba-4c0e-9c41-09db40708f09@11b75c17-1f62-4a45-847b-348f3e6a5485@4fc4d50b-5c81-47f7-8c7a-7802c28c0dca": {
"value": true,
"boost": 1
}
}
},
"path": "vehiclesCollection",
"ignore_unmapped": false,
"score_mode": "avg",
"boost": 1
}
}
]
}
}
我正在尝试使用嵌套术语查询在 Elastic 搜索中查找一些实体。如果我执行搜索查询,则没有任何结果。但是,如果我将查询更改为使用 "match" 而不是 "term",我将获得命中。我做错了什么?
我想要实现的是只返回完全匹配。
ES中实体如下
"vehiclesCollection": [
{
"id": "c0163692-69c5-442e-a30c-a3789384904d",
"additionalFields": {
"23698e0d-e5ba-4c0e-9c41-09db40708f09@11b75c17-1f62-4a45-847b- 348f3e6a5485@1a61281d-73b0-41ea-964d-d869a172752d": "HaveNoIdea",
"23698e0d-e5ba-4c0e-9c41-09db40708f09@11b75c17-1f62-4a45-847b-348f3e6a5485@91b5e07e-6e09-4a81-a3ef-d43f1ea99b34": "Strane",
"23698e0d-e5ba-4c0e-9c41-09db40708f09@11b75c17-1f62-4a45-847b-348f3e6a5485@4fc4d50b-5c81-47f7-8c7a-7802c28c0dca": true,
"23698e0d-e5ba-4c0e-9c41-09db40708f09@11b75c17-1f62-4a45-847b-348f3e6a5485@c35b1e06-f918-4f70-a323-804c004ddcbe": "License Plate"
},
"searchableAdditionalFieldValues": [
"License Plate",
"Strane",
"HaveNoIdea"
]
}
]
提示:这只是我执行搜索的部分。
它有以下映射
{
"vehiclesCollection": {
"type": "nested",
"properties": {
"additionalFields": {
"properties": {
"23698e0d-e5ba-4c0e-9c41-09db40708f09@11b75c17-1f62-4a45-847b-348f3e6a5485@1a61281d-73b0-41ea-964d-d869a172752d": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"23698e0d-e5ba-4c0e-9c41-09db40708f09@11b75c17-1f62-4a45-847b-348f3e6a5485@4fc4d50b-5c81-47f7-8c7a-7802c28c0dca": {
"type": "boolean"
},
"23698e0d-e5ba-4c0e-9c41-09db40708f09@11b75c17-1f62-4a45-847b-348f3e6a5485@91b5e07e-6e09-4a81-a3ef-d43f1ea99b34": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"23698e0d-e5ba-4c0e-9c41-09db40708f09@11b75c17-1f62-4a45-847b-348f3e6a5485@c35b1e06-f918-4f70-a323-804c004ddcbe": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"id": {
"type": "keyword"
},
"searchableAdditionalFieldValues": {
"type": "text",
"index_options": "offsets",
"analyzer": "ngram_tokenizer_analyzer",
"search_analyzer": "whitespace_analyzer"
}
}
}
}
和搜索查询(如果将术语与匹配切换,它将起作用,但我只想要完全匹配)
{
"bool": {
"must": [
{
"nested": {
"query": {
"term": {
"vehiclesCollection.additionalFields.23698e0d-e5ba-4c0e-9c41-09db40708f09@11b75c17-1f62-4a45-847b-348f3e6a5485@c35b1e06-f918-4f70-a323-804c004ddcbe": {
"value": "License Plate",
"boost": 1
}
}
},
"path": "vehiclesCollection",
"ignore_unmapped": false,
"score_mode": "avg",
"boost": 1
}
},
{
"nested": {
"query": {
"term": {
"vehiclesCollection.additionalFields.23698e0d-e5ba-4c0e-9c41-09db40708f09@11b75c17-1f62-4a45-847b-348f3e6a5485@1a61281d-73b0-41ea-964d-d869a172752d": {
"value": "HaveNoIdea",
"boost": 1
}
}
},
"path": "vehiclesCollection",
"ignore_unmapped": false,
"score_mode": "avg",
"boost": 1
}
},
{
"nested": {
"query": {
"term": {
"vehiclesCollection.additionalFields.23698e0d-e5ba-4c0e-9c41-09db40708f09@11b75c17-1f62-4a45-847b-348f3e6a5485@4fc4d50b-5c81-47f7-8c7a-7802c28c0dca": {
"value": true,
"boost": 1
}
}
},
"path": "vehiclesCollection",
"ignore_unmapped": false,
"score_mode": "avg",
"boost": 1
}
}
]
}
}
我的第一个想法是它与搜索分析有关。但我不确定如何为这个特定查询设置它们。我正在使用 Elasticsearch Java API.
根据经验,您需要
- 运行 您对
.keyword
子字段的term
查询 - 运行 您在
text
顶级字段上的匹配查询
因此您的查询将是(前两个嵌套 term
查询使用 ***.keyword
子字段):
{
"bool": {
"must": [
{
"nested": {
"query": {
"term": {
"vehiclesCollection.additionalFields.23698e0d-e5ba-4c0e-9c41-09db40708f09@11b75c17-1f62-4a45-847b-348f3e6a5485@c35b1e06-f918-4f70-a323-804c004ddcbe.keyword": {
"value": "License Plate",
"boost": 1
}
}
},
"path": "vehiclesCollection",
"ignore_unmapped": false,
"score_mode": "avg",
"boost": 1
}
},
{
"nested": {
"query": {
"term": {
"vehiclesCollection.additionalFields.23698e0d-e5ba-4c0e-9c41-09db40708f09@11b75c17-1f62-4a45-847b-348f3e6a5485@1a61281d-73b0-41ea-964d-d869a172752d.keyword": {
"value": "HaveNoIdea",
"boost": 1
}
}
},
"path": "vehiclesCollection",
"ignore_unmapped": false,
"score_mode": "avg",
"boost": 1
}
},
{
"nested": {
"query": {
"term": {
"vehiclesCollection.additionalFields.23698e0d-e5ba-4c0e-9c41-09db40708f09@11b75c17-1f62-4a45-847b-348f3e6a5485@4fc4d50b-5c81-47f7-8c7a-7802c28c0dca": {
"value": true,
"boost": 1
}
}
},
"path": "vehiclesCollection",
"ignore_unmapped": false,
"score_mode": "avg",
"boost": 1
}
}
]
}
}