本文介绍了对复合对象数组以及日期范围进行Elasticsearch查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,如何为具有日期范围和诸如此类的其他字段参数的嵌套复合对象创建弹性搜索查询

Hi I have a question on how to create an elastic search query for a nested composite object with date ranges and additional field parameters like so

[{
            "name": "A",
            "availability": [
                {
                    "partial": true,
                    "dates": {
                        "gte": "2020-12-01",
                        "lte": "2020-12-02"
                    }
                }
            ]
        },
        {
            "name": "B",
            "availability": [
                {
                    "partial": true,
                    "dates": {
                        "gte": "2020-12-05",
                        "lte": "2020-12-06"
                    }
                },
                {
                    "partial": false,
                    "dates": {
                        "gte": "2020-12-08",
                        "lte": "2020-12-11"
                    }
                }
            ]
        }]

这是我的实体数据

@Document(indexName = "workers")
public class Worker {
    @Id
    private String id;

    @Field(type = FieldType.Text)
    private String name;

    @Field(type = FieldType.Nested)
    private List<Availability> availability;
}

public class Availability {
    @Field(type = FieldType.Boolean)
    private boolean partial;

    @Field(type = FieldType.Date_Range, format = DateFormat.custom, pattern = "uuuu-MM-dd")
    private Map<String, LocalDate> dates;
}


这是我当前编写的搜索查询,但结果为空

This is the search query that I have currently written, but the results come as empty

         final BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
queryBuilder.must(QueryBuilders.termQuery("availability.partial", query.isPartial()));

         RangeQueryBuilder availability = QueryBuilders.rangeQuery("availability.dates")
                   .gte(query.getStartDate())
                   .lte(query.getEndDate());
queryBuilder.must(availability);

        Pageable pageable = PageRequest.of(pageNumber, pageSize);

        // @formatter:off
        return new NativeSearchQueryBuilder()
                .withPageable(pageable)
                .withQuery(queryBuilder)
                .build();

这是我的查询dto

public class WorkerQuery {
    private boolean partial;
    private LocalDate startDate;
    private LocalDate endDate;
}

// Request data
{
    "partial": true,
    "startDate": "2020-12-01",
    "endDate": "2020-12-02"
}

推荐答案

很好的开始!!因为 availability nested ,所以您只是缺少了 nested 查询.Java查询必须是这样的:

Great start!! You're just missing a nested query since availability is nested. The Java query needs to be like this:

final BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
queryBuilder.must(QueryBuilders.termQuery("availability.partial", query.isPartial()));

RangeQueryBuilder availability = QueryBuilders.rangeQuery("availability.dates")
               .gte(query.getStartDate())
               .lte(query.getEndDate())
               .relation("within");
queryBuilder.must(availability);

final NestedQueryBuilder nested = QueryBuilders.nestedQuery("availability", queryBuilder);

Pageable pageable = PageRequest.of(pageNumber, pageSize);

// @formatter:off
return new NativeSearchQueryBuilder()
            .withPageable(pageable)
            .withQuery(nested)
            .build();

这篇关于对复合对象数组以及日期范围进行Elasticsearch查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 20:56