Spring -data-elasticsearch using criteria with nested objects

I am new to elasticsearch and spring -data-elasticsearch, and I have some problems with querying for nested objects.

I am using the ElasticSearch repository to save an instance of a nested model in elasticsearch. As a result, there is only one entry in elasticsearch containing all the data, as I understand it, this means that I have a subdocument.

I need to implement a relatively complex query using criteria to create an iterative query. When I try to access a nested property using dot notation like

startPoint.providingTimeRange.startTime

I do not get any search results (but the corresponding data is present in elasticsearch).

In Spring Simplified data lookup with nested fields and display I found spring -data-elasticsearch being able to query for nested objects using nestedQuery.

Is there a way to combine search criteria with nestedQuery?

Thanks in advance, Christoph

Update 1: To provide some sample code, I created a demo project that contains similar nested objects, like in my real project: https://github.com/empulse-gmbh/elasticsearchtest

An example is to find FoodTrucks that are in a specific place at a specific time.

In this example, I used the repository to save my nested objects.

I used two (both not working) query approaches for nested objects:

        /*
         * add search criteria by timerange. it is assumed in timerange from
         * is always before to.
         */
        TimeRange searchTimeRange = foodTruckSearch.getSearchTimeRange();
        if (searchTimeRange != null) {

            String startTimePath = "locationPoint.timeRange.from";
            String endTimePath = "locationPoint.timeRange.to";

            searchCriteria = searchCriteria.and(
                    new Criteria(startTimePath).between(searchTimeRange
                            .getFrom().getTime(), searchTimeRange.getTo()
                            .getTime())).or(
                    new Criteria(endTimePath).between(searchTimeRange
                            .getFrom().getTime(), searchTimeRange.getTo()
                            .getTime()));
        }

and

    TimeRange searchTimeRange = foodTruckSearch.getSearchTimeRange();
    if (searchTimeRange != null) {

        String startTimePath = "locationPoint.timeRange.from";
        String endTimePath = "locationPoint.timeRange.to";

        searchQuery.must(nestedQuery(
                "locationPoint",
                boolQuery().should(
                        rangeQuery(startTimePath).from(
                                searchTimeRange.getFrom().getTime()).to(
                                searchTimeRange.getTo().getTime())).should(
                        rangeQuery(endTimePath).from(
                                searchTimeRange.getFrom()).to(
                                searchTimeRange.getTo()))));

    }

Update2: Mohsin Husen . . Elasticsearch:

elasticSearchNode = NodeBuilder.nodeBuilder().clusterName("foodtruck-test").local(true).build();

clusterName .

(, ) geo_distance. Elasticearch, , "like" "range", "/" "".

QueryBuilder FilterBuilder spring -data-elasticsearch .

, Location spring -data-elasticsearch GeoPoint.

:

    /*
     * add search criteria for radius search
     */
    FilterBuilder searchFilter = null;
    if (foodTruckSearch.getLatitude() != null
            && foodTruckSearch.getLongitude() != null) {

        if (foodTruckSearch.getSearchRadiusInKilometers() == null) {
            foodTruckSearch.setSearchRadiusInKilometers(5);
        }

        searchFilter = geoDistanceFilter("location.point")
                .distance(
                        foodTruckSearch.getSearchRadiusInKilometers()
                                + "km").lat(foodTruckSearch.getLatitude())
                .lon(foodTruckSearch.getLongitude());

    }

    if (searchFilter != null) {
        NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(
                searchQuery, searchFilter);
        return IteratorUtils.toList(foodTruckRepository.search(
                nativeSearchQuery).iterator());
    } else {
        return IteratorUtils.toList(foodTruckRepository.search(searchQuery)
                .iterator());
    }

:

org.elasticsearch.action.search.SearchPhaseExecutionException: Failedtoexecutephase[query_fetch],
allshardsfailed;shardFailures{
    [1][searchexample][0]: SearchParseException[[searchexample][0]: query[MatchNoDocsQuery],
    from[0],
    size[10]: ParseFailure[Failedtoparsesource[{
    "from": 0,
    "size": 10,
    "query": {
        "bool": {

        }
    },
    "post_filter": {
        "geo_distance": {
            "location.point": [6.9599115,
            50.9406645],
            "distance": "10km"
        }
    }
}]]];nested: QueryParsingException[[searchexample]failedtofindgeo_pointfield[location.point]]   ;
}

- geo_distance (?) spring -data-elasticsearch?

+4
2

return new ElasticsearchTemplate(elasticSearchNode.client());

spring -data-elasticsearch, Elasticsearch.

, , geo_distance,

@GeoPointField

, lat/lon.

, FoodTruckServiceImpl , .

GitHub . FoodTruck spring -data-elasticsearch.

+1

, , .

1) , ( ), spring elasticsearch M1 . (https://jira.springsource.org/browse/DATAES-53)

2) ,

    @Field(type=FieldType.Nested)
    private LocationPoint locationPoint;

    @Field(type = FieldType.Nested)
    private TimeRange timeRange;

3) , , query_string . elasticsearchTemplate .

4)

, locationPoint.timeRange.from locationPoint.timeRange.to

 String startTimePath = "locationPoint.timeRange.from";
    String endTimePath = "locationPoint.timeRange.to";

    searchQuery.must(nestedQuery(
            "locationPoint.timeRange",
            boolQuery().should(
                    rangeQuery(startTimePath).from(
                            searchTimeRange.getFrom().getTime()).to(
                            searchTimeRange.getTo().getTime())).should(
                    rangeQuery(endTimePath).from(
                            searchTimeRange.getFrom()).to(
                            searchTimeRange.getTo()))));

, .

+2

Source: https://habr.com/ru/post/1526939/


All Articles