问题描述
我在Elasticsearch中编写了以下lucene查询,以获取提到的具有ID字段的文档:
GET requirements_v3 / _search
{
from:0,
size:10,
query:{
bool:{
filter: {
bool:{
should:[
{ match:{
Id: b8bf49a4-960b-4fa8-8c5f-a3fce4b4d07b
}},
{
match:{
Id: 048b7907-2b5a-438a-ace9-f1e1fd67ca69
}
},
{
match:{
Id: 3b385896-1207-4f6d-8ae9-f3ced84cf1fa
}
},
{
match:{
Id: 0aa1db52-c0fb-4bf6-9223-00edccc32703
}
},
{
match: {
Id: 8c399993-f273-4ee0-a1ab-3a85c6848113
}
},
{
match:{
id: 4461eb37-487e-4899-a7be-914640fab0e0
}
},
{
ma tch:{
Id: 07052261-b904-4bfc-a6fd-3acd28114c6a
}
},
{
match:{
Id: 95816ff0-9eae-4196-99fc-86c6f43395fd
}
},
{
match:{
Id: ea8a59a6-2b2f-467a-9beb-e281b1581a0a
}
},
{
match:{
Id: 33f87d98-024f-4893 -aa1c-8d438a98cd1f
}
}
]
}
}
}
}
以上查询的响应为:
{
接收:14,
timed_out:假,
_shards:{
总计:5,
成功:5 ,
已跳过:0,
失败:0
},
点击数:{
总计:18,
max_score:0,
hits:[
{
_index: requirements_v3,
_type: _doc,
_id : 9d8060da-c3e2-4f6d-b4e2-17e65b266c76,
_score:0,
_source:{
Id: 9d8060da- c3e2-4f6d-b4e2-17e65b266c76,
名称:创建扩展/有限保修配置
}
},
{
_index: Requirements_v3,
_type: _doc,
_id: 4461eb37-487e-4899-a7be-914640fab0e0,
_score:0,
_source:{
Id: 4461eb37-487e-4899-a7be-914640fab0e0,
Name:创建扩展/有限保修配置
}
} ,
{
_index: requirements_v3,
_type: _doc,
_id: 33f87d98-024f-4893-aa1c-8d438a98cd1f,
_score:0,
_source:{
Id: 33f87d98-024f-4893-aa1c-8d438a98cd1f,
Name:创建配置器
}
},
{
_index: requirements_v3,
_type: _doc,
_id: d75d9a7c-e145-487e-922f-102c16d0026f,
_score:0,
_source:{
Id: d75d9a7c-e145-487e-922f-102c16d0026f,
名称:创建配置器
}
},
{
_index: requirements_v3,
_type: _ doc,
_id: 007eadb7 -adda-487e-b7fe-6f6b5648de2e,
_score:0,
_source:{
Id: 007eadb7-adda-487e-b7fe-6f6b5648de2e,
名称:详细信息页-构建
}
},
{
_index: requirements_v3,
_type: _doc,
_id: 95816ff0-9eae-4196-99fc-86c6f43395fd,
_score:0,
_source:{
Id: 95816ff0-9eae-4196-99fc-86c6f43395fd,
Name:创建扩展/有限保修配置
}
},
{
_index : requirements_v3,
_type: _doc,
_id: 07052261-b904-4bfc-a6fd-3acd28114c6a,
_score:0,
_source:{
Id: 07052261-b904-4bfc-a6fd-3acd28114c6a,
Name: HUC
}
},
{
_index: requirements_v3,
_type: _doc,
_id: d 60daf3a-4681-4bfc-a3a9-b04b5b005f73,
_score:0,
_source:{
Id: d60daf3a-4681-4bfc-a3a9-b04b5b005f73,
名称: DAMS UpsertUnenrollPrice}
},
{
_index: requirements_v3,
_type: _doc,
_id: c1b367f2-a57a-487e-994c-84470e0f9db4,
_score:0,
_source:{
Id: c1b367f2-a57a- 487e-994c-84470e0f9db4,
名称:项目设置
}
},
{
_index: requirements_v3,
_type: _doc,
_id: b8bf49a4-960b-4fa8-8c5f-a3fce4b4d07b,
_score:0,
_source:{
Id: b8bf49a4-960b-4fa8-8c5f-a3fce4b4d07b,
名称:分期付款
}
}
]
}
}
这里提到的totalHits为'18'。为什么返回的项目多于10?我认为匹配查询应用于完全匹配,为什么还要在此处返回更多文档?
PS:我知道我可以使用Ids查询来实现,但是我想知道为什么这不会返回正确的响应
更新:将大小设置为20会返回以下响应:
{
took:195,
timed_out:false,
_shards:{
total :5,5,
成功:5,
跳过:0,
失败:0
},
点击:{
总计:18,
最大得分:0,
点击数:[
{
_index: requirements_v3,
_type : _doc,
_id: 9d8060da-c3e2-4f6d-b4e2-17e65b266c76,
_score:0,
_source:{
Id : 9d8060da-c3e2-4f6d-b4e2-17e65b266c76,
名称:创建扩展/有限保修配置
}
},
{
_index: requirements_v3,
_type: _ doc,
_id: 4461eb37-487e-4899-a7be -914640fab0e0,
_score:0,
_source:{
Id: 4461eb37-487e-4899-a7be-914640fab0e0,
名称 :创建扩展/有限保修配置
}
},
{
_index: requirements_v3,
_type: _doc,
_id: 33f87d98-024f-4893-aa1c-8d438a98cd1f,
_score:0,
_source:{
Id: 33f87d98-024f -4893-aa1c-8d438a98cd1f,
名称:创建配置器
}
},
{
_index: requirements_v3,
_type: _doc,
_id: d75d9a7c-e145-487e-922f-102c16d0026f,
_score:0,
_source:{
Id: d75d9a7c-e145-487e-922f-102c16d0026f,
名称:创建配置器
}
},
{
_index: requirements_v3,
_type: _ doc,
_id: 007eadb7-adda-487e-b7fe-6f6b5648de2e,
_score:0 ,
_source:{
Id: 007eadb7-adda-487e- b7fe-6f6b5648de2e,
名称:详细信息页-构建
}
},
{
_index: requirements_v3,
_type: _doc,
_id: 95816ff0-9eae-4196-99fc-86c6f43395fd,
_score:0,
_source:{
Id: 95816ff0-9eae-4196-99fc-86c6f43395fd,
名称:创建扩展/有限保修配置
}
},
{
_index: requirements_v3,
_type: _ doc,
_id: 07052261-b904-4bfc-a6fd-3acd28114c6a,
_score :0,
_source:{
Id: 07052261-b904-4bfc-a6fd-3acd28114c6a,
名称: HUC
}
},
{
_index: requirements_v3,
_type: _doc,
_id: d60daf3a-4681-4bfc-a3a9 -b04b5b005f73,
_score:0,
_source:{
Id: d60daf3a-4681-4bfc-a3a9-b04b5b005f73,
名称 : DAMS UpsertUnenrollPrice
}
},
{
_index : requirements_v3,
_type: _doc,
_id: c1b367f2-a57a-487e-994c-84470e0f9db4,
_score:0,
_source:{
Id: c1b367f2-a57a-487e-994c-84470e0f9db4,
Name:项目设置
}
},
{
_index: requirements_v3,
_type: _doc,
_id: b8bf49a4-960b-4fa8-8c5f-a3fce4b4d07b,
_score:0,
_source:{
Id: b8bf49a4-960b-4fa8-8c5f-a3fce4b4d07b,
名称:安装
}
},
{
_index: requirements_v3,
_type: _ doc,
_id: b9437079-47c4 -487e-abf0-1ff076f69e0f,
_score:0,
_source:{
Id: b9437079-47c4-487e-abf0-1ff076f69e0f,
名称:详细信息页面-字符串
}
},
{
_index: requirements_v3,
_type: _doc ,
_id: 0aa1db52-c0fb-4bf6-9223-00edccc32703,
_score:0,
_s ource:{
Id: 0aa1db52-c0fb-4bf6-9223-00edccc32703,
Name:创建扩展/有限保修配置
}
} ,
{
_index: requirements_v3,
_type: _doc,
_id: ea8a59a6-2b2f-467a-9beb-e281b1581a0a,
_score:0,
_source:{
Id: ea8a59a6-2b2f-467a-9beb-e281b1581a0a,
Name:创建配置器
}
},
{
_index: requirements_v3,
_type: _doc,
_id: fd259359-4f6d-4530-ac29-fcebe00d66a6,
_score:0,
_source:{
Id: fd259359-4f6d-4530-ac29-fcebe00d66a6,
名称:邀请平台
}
},
{
_index: requirements_v3,
_type: _doc ,
_id: 1b2ba0bb-3e7f-46fb-b904-07460b84848b,
_score:0,
_source:{
Id: 1b2ba0bb-3e7f-46fb-b904-07460b84848b,
名称:训练
}
},
{
_index: requirements_v3,
_type: _doc,
_id: 8c399993-f273-4ee0 -a1ab-3a85c6848113,
_score:0,
_source:{
Id: 8c399993-f273-4ee0-a1ab-3a85c6848113,
名称:配置要报告的ASIN
}
},
{
_index: requirements_v3,
_type: _ doc,
_id: 3b385896-1207-4f6d-8ae9-f3ced84cf1fa,
_score:0,
_source:{
Id: 3b385896-1207 -4f6d-8ae9-f3ced84cf1fa,
名称:创建扩展/有限保修配置
}
},
{
_index: requirements_v3 ,
_type: _doc,
_id: 048b7907-2b5a-438a-ace9-f1e1fd67ca69,
_score:0,
_source :{
Id: 048b7907-2b5a-438a-ace9-f1e1fd67ca69,
名称:邀请平台
}
}
]
}
}
让我们通过以下映射了解这一点,例如:
{
_doc: {
properties:{
Id:{
type: text,
fields:{
keyword:{
type:关键字,
ignore_above:256
}
}
},
Name:{
type: text,
fields:{
keyword:{
type: keyword,
ignore_above:256
}
}
}
}
}
}
上面的映射是由elasticsearch动态创建的。现在让我们集中讨论 Id
字段。其类型为文本
。默认情况下,文本
数据类型的分析器
是标准
分析器。当将此分析器应用于此字段的输入时,它将被标记为术语。因此,例如,如果您输入 Id
的值是 33f87d98-024f-4893-aa1c-8d438a98cd1f
,则会生成以下令牌:
33f87d98
024f
4893
aa1c
8d438a98cd1f
您可以看到输入值被使用的-
分割作为分隔符。这是因为是
Id
下还有另一个子字段,它是关键字
,其类型为关键字
。对于类型关键字
的输入,将直接对其进行索引,而无需进行任何修改。
现在让我们了解为什么会得到更多文档匹配,结果计数超出预期。在查询中,您对 Id
字段使用了 match
查询,如下所示:
{
match:{
Id: b8bf49a4-960b-4fa8-8c5f-a3fce4b4d07b
}
}
默认情况下,匹配查询使用与映射中的字段相同的分析器。因此,再次在查询中的 Id
值上应用相同的分析器,并以与上述类似的方式将输入拆分为令牌。在匹配查询输入字符串的标记之间应用的默认运算符为OR,因此查询实际上变为:
b8bf49a4 OR 960b OR 4fa8 OR 8c5f或a3fce4b4d07b
如果上述任何令牌与存储在 Id $中的任何索引词匹配, c $ c>字段,则该文档被视为匹配项。
基于以上映射的以上解决方案:
改为使用关键字字段。因此查询变为:
{
match:{
Id.keyword: b8bf49a4-960b-4fa8-8c5f-a3fce4b4d07b
}
}
更多有关匹配方式的信息 也正如@Curious_MInd在其回答中提到的,最好使用 I have written the following lucene query in elasticsearch for getting documents with Id field as mentioned: The response for the above query is: This mentions totalHits as '18'. Why is it returning more items than 10? I believe match query should be used for 'exact' matches, so why more documents are returned here? P.S.: I know I can use the Ids query for this, but I want to know why is this not returning the correct response Update: Setting the size to 20 returns the following response: Lets understand this by the following mapping e.g: The above mapping is created dynamically by elasticsearch. Lets us now focus on As you can see the input value is splitted by There is another sub-field under Now lets understand why more documents get matched and result count is more than expected. In your query you used By default match query uses the same analyzer that is applied on the field in mapping. So on the There if any of the above tokens match to any of the indexed terms stored in Solution for the above based on above mapping: Use the keyword field instead. So the query becomes: More on how match works see here. Also as mention by @Curious_MInd in his answer its better to use 这篇关于“匹配”查询以及“应该”子句在Elasticsearch中提供了超出要求的匹配结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
条款
,而不应该在中使用多个
匹配
。GET requirements_v3/_search
{
"from": 0,
"size": 10,
"query": {
"bool": {
"filter": {
"bool": {
"should": [
{"match": {
"Id": "b8bf49a4-960b-4fa8-8c5f-a3fce4b4d07b"
}},
{
"match": {
"Id": "048b7907-2b5a-438a-ace9-f1e1fd67ca69"
}
},
{
"match": {
"Id": "3b385896-1207-4f6d-8ae9-f3ced84cf1fa"
}
},
{
"match": {
"Id": "0aa1db52-c0fb-4bf6-9223-00edccc32703"
}
},
{
"match": {
"Id": "8c399993-f273-4ee0-a1ab-3a85c6848113"
}
},
{
"match": {
"Id": "4461eb37-487e-4899-a7be-914640fab0e0"
}
},
{
"match": {
"Id": "07052261-b904-4bfc-a6fd-3acd28114c6a"
}
},
{
"match": {
"Id": "95816ff0-9eae-4196-99fc-86c6f43395fd"
}
},
{
"match": {
"Id": "ea8a59a6-2b2f-467a-9beb-e281b1581a0a"
}
},
{
"match": {
"Id": "33f87d98-024f-4893-aa1c-8d438a98cd1f"
}
}
]
}
}
}
}
{
"took": 14,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 18,
"max_score": 0,
"hits": [
{
"_index": "requirements_v3",
"_type": "_doc",
"_id": "9d8060da-c3e2-4f6d-b4e2-17e65b266c76",
"_score": 0,
"_source": {
"Id": "9d8060da-c3e2-4f6d-b4e2-17e65b266c76",
"Name": "Create Extended/Limited Warranty Configuration"
}
},
{
"_index": "requirements_v3",
"_type": "_doc",
"_id": "4461eb37-487e-4899-a7be-914640fab0e0",
"_score": 0,
"_source": {
"Id": "4461eb37-487e-4899-a7be-914640fab0e0",
"Name": "Create Extended/Limited Warranty Configuration"
}
},
{
"_index": "requirements_v3",
"_type": "_doc",
"_id": "33f87d98-024f-4893-aa1c-8d438a98cd1f",
"_score": 0,
"_source": {
"Id": "33f87d98-024f-4893-aa1c-8d438a98cd1f",
"Name": "Create Configurator"
}
},
{
"_index": "requirements_v3",
"_type": "_doc",
"_id": "d75d9a7c-e145-487e-922f-102c16d0026f",
"_score": 0,
"_source": {
"Id": "d75d9a7c-e145-487e-922f-102c16d0026f",
"Name": "Create Configurator"
}
},
{
"_index": "requirements_v3",
"_type": "_doc",
"_id": "007eadb7-adda-487e-b7fe-6f6b5648de2e",
"_score": 0,
"_source": {
"Id": "007eadb7-adda-487e-b7fe-6f6b5648de2e",
"Name": "Detail Page - Build"
}
},
{
"_index": "requirements_v3",
"_type": "_doc",
"_id": "95816ff0-9eae-4196-99fc-86c6f43395fd",
"_score": 0,
"_source": {
"Id": "95816ff0-9eae-4196-99fc-86c6f43395fd",
"Name": "Create Extended/Limited Warranty Configuration"
}
},
{
"_index": "requirements_v3",
"_type": "_doc",
"_id": "07052261-b904-4bfc-a6fd-3acd28114c6a",
"_score": 0,
"_source": {
"Id": "07052261-b904-4bfc-a6fd-3acd28114c6a",
"Name": "HUC"
}
},
{
"_index": "requirements_v3",
"_type": "_doc",
"_id": "d60daf3a-4681-4bfc-a3a9-b04b5b005f73",
"_score": 0,
"_source": {
"Id": "d60daf3a-4681-4bfc-a3a9-b04b5b005f73",
"Name": "DAMS UpsertUnenrollPrice" }
},
{
"_index": "requirements_v3",
"_type": "_doc",
"_id": "c1b367f2-a57a-487e-994c-84470e0f9db4",
"_score": 0,
"_source": {
"Id": "c1b367f2-a57a-487e-994c-84470e0f9db4",
"Name": "Item Setup"
}
},
{
"_index": "requirements_v3",
"_type": "_doc",
"_id": "b8bf49a4-960b-4fa8-8c5f-a3fce4b4d07b",
"_score": 0,
"_source": {
"Id": "b8bf49a4-960b-4fa8-8c5f-a3fce4b4d07b",
"Name": "Installments"
}
}
]
}
}
{
"took": 195,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 18,
"max_score": 0,
"hits": [
{
"_index": "requirements_v3",
"_type": "_doc",
"_id": "9d8060da-c3e2-4f6d-b4e2-17e65b266c76",
"_score": 0,
"_source": {
"Id": "9d8060da-c3e2-4f6d-b4e2-17e65b266c76",
"Name": "Create Extended/Limited Warranty Configuration"
}
},
{
"_index": "requirements_v3",
"_type": "_doc",
"_id": "4461eb37-487e-4899-a7be-914640fab0e0",
"_score": 0,
"_source": {
"Id": "4461eb37-487e-4899-a7be-914640fab0e0",
"Name": "Create Extended/Limited Warranty Configuration"
}
},
{
"_index": "requirements_v3",
"_type": "_doc",
"_id": "33f87d98-024f-4893-aa1c-8d438a98cd1f",
"_score": 0,
"_source": {
"Id": "33f87d98-024f-4893-aa1c-8d438a98cd1f",
"Name": "Create Configurator"
}
},
{
"_index": "requirements_v3",
"_type": "_doc",
"_id": "d75d9a7c-e145-487e-922f-102c16d0026f",
"_score": 0,
"_source": {
"Id": "d75d9a7c-e145-487e-922f-102c16d0026f",
"Name": "Create Configurator"
}
},
{
"_index": "requirements_v3",
"_type": "_doc",
"_id": "007eadb7-adda-487e-b7fe-6f6b5648de2e",
"_score": 0,
"_source": {
"Id": "007eadb7-adda-487e-b7fe-6f6b5648de2e",
"Name": "Detail Page - Build"
}
},
{
"_index": "requirements_v3",
"_type": "_doc",
"_id": "95816ff0-9eae-4196-99fc-86c6f43395fd",
"_score": 0,
"_source": {
"Id": "95816ff0-9eae-4196-99fc-86c6f43395fd",
"Name": "Create Extended/Limited Warranty Configuration"
}
},
{
"_index": "requirements_v3",
"_type": "_doc",
"_id": "07052261-b904-4bfc-a6fd-3acd28114c6a",
"_score": 0,
"_source": {
"Id": "07052261-b904-4bfc-a6fd-3acd28114c6a",
"Name": "HUC"
}
},
{
"_index": "requirements_v3",
"_type": "_doc",
"_id": "d60daf3a-4681-4bfc-a3a9-b04b5b005f73",
"_score": 0,
"_source": {
"Id": "d60daf3a-4681-4bfc-a3a9-b04b5b005f73",
"Name": "DAMS UpsertUnenrollPrice"
}
},
{
"_index": "requirements_v3",
"_type": "_doc",
"_id": "c1b367f2-a57a-487e-994c-84470e0f9db4",
"_score": 0,
"_source": {
"Id": "c1b367f2-a57a-487e-994c-84470e0f9db4",
"Name": "Item Setup"
}
},
{
"_index": "requirements_v3",
"_type": "_doc",
"_id": "b8bf49a4-960b-4fa8-8c5f-a3fce4b4d07b",
"_score": 0,
"_source": {
"Id": "b8bf49a4-960b-4fa8-8c5f-a3fce4b4d07b",
"Name": "Installments"
}
},
{
"_index": "requirements_v3",
"_type": "_doc",
"_id": "b9437079-47c4-487e-abf0-1ff076f69e0f",
"_score": 0,
"_source": {
"Id": "b9437079-47c4-487e-abf0-1ff076f69e0f",
"Name": "Detail Page - Strings "
}
},
{
"_index": "requirements_v3",
"_type": "_doc",
"_id": "0aa1db52-c0fb-4bf6-9223-00edccc32703",
"_score": 0,
"_source": {
"Id": "0aa1db52-c0fb-4bf6-9223-00edccc32703",
"Name": "Create Extended/Limited Warranty Configuration"
}
},
{
"_index": "requirements_v3",
"_type": "_doc",
"_id": "ea8a59a6-2b2f-467a-9beb-e281b1581a0a",
"_score": 0,
"_source": {
"Id": "ea8a59a6-2b2f-467a-9beb-e281b1581a0a",
"Name": "Create Configurator"
}
},
{
"_index": "requirements_v3",
"_type": "_doc",
"_id": "fd259359-4f6d-4530-ac29-fcebe00d66a6",
"_score": 0,
"_source": {
"Id": "fd259359-4f6d-4530-ac29-fcebe00d66a6",
"Name": "Invite Platform"
}
},
{
"_index": "requirements_v3",
"_type": "_doc",
"_id": "1b2ba0bb-3e7f-46fb-b904-07460b84848b",
"_score": 0,
"_source": {
"Id": "1b2ba0bb-3e7f-46fb-b904-07460b84848b",
"Name": "Training"
}
},
{
"_index": "requirements_v3",
"_type": "_doc",
"_id": "8c399993-f273-4ee0-a1ab-3a85c6848113",
"_score": 0,
"_source": {
"Id": "8c399993-f273-4ee0-a1ab-3a85c6848113",
"Name": "Configure ASIN for Reporting"
}
},
{
"_index": "requirements_v3",
"_type": "_doc",
"_id": "3b385896-1207-4f6d-8ae9-f3ced84cf1fa",
"_score": 0,
"_source": {
"Id": "3b385896-1207-4f6d-8ae9-f3ced84cf1fa",
"Name": "Create Extended/Limited Warranty Configuration"
}
},
{
"_index": "requirements_v3",
"_type": "_doc",
"_id": "048b7907-2b5a-438a-ace9-f1e1fd67ca69",
"_score": 0,
"_source": {
"Id": "048b7907-2b5a-438a-ace9-f1e1fd67ca69",
"Name": "Invite Platform"
}
}
]
}
}
{
"_doc": {
"properties": {
"Id": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"Name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
Id
field. Its type is text
. By default the analyzer
for text
datatype is standard
analyzer. When this analyzer is applied on the input for this field it get tokenized into terms. So for example if you input value for Id
is 33f87d98-024f-4893-aa1c-8d438a98cd1f
following tokens get generated:33f87d98
024f
4893
aa1c
8d438a98cd1f
-
being used as delimiter. This is because standard analyzer is applied on it.Id
which is keyword
and its type is keyword
. For type keyword
the input is indexed as it is without applying any modification.match
query on Id
field as below:{
"match": {
"Id": "b8bf49a4-960b-4fa8-8c5f-a3fce4b4d07b"
}
}
Id
value in the query again the same analyzer is applied and the input is splitted into tokens in a similar way as above. The default operator that is applied between tokens of match query input string is OR and hence your query actually becomes:b8bf49a4 OR 960b OR 4fa8 OR 8c5f OR a3fce4b4d07b
Id
field, the document is considered a match.{
"match": {
"Id.keyword": "b8bf49a4-960b-4fa8-8c5f-a3fce4b4d07b"
}
}
terms
than using multiple match
in should
.