参考: 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 索引四
在这个基础上做筛选文章来源:https://www.toymoban.com/news/detail-536996.html
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模板网!