ES查询多个索引,但是某些索引的name不同

这篇具有很好参考价值的文章主要介绍了ES查询多个索引,但是某些索引的name不同。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

参考: https://blog.csdn.net/qq_37147750/article/details/111319151

背景:
目前有四个索引index, 对于这四个index他们的字段并不完全相同,要支持筛选。
目前的问题是,其中有两个索引要先根据条件筛选一遍。后续的筛选根据这次的结果做基础。
但是这两个索引的筛选条件也不一样。
相当于 SQL:  (select * from A where a_id in (x1,x2,x3,x4)) union all (select * from B where b_id in (y1,x2,x3,x4)) union all 索引三  union all  索引四

在这个基础上做筛选

 public QueryBuilder addIndexCondition(String indexName, String field, List<String> terms){
	    QueryBuilder firstQuery = new BoolQueryBuilder()
                .must(QueryBuilders.termQuery("_index", indexName))
                .must(QueryBuilders.termsQuery(field, terms));
	    return firstQuery;
  }
  
  public QueryBuilder addIndexNoCondition(String indexName){
	    QueryBuilder firstQuery = new BoolQueryBuilder()
              .must(QueryBuilders.termQuery("_index", indexName));
	    return firstQuery;
}
if (StringUtils.isNotBlank(data_type) && (data_type.contains("1") && data_type.contains("2"))) {
            	
            	QueryBuilder youtubeQuery = sc.addIndexNoCondition("youtube_info");
            	QueryBuilder instagramQuery = sc.addIndexNoCondition("instagram_info");

            	
				QueryBuilder firstQuery = null;
				if(fbList.size() >0){
					firstQuery = sc.addIndexCondition("facebook_info", "fu_id", fbList);
				}
			
				QueryBuilder secondQuery = null;
				if(twList.size() >0){
					secondQuery = sc.addIndexCondition("twitter_info", "uid", twList);
				}
				
				if(firstQuery != null && secondQuery != null){
					QueryBuilder mainQuery = new BoolQueryBuilder().should(firstQuery).should(secondQuery).should(youtubeQuery).should(instagramQuery);
					sc.addQuery(mainQuery, ISIOperator.MUST);
					
				}else if(firstQuery != null && secondQuery == null){
					QueryBuilder mainQuery = new BoolQueryBuilder().should(firstQuery);
					sc.addQuery(mainQuery, ISIOperator.MUST);
				}else if(firstQuery == null && secondQuery != null){
					QueryBuilder mainQuery = new BoolQueryBuilder().should(secondQuery);
					sc.addQuery(mainQuery, ISIOperator.MUST);
				}
				
    			
            } else if ("1".equals(data_type)) {
            	if(twList.size() >0){
            		sc.addPrimitiveTermQuery("uid", twList, ISIOperator.MUST);//twitter
            	}
           
            } else if ("2".equals(data_type)) {
            	if(fbList.size() >0){
              		 sc.addPrimitiveTermQuery("fu_id", fbList, ISIOperator.MUST);//facebook
            	}
            }

DSL:文章来源地址https://www.toymoban.com/news/detail-536996.html

[twitter_info, facebook_info, youtube_info, instagram_info]


{
  "from" : 0,
  "size" : 15,
  "query" : {
    "bool" : {
      "must" : [
        {
          "bool" : {
            "should" : [
              {
                "bool" : {
                  "must" : [
                    {
                      "term" : {
                        "_index" : {
                          "value" : "facebook_info",
                          "boost" : 1.0
                        }
                      }
                    },
                    {
                      "terms" : {
                        "fu_id" : [
                         
                          
                          "1118",
                          "1119",
                          "1120"
                        ],
                        "boost" : 1.0
                      }
                    }
                  ],
                  "disable_coord" : false,
                  "adjust_pure_negative" : true,
                  "boost" : 1.0
                }
              },
              {
                "bool" : {
                  "must" : [
                    {
                      "term" : {
                        "_index" : {
                          "value" : "twitter_info",
                          "boost" : 1.0
                        }
                      }
                    },
                    {
                      "terms" : {
                        "uid" : [
                         
                          "990",
                          "991",
                          "1036"
                        ],
                        "boost" : 1.0
                      }
                    }
                  ],
                  "disable_coord" : false,
                  "adjust_pure_negative" : true,
                  "boost" : 1.0
                }
              },
              {
                "bool" : {
                  "must" : [
                    {
                      "term" : {
                        "_index" : {
                          "value" : "youtube_info",
                          "boost" : 1.0
                        }
                      }
                    }
                  ],
                  "disable_coord" : false,
                  "adjust_pure_negative" : true,
                  "boost" : 1.0
                }
              },
              {
                "bool" : {
                  "must" : [
                    {
                      "term" : {
                        "_index" : {
                          "value" : "instagram_info",
                          "boost" : 1.0
                        }
                      }
                    }
                  ],
                  "disable_coord" : false,
                  "adjust_pure_negative" : true,
                  "boost" : 1.0
                }
              }
            ],
            "disable_coord" : false,
            "adjust_pure_negative" : true,
            "boost" : 1.0
          }
        }
      ],
      "disable_coord" : false,
      "adjust_pure_negative" : true,
      "boost" : 1.0
    }
  },
  "stored_fields" : "auto_id",
  "sort" : [
    {
      "pubtime" : {
        "order" : "desc"
      }
    }
  ]
}

