• es的nested查询


    一、一层嵌套
    mapping:

    PUT /nested_example
    {
      "mappings": {
        "properties": {
          "name": {
            "type": "text"
          },
          "books": {
            "type": "nested",
            "properties": {
              "title": {
                "type": "text"
              },
              "author": {
                "type": "text"
              }
            }
          }
        }
      }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    导入数据到索引:

    
    POST /nested_example/_bulk
    { "index" : { "_id" : "1" } }
    { "name" : "John Doe", "books" : [ { "title" : "Book 1", "author" : "Author 1" }, { "title" : "Book 2", "author" : "Author 2" } ] }
    { "index" : { "_id" : "2" } }
    { "name" : "Jane Smith", "books" : [ { "title" : "Book 3", "author" : "Author 3" }, { "title" : "Book 4", "author" : "Author 4" } ] }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    使用DevTools执行多层嵌套查询

     POST /nested_example/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "name": "Tom"
              }
            },
            {
              "nested": {
                "path": "books",
                "query": {
                  "match": {
                    "books.author": "Jerry"
                  }
                }
              }
            }
          ]
        }
      }
    }
    
    
    • 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

    Java查询语句

     import org.elasticsearch.action.search.SearchRequest;
    import org.elasticsearch.client.RequestOptions;
    import org.elasticsearch.client.RestHighLevelClient;
    import org.elasticsearch.index.query.BoolQueryBuilder;
    import org.elasticsearch.index.query.MatchQueryBuilder;
    import org.elasticsearch.index.query.NestedQueryBuilder;
    import org.elasticsearch.index.query.QueryBuilders;
    import org.elasticsearch.search.builder.SearchSourceBuilder;
    
    public class ElasticsearchNestedQueryExample {
        public static void main(String[] args) throws Exception {
            RestHighLevelClient client = new RestHighLevelClient();
    
            SearchRequest searchRequest = new SearchRequest("nested_example");
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    
            // 构建bool查询
            BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
    
            // 添加must子句用于匹配name字段为"Tom"
            MatchQueryBuilder nameQuery = QueryBuilders.matchQuery("name", "Tom");
            boolQuery.must(nameQuery);
    
            // 构建嵌套查询
            NestedQueryBuilder nestedQuery = QueryBuilders.nestedQuery(
                "books",
                QueryBuilders.matchQuery("books.author", "Jerry"),
                ScoreMode.None // 可选的分数模式
            );
            boolQuery.must(nestedQuery);
    
            sourceBuilder.query(boolQuery);
            searchRequest.source(sourceBuilder);
    
            // 执行查询
            client.search(searchRequest, RequestOptions.DEFAULT);
    
            // 关闭客户端连接
            client.close();
        }
    }
    
    
    
    • 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

    二、多层嵌套
    mapping:

    PUT /nested_example
    {
      "mappings": {
        "properties": {
          "name": {
            "type": "text"
          },
          "books": {
            "type": "nested",
            "properties": {
              "title": {
                "type": "text"
              },
              "authors": {
                "type": "nested",
                "properties": {
                  "author_name": {
                    "type": "text"
                  }
                }
              }
            }
          }
        }
      }
    }
    
    • 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

    导入数据到索引

    POST /nested_example/_bulk
    { "index" : { "_id" : "1" } }
    { "name" : "John Doe", "books" : [ { "title" : "Book 1", "authors" : [ { "author_name" : "Author 1" }, { "author_name" : "Author 2" } ] } ] }
    { "index" : { "_id" : "2" } }
    { "name" : "Jane Smith", "books" : [ { "title" : "Book 2", "authors" : [ { "author_name" : "Author 2" }, { "author_name" : "Author 3" } ] } ] }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    使用DevTools执行多层嵌套查询

    POST /nested_example/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "name": "Tom"
              }
            },
            {
              "nested": {
                "path": "books",
                "query": {
                  "nested": {
                    "path": "books.authors",
                    "query": {
                      "match": {
                        "books.authors.author_name": "Author 2"
                      }
                    }
                  }
                }
              }
            }
          ]
        }
      }
    }
    
    • 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

    Java查询语句:

    import org.elasticsearch.action.search.SearchRequest;
    import org.elasticsearch.client.RequestOptions;
    import org.elasticsearch.client.RestHighLevelClient;
    import org.elasticsearch.index.query.BoolQueryBuilder;
    import org.elasticsearch.index.query.MatchQueryBuilder;
    import org.elasticsearch.index.query.NestedQueryBuilder;
    import org.elasticsearch.index.query.QueryBuilders;
    import org.elasticsearch.search.builder.SearchSourceBuilder;
    
    public class ElasticsearchNestedNestedQueryExample {
        public static void main(String[] args) throws Exception {
            RestHighLevelClient client = new RestHighLevelClient();
    
            SearchRequest searchRequest = new SearchRequest("nested_example");
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    
            // 构建bool查询
            BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
    
            // 添加must子句用于匹配name字段为"Tom"
            MatchQueryBuilder nameQuery = QueryBuilders.matchQuery("name", "Tom");
            boolQuery.must(nameQuery);
    
            // 构建外层nested查询
            NestedQueryBuilder outerNestedQuery = QueryBuilders.nestedQuery(
                "books",
                // 构建内层nested查询
                QueryBuilders.nestedQuery(
                    "books.authors",
                    QueryBuilders.matchQuery("books.authors.author_name", "Author 2"),
                    ScoreMode.None // 可选的分数模式
                ),
                ScoreMode.None // 可选的分数模式
            );
    
            boolQuery.must(outerNestedQuery);
    
            sourceBuilder.query(boolQuery);
            searchRequest.source(sourceBuilder);
    
            // 执行查询
            client.search(searchRequest, RequestOptions.DEFAULT);
    
            // 关闭客户端连接
            client.close();
        }
    }
    
    • 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
  • 相关阅读:
    基于深度学习网络的烟雾检测算法matlab仿真
    【Redis】事务、lua脚本、发布订阅、异步连接
    微服务中的鉴权该怎么做?
    【全志T113-S3_100ask】8-USB串口获取GPS数据(含解析)
    HTML基础 - SVG标签
    计算机毕业设计Java居民个人健康信息与个人健康服务综合平台(源码+mysql数据库+系统+lw文档)
    架构篇(七)安全架构
    HTML5 基础
    Cilium 1.11:服务网格的未来已来
    Qt源码解析-源码解析-QVideoWidget播放手机视频旋转问题
  • 原文地址:https://blog.csdn.net/jifgnie/article/details/133526929