Home > Enterprise >  How to filter entries in nested List by ID in nested List in elasticsearch with java?
How to filter entries in nested List by ID in nested List in elasticsearch with java?

Time:10-26

Basically I do have a Set of a Records which has a Set of NestedRecords included:

Set<Record> records;

These are the classes:

public class Record {
  String id;
  Set<NestedRecord> nestedRecords;
}

public class NestedRecord {
  String nestedId;
  int count;
}

What I want to achieve is filter the out the entries in the nestedRecords list by a Set of nested ids. So I only want the nestedRecords in the List in Records which include the id's in my set nested Id's

Let's say this is my Set:

Set<String> nestedIds = new HashSet<String>() {{
add("a");
add("b");
}};

So this is how I do it, but it always filters out everything.

TermsQueryBuilder recordExists = QueryBuilders.termsQuery("NESTED_RECORDS.NESTED_ID",Lists.newArrayList(nestedIds).toString());

NestedQueryBuilder nestedRecords = QueryBuilders.nestedQuery("NESTED_RECORDS", recordExists, ScoreMode.Total);

BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolQuery.must(nestedRecords);
query.filter(boolQuery);

CodePudding user response:

I think you are missing the path parameter in your nestedQuery call.

NestedQueryBuilder nestedRecords = QueryBuilders.nestedQuery("NESTED_RECORD", recordExists, ScoreMode.Total);

should be

NestedQueryBuilder nestedRecords = QueryBuilders.nestedQuery("nestedRecords", recordExists, ScoreMode.Total);

CodePudding user response:

I'm usually use Kotlin, so I don't know how java does it, but I think your toString() is wrong there

TermsQueryBuilder recordExists = nestedId.termsQuery("nestedRecords.nestedId",Lists.newArrayList(nestedIds).toString());

can be

TermsQueryBuilder recordExists = QueryBuilders.termsQuery("nestedRecords.nestedId", Lists.newArrayList(nestedIds));

and

NestedQueryBuilder nestedRecords = QueryBuilders.nestedQuery("NESTED_RECORD", recordExists, ScoreMode.Total);
NestedQueryBuilder nestedRecords = QueryBuilders.nestedQuery("nestedRecords", recordExists, ScoreMode.Total);
  • Related