到了这里,关于ES查询多个索引,但是某些索引的name不同的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 如何使用ES做简单的时间条件过滤+模糊查询+精确匹配+关键字排除,查询 elasticsearch查询结果包含或排除某些字段、_source查询出需要的属性名称

    目录 一、时间条件过滤+模糊查询+精确匹配+排除 1. 查询出包含 log_geo 的数据 “wildcard”: { “message”: “log_geo” } 2. 查询某个时间段的数据 3. 条件查询与条件排除数据 4. from 表示起始的记录的ID 5. size 表示显示的记录数 6.sort排序 desc降序、asc升序  7.should查询在mysql中

    2024年01月18日
    浏览(54)
  • ElasticSearch系列 - SpringBoot整合ES:多个精确值查询 terms

    ElasticSearch - SpringBoot整合ES:多个精确值查询 terms 01. ElasticSearch terms 查询支持的数据类型 在Elasticsearch中,terms查询支持多种数据类型,包括: 字符串类型:可以将多个字符串值作为数组传递给terms查询,以匹配包含任何一个指定字符串值的文档。 数值类型:可以将多个数值作

    2024年02月16日
    浏览(58)
  • es查询响应结果中获取某些字段的值

            有时候使用es查询出的结果包含多个字段,如果数据中仅仅包含几个字段时,我们是很容易挑出自己需要的字段值,但是如果数据中包含几十或者几百甚至更多时,尤其是数据中嵌套好多层时,不容易直接挑取出需要的值,这时候可以借助程序直接查找出来。或者

    2024年02月12日
    浏览(43)
  • 原生语言操作和spring data中RestHighLevelClient操作Elasticsearch,索引,文档的基本操作,es的高级查询.查询结果处理. 数据聚合.相关性系数打分

    ​ Elasticsearch 是一个分布式、高扩展、高实时的搜索与数据分析引擎。它能很方便的使大量数据具有搜索、分析和探索的能力。充分利用Elasticsearch的水平伸缩性,能使数据在生产环境变得更有价值。Elasticsearch 的实现原理主要分为以下几个步骤,首先用户将数据提交到Elasti

    2024年02月05日
    浏览(59)
  • ES命令行查询es集群的状态、分片、索引

    查看es集群状态 查看es分片信息 查看es索引 查看ES索引 本文参考:https://www.cnblogs.com/expiator/p/14847705.html

    2024年02月12日
    浏览(33)
  • Es 索引查询与删除

    1、 #删除单个索引 2、#删除多个指定索引,中间用逗号隔开 3、#模糊匹配删除 4、#使用通配符,删除所有的索引 5、#获取当前索引 6、如果存储不够可以设置定时删除,下面是保留3天的日志 以下是定时删除脚本:

    2024年02月11日
    浏览(30)
  • 使用java来查询es索引(基于es7.8)

    1、先引入pom依赖: 2、然后在main方法里进行测试: 运行一下,打印结果跟在postman里执行出来是一样的: 后面会根据字段进行条件查询,所以在建立映射的时候,需要指定index属性为true。如下图,在postman创建映射:

    2024年02月11日
    浏览(36)
  • ES查询索引字段的分词结果

    一、_termvectors  1、查看文档中某一个字段的分词结果 GET /{index}/{type}/{_id}/_termvectors?fields=[field] 2、样例: text的值为:https://www.b4d99.com/html/202204/45672.html 得到的结果: 二、_analyze 1、语法 2、样例: text的值为:https://www.b4d99.com/html/202204/45672.html 得到的结果:

    2024年02月11日
    浏览(48)
  • ES(elasticsearch)删除指定索引

    需要删除指定的索引 执行命令 比如:DELETE /mysql-status_-2023.06 执行结果: 执行命令 比如:HEAD /mysql-status_-2023.06 执行结果: 说明已经删除完毕 删除命令: DELETE /索引名 查看是否删除成功: HEAD /索引名 查看索引命令: GET /索引名称 批量查看索引命令: GET /索引名称1,索引名称

    2024年02月11日
    浏览(52)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包