1

My Document is like:

class Foo{
 private Integer idDl;
 private String Name;
 private String Add;
 @Field(type = FieldType.Nested)
 private List< Bar> Bar;
 }
 
 class Bar{
 private Integer barId;
 private List<String> barData
 }

and Foo sample data is like:

{
    "idDl": 123,
    "Name": "ABCD",
    "Add": "FL",
    "Bar": [
        {
            "barId": 456,
            "barData": [
                "Bar1",
                "Bar2"
            ]
        },
        {
            "barId": 985,
            "barData": [
                "Bar4",
                "Bar5"
            ]
        }
    ]
}

I want to return all Fooobjects where Bar.barId is matching but as Bar is list object in Foo, Foo must only contain only one Bar object who's id is matching with the id provided by user.I am using NativeSearchQueryBuilder provided by spring-data-elasticsearch as:

String[] includeFields = new String[]{"idDl", "Name"};
String[] excludeFields = new String[]{"Add"}; // to exclude Add field of Foo
Query searchQuery = new NativeSearchQueryBuilder()
            .withQuery(matchQuery("Bar.barId", 456))
            //.withQuery(termQuery("Bar.barId", 456))
            .withSourceFilter(new FetchSourceFilter(includeFields, excludeFields))
            .build();
return elasticsearchRestTemplate.queryForList( searchQuery, Foo.class);

The response I am getting consist of all Bar objects irrespective of Id, here is the sample response:

[
    {
        "idDl": 123,
        "Name": "ABCD",
        "Add": "FL",
        "Bar": [
            {
                "barId": 456,
                "barData": [
                    "Bar1",
                    "Bar2"
                ]
            },
            {
                "barId": 985,
                "barData": [
                    "Bar4",
                    "Bar5"
                ]
            }
        ]
    },
    {
        "idDl": 758,
        "Name": "PQR",
        "Add": "NY",
        "Bar": [
            {
                "barId": 456,
                "barData": [
                    "Bar1",
                    "Bar2"
                ]
            },
            {
                "barId": 671,
                "barData": [
                    "Bar24",
                    "Bar25"
                ]
            }
        ]
    }
]

I tried using termQuery as commented in the snippet but i ain't getting response for it and for matchQuery I am getting response as above. In the response the Bar must contain only objects having the id 456 i.e. Id being sent in query. Any suggestions will be helpful

1 Answer 1

1

You are querying for Foo objects where the exists a Bar that matches the criteria and Elasticsearch returns these Foos. If you want to have the only the Bars that matched, you need to add inner_hits to your query.

Check this question and answer for how to retrieve these, retrieving inner hits with Spring Data Elasticsearch will come with version 4.1.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.