es的nested查询

一、一层嵌套
mapping:

PUT /nested_example
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "books": {
        "type": "nested",
        "properties": {
          "title": {
            "type": "text"
          },
          "author": {
            "type": "text"
          }
        }
      }
    }
  }
}

导入数据到索引:


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" } ] }

使用DevTools执行多层嵌套查询

 POST /nested_example/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "Tom"
          }
        },
        {
          "nested": {
            "path": "books",
            "query": {
              "match": {
                "books.author": "Jerry"
              }
            }
          }
        }
      ]
    }
  }
}

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();
    }
}


二、多层嵌套
mapping:

PUT /nested_example
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "books": {
        "type": "nested",
        "properties": {
          "title": {
            "type": "text"
          },
          "authors": {
            "type": "nested",
            "properties": {
              "author_name": {
                "type": "text"
              }
            }
          }
        }
      }
    }
  }
}

导入数据到索引

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" } ] } ] }

使用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"
                  }
                }
              }
            }
          }
        }
      ]
    }
  }
}

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值