Elasticsearch 跨索引库搜索

跨索引库搜索

mapping元数据字段

Metadata fields are used to customize how a document’s associated metadata is treated.
元数据字段用于自定义处理文档相关元数据

Identity metadata fields 身份元数据

  • _index

The index to which the document belongs. 文档所在索引库

  • _type

The document’s mapping type.

  • _id

The document’s ID. 文档唯一标识

注意 _index _id 可用于 query 查询

_index 查询,包括跨索引库查询,_bulk 查询,_mget 查询,_search 查询,_msearch 查询

PUT index_1/_doc/1
{
  "text": "Document in index 1"
}

PUT index_2/_doc/2?refresh=true
{
  "text": "Document in index 2"
}

GET index_1,index_2/_search
{
  "query": {
    "terms": {
      "_index": ["index_1", "index_2"] 
    }
  },
  "aggs": {
    "indices": {
      "terms": {
        "field": "_index", 
        "size": 10
      }
    }
  },
  "sort": [
    {
      "_index": { 
        "order": "asc"
      }
    }
  ],
  "script_fields": {
    "index_name": {
      "script": {
        "lang": "painless",
        "source": "doc['_index']" 
      }
    }
  }
}

_id 查询,包括跨索引库查询,_bulk 查询,_mget 查询,_search 查询,_msearch 查询

The value of the `_id` field is accessible in queries such as `term`, `terms`, `match`, and `query_string`
可用于 term,terms,match,query_string
# Example documents
PUT my-index-000001/_doc/1
{
  "text": "Document with ID 1"
}

PUT my-index-000001/_doc/2?refresh=true
{
  "text": "Document with ID 2"
}

GET my-index-000001/_search
{
  "query": {
    "terms": {
      "_id": [ "1", "2" ] 
    }
  }
}

使用 _index 跨索引库搜索

不使用 _index 则会存在命中不同索引库的相同字段返回重复数据,在 bool 的 should 查询中加入 _index 可支持指定索引库 or 查询

# 跨索引搜索
GET public_dynamic_index_sc,user_publish_mood_index_sc/_search
{
    "size":100,
    "query":{
        "bool":{
            "should":[
                {
                    "bool":{
                        "must":[
                            {
                                "term":{
                                    "user_id":5
                                }
                            },
                            {
                                "term":{
                                    "_index":"public_dynamic_index_sc"
                                }
                            },
                            {
                                "term":{
                                    "del_flag":0
                                }
                            },
                            {
                                "terms":{
                                    "verify_status":[0,2,3]
                                }
                            }
                        ]
                    }
                },
                {
                    "bool":{
                        "must":[
                            {
                                "term":{
                                    "user_id":5
                                }
                            },
                            {
                                "term":{
                                    "del_flag":0
                                }
                            },
                            {
                                "term":{
                                    "_index":"user_publish_mood_index_sc"
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}

java 客户端

		// 作品
		BoolQueryBuilder dynamicMustQueryBuilder = QueryBuilders.boolQuery()
				.must(QueryBuilders.termQuery("user_id", 5))
				.must(QueryBuilders.termQuery("del_flag", 0))
				.must(QueryBuilders.termsQuery("verify_status", new int[]{0,2,3}))
				.must(QueryBuilders.termQuery("_index", DYNAMIC_INDEX));

		// 心情
		BoolQueryBuilder userMoodMustQueryBuilder = QueryBuilders.boolQuery()
				.must(QueryBuilders.termQuery("user_id", 5))
				.must(QueryBuilders.termQuery("del_flag", 0))
				.must(QueryBuilders.termQuery("_index", MOOD_INDEX));
		BoolQueryBuilder allShouldQueryBuilder = QueryBuilders.boolQuery()
				.should(dynamicMustQueryBuilder)
				.should(userMoodMustQueryBuilder);

		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
				.query(allShouldQueryBuilder)
				.size(100);
		SearchRequest searchRequest = new SearchRequest(DYNAMIC_INDEX,MOOD_INDEX).source(searchSourceBuilder);

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:像素格子 设计师:CSDN官方博客 返回首页
评论
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值