ES 嵌套查询

背景

一个配方由多种原材料组成,需求是根据各种原材料的用量搜索出对应的配方

配方实体类

java 复制代码
class Formula {

    private long id;

    private String name;

    private List<Material> materials;

}

class Material {

    @JsonProperty("material_id")
    private long materialId;

    private float amount;

}

1、定义索引映射 Mapping

PUT /formula
{
  "mappings": {
    "properties": {
      "id": {
        "type": "long"
      },
      "name": {
        "type": "text"
      },
      "materials": {
        "type": "nested",
        "properties": {
          "material_id": {
            "type": "long"
          },
          "amount": {
            "type": "float"
          }
        }
      }
    }
  }
}

2、添加测试数据

POST /formula/_doc/1
{
  "id": "1",
  "name": "formula A",
  "materials": [
    { "material_id": "material_1", "amount": 100 },
    { "material_id": "material_2", "amount": 50 }
  ]
}

POST /formula/_doc/2
{
  "id": "2",
  "name": "formula B",
  "materials": [
    { "material_id": "material_2", "amount": 30 },
    { "material_id": "material_3", "amount": 20 }
  ]
}

3、查询

GET /formula/_search
{
  "query": {
    "bool": {
        "must": [
            {
                "match": {
                    "name": "formula A"
                }
            },
            {
                "nested": {
                    "path": "materials",
                    "query": {
                        "bool": {
                        "must": [
                            { "match": { "materials.material_id": "2" } },
                            { "range": { "materials.amount": { "gte": 40, "lte": 60 } } }
                        ]
                        }
                    }
                }
            }
        ]
    }
  }
}

RestHighLevelClient

java 复制代码
// nested
BoolQueryBuilder nestedBoolQueryBuilder = new BoolQueryBuilder();
nestedBoolQueryBuilder.must().add(QueryBuilders.matchPhraseQuery("materials.material_id", 1));
nestedBoolQueryBuilder.must().add(QueryBuilders.rangeQuery("materials.amount").from(40).to(60));
NestedQueryBuilder nestedQueryBuilder =  QueryBuilders.nestedQuery("materials", nestedBoolQueryBuilder, ScoreMode.Avg);

// query
BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
boolQueryBuilder.must(nestedQueryBuilder);
// simple
boolQueryBuilder.must(QueryBuilders.matchQuery("name", "formula A"));

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(boolQueryBuilder);

SearchRequest searchRequest = new SearchRequest(new String[]{index}, searchSourceBuilder);

// response
SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

ElasticsearchClient

java 复制代码
SearchResponse<Formula> response = elasticsearchClient.search(s -> s
                .index("formula")
                .query(q -> q.match(t -> t.field("name").query("formula A")))
                .query(q -> q.nested(nq -> nq.path("materials")
                                .query(nq1 -> nq1.match(t -> t.field("materials.material_id").query("2")))
                                .query(nq2 -> nq2.range(r -> r.field("materials.amount").from("40").to("60")))
                        )
                ),
        Formula.class
);
相关推荐
XMYX-02 小时前
Python 操作 Elasticsearch 全指南:从连接到数据查询与处理
python·elasticsearch·jenkins
落落落sss3 小时前
MQ集群
java·服务器·开发语言·后端·elasticsearch·adb·ruby
河岸飞流4 小时前
Centos安装Elasticsearch教程
elasticsearch·centos
爱吃土豆的马铃薯ㅤㅤㅤㅤㅤㅤㅤㅤㅤ5 小时前
Elasticsearch的查询语法——DSL 查询
大数据·elasticsearch·jenkins
A陈雷5 小时前
springboot整合elasticsearch,并使用docker desktop运行elasticsearch镜像容器遇到的问题。
spring boot·elasticsearch·docker
Make_magic6 小时前
Git学习教程(更新中)
大数据·人工智能·git·elasticsearch·计算机视觉
Elastic 中国社区官方博客7 小时前
使用真实 Elasticsearch 进行更快的集成测试
大数据·运维·服务器·数据库·elasticsearch·搜索引擎·集成测试
SafePloy安策15 小时前
ES信息防泄漏:策略与实践
大数据·elasticsearch·开源
涔溪16 小时前
Ecmascript(ES)标准
前端·elasticsearch·ecmascript
csdn56597385019 小时前
Elasticsearch 重建索引 数据迁移
elasticsearch·数据迁移·重建索引