当前位置:   article > 正文

es的nested查询_querybuilders.nestedquery

querybuilders.nestedquery

一、一层嵌套
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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/654402
推荐阅读
  

闽ICP备14008679号