elasticsearch - ElasticSearch 按术语问题过滤
问题描述
在按字段过滤时遇到了一个有趣的问题state
:
如果我要从过滤器块中删除条款 - 一切都好,如果我要添加 term\terms 什么都找不到
作为例子
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
]
}
]
}
}
上面都很好,但下面什么也没找到
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" : [ ]
}
}
有没有人面临同样的问题?
解决方案
正如@Val 指出的那样,该state
字段应该是keyword
类型。
这是因为如果您使用术语查询,它会返回在提供的字段中包含一个或多个确切术语的文档。并且,对于术语查询,它返回在提供的字段中包含确切术语的文档。因此,在这两个查询中,您都需要完全匹配。
因此,您可以将state
字段的数据类型更改为keyword
type
{
"mappings": {
"properties": {
"state": {
"type": "keyword"
}
}
}
}
或者,如果您没有明确定义任何映射,那么您也可以像这样修改您的搜索查询:
{
"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
}
}
}
这使用关键字分析器而不是标准分析器(注意状态字段后的“.keyword”)
推荐阅读
- ios - 用不同的字体大小对齐 UIButton 和 UILabel 文本
- java - 当我尝试 git push heroku 时出现错误
- javascript - 我可以为视频流创建 K6 负载测试吗
- linux - iraf, make: *** 没有规则使目标'sptable'。停止
- regex - 无法以正确的方式从某些 json 响应中解析出所有容器中的两个字段
- database - 查询没有主键的 Firestore
- html - 文件链接不可点击
- mysql - 使用 VBA 代码从 excel 连接到 MySQL 服务器
- django - Django 过滤列表和特定数据
- mysql - 如何连接三个表来查看sql中的数据?