你可以的,加油

数组查询完全匹配问题

Elasticsearch | 作者 fengzxia | 发布于2018年09月06日 | 阅读数:4209

数据格式如下
{
"_index" : "xxx",
"_type" : "xxx",
"_id" : "123",
"_version" : 1,
"_score" : 1,
"_source" : {
"iD" : 123,
"name" : "xxx",
"tags" : [
"文字",
"恋爱",
"冒险"
],
"keywords" : []
}
}
在查询时搜索tags,如果我查询条件是“文字”我想只查询出tags:["文字"],而tags:["文字","冒险"],tags:["文字","冒险","恋爱"]这些数据不查询出来,请教下各位大佬,我应该怎么去查询,或者说我应该把数据怎么存放才能实现我的这种需求,求指导思路
已邀请:

rochy - rochy_he

赞同来自:

如果以现在的数据结构想查询出来,可以借助自定义评分;
你可以看一下思路:_score * (ctx._source['tags'].lenth > 1 ? 0 : 1)
 
这样就可以使得 tags 数组个数大于 1 的得分为 0,
然后你在 query 里面使用 min_score 就可以把这部分数据过滤掉了

heeexy

赞同来自:

"filter": [
{
"script": {
"script": "doc['tags'].size() ==2"
}
}
]

laoyang360 - 《一本书讲透Elasticsearch》作者,Elastic认证工程师 [死磕Elasitcsearch]知识星球地址:http://t.cn/RmwM3N9;微信公众号:铭毅天下; 博客:https://elastic.blog.csdn.net

赞同来自:

//同楼上,以下demo需要kibana调试一下
{
"filter": [
{
"script": {
"script": "doc['tags'].size() ==1"
}
},
{
"terms":{
"tags":"文字"
}}
]

要回复问题请先登录注册