[英]ElasticSearch filter by terms issue
faced an interesting issue on filter by terms on field state
:在按字段state
上的术语过滤时遇到了一个有趣的问题:
if I will remove terms from filter block - all good, if I will add term\terms nothing will be found如果我要从过滤器块中删除术语 - 一切都好,如果我要添加 term\terms 什么都找不到
as Example作为例子
curl -X GET "http://elasticsearch:9200/_search?pretty" -H 'Content-Type: application/json' -u "elastic:pwd" -d'
{
"from": 0,
"size": 10000,
"sort": [
{
"campaign.priority": {
"order": "desc"
}
}
],
"query": {
"bool": {
"filter": [
{
"terms": {
"giftStatusId": [
10
]
}
}
],
"should": [
{
"query_string": {
"query": "*68561*"
}
},
{
"term": {
"giftId": "68561"
}
},
{
"term": {
"state":
"complete"
}
}
],
"minimum_should_match": 1
}
}
}'
{
"took" : 32,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "research",
"_id" : "190823",
"_score" : null,
"_source" : {
"id" : 190823,
"agent" : 1045,
"giftId" : 68561,
"sender" : {
"firstName" : "TeamOwner",
"lastName" : "Internal.alyce.gifts.GiftCampaignDetailsTests",
"email" : "1203110345eeJq_@test.comz"
},
"recipient" : {
"id" : 20220,
"firstName" : "Contact",
"lastName" : "lastName_lmfk",
"email" : "1203110416VTLR_@contactEmail.comz"
},
"campaign" : {
"id" : 3981,
"name" : "campaign_UhGw",
"priority" : 0,
"organizationId" : "1487",
"organizationName" : "Org_hABD",
"teamId" : 2785,
"teamName" : "1203110359bWht_Team"
},
"state" : "unclaimedAuto",
"createdAt" : "2020-12-03T11:06:26+00:00",
"updatedAt" : "2020-12-08T09:27:28+00:00",
"giftStatusId" : 10
},
"sort" : [
0
]
}
]
}
}
All good above, but below nothing found上面都很好,但下面什么也没找到
curl -X GET "http://elasticsearch:9200/_search?pretty" -H 'Content-Type: application/json' -u "elastic:pwd" -d'
{
"from": 0,
"size": 10000,
"sort": [
{
"campaign.priority": {
"order": "desc"
}
}
],
"query": {
"bool": {
"must": [
{
"terms": {
"giftStatusId": [
10
]
}
},
{
"terms": {
"state": [
"unclaimedAuto"
]
}
}
],
"should": [
{
"query_string": {
"query": "*68561*"
}
},
{
"term": {
"giftId": "68561"
}
}
],
"minimum_should_match": 1
}
}
}'
{
"took" : 29,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}
Does anyone faced same issue?有没有人面临同样的问题?
As @Val pointed out the state
field should be of keyword
type.正如@Val 指出的, state
字段应该是keyword
类型。
This is because if you are using terms query it returns documents that contain one or more exact terms in a provided field.这是因为如果您使用术语查询,它会返回在提供的字段中包含一个或多个确切术语的文档。 And, for term query it returns documents that contain an exact term in a provided field.并且,对于术语查询,它返回在提供的字段中包含确切术语的文档。 So, in both the queries, you need to have an exact match.因此,在这两个查询中,您都需要完全匹配。
So either you can change the data type of the state
field to keyword
type因此,您可以将state
字段的数据类型更改为keyword
类型
{
"mappings": {
"properties": {
"state": {
"type": "keyword"
}
}
}
}
OR if you have not explicitly defined any mapping, then you can also modify your search query like this:或者,如果您没有明确定义任何映射,那么您也可以像这样修改您的搜索查询:
{
"from": 0,
"size": 10000,
"sort": [
{
"campaign.priority": {
"order": "desc"
}
}
],
"query": {
"bool": {
"must": [
{
"terms": {
"giftStatusId": [
10
]
}
},
{
"terms": {
"state.keyword": [ ---> note this
"unclaimedAuto"
]
}
}
],
"should": [
{
"query_string": {
"query": "*68561*"
}
},
{
"term": {
"giftId": "68561"
}
}
],
"minimum_should_match": 1
}
}
}
This uses the keyword analyzer instead of the standard analyzer (notice the ".keyword" after state field)这使用关键字分析器而不是标准分析器(注意 state 字段后的“.keyword”)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.