• 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 查询
    
    • 1

    _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']" 
          }
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    _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" ] 
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    使用 _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"
                                    }
                                }
                            ]
                        }
                    }
                ]
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    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);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
  • 相关阅读:
    企业信息化与电子商务>供应链信息流
    基于JAVA商超销售系统计算机毕业设计源码+数据库+lw文档+系统+部署
    Rockchip Clock
    往USBKey里面导入双证书专题:概念介绍、执行逻辑
    Linux shell编程学习笔记28:脚本调试 set命令
    【web课程设计网页规划与设计】基于HTML+CSS+JavaScript火车票网上预订系统网站(4个页面)
    SpringBoot框架集成Dubbo
    网络管理器(NetworkManager)未运行。 这个问题如何解决
    怎么把视频转换成mp4格式?
    JAVA多线程技术-线程的生命周期
  • 原文地址:https://blog.csdn.net/white_while/article/details/126225099