Q:非洲食人族的酋长吃什么?

elasticsearch能不能实现判断是否存在字段,然后再添加查询条件

Elasticsearch | 作者 dongkaihuahit | 发布于2020年09月14日 | 阅读数:7178

使用java的RestHighLevelClient。
问题是建立在这样的情况下: 
1、多个索引一个查询语句同时查询,结果进行排序; 
2、每个索引之间字段不完全相同,需要对不同索引添加不同的查询条件,希望能通过判断某索引的某field是否存在,然后针对field添加只作用于这个索引的查询条件(这个查询条件不影响查询其他没有这个字段的索引); 
 
索引之间不需要关联,只是想要一个多索引查询能针对每个索引添加个性查询条件
 
感谢大神们帮忙

 
已邀请:

pony_maggie - 公众号:犀牛饲养员的技术笔记

赞同来自: dongkaihuahit

可以实现的,组合多个bool查询。比如下面这个示例:
 
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"term": {
"query-field": {
"value": "query-term"
}
}
}
],
"filter": [
{
"terms": {
"_index": [
"index-1",
"index-2"
]
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"no-match-query-field": {
"value": "no-match-query-term"
}
}
}
],
"must_not": [
{
"terms": {
"_index": [
"index-1",
"index-2"
]
}
}
]
}
}
]
}
}

Hachiko

赞同来自:

兄弟,我也有类似场景,同求助,说一下:
 
文档中有如下两组值:
{
  "role":["r1","r2","r3"],
  "groupid":["g1","g2","g3"]   //groupid不为空时,查询条件必须满足,类似 in
}
{
  "role":["r1","r2","r3"],
  "groupid":null               //为空时,结果直接包含这一条
}
 
查询使用:
{
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "role": [
              "r3",
              "r4"
            ]
          }
        }
      ],
      "filter": [
        {
          "terms": {
            "groupid": [  //理想时此处在groupid为null时filter/terms不起作用,知道语句有问题,但不知道如何写
              "g1",
              "g4"
            ]
          }
        }
      ]
    }
  }
}
返回值(返回一组,而不是在groupid为null时,filter失效,返回两组值):
"hits" : [
      {
        "_index" : "hedextest",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "role" : [
            "r1",
            "r2",
            "r3"
          ],
          "groupid" : [
            "g1",
            "g2",
            "g3"
          ]
        }
      }
    ]

如果各位前辈知道如何解决这样的问题,麻烦帮助一下,多谢

要回复问题请先登录注册