0

I have a requirement where i need to to fetch all the users using elastic search for the landing page using pagination where in topmost/first record should be from current logged in user.

I am able to get the paginated response for all the users using match all query but i am not able to get the current user record as top most record in paginated response.

is it possible to achieve this using elastic search query, my elastic search version is 7.6.0

here is my code

    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    BoolQueryBuilder builder = QueryBuilders.boolQuery();
    long totalHits;
    builder.must(QueryBuilders.matchAllQuery());
    builder.must(QueryBuilders.existsQuery(book.squadMemberMongoId.keyword)); // squad member must be present

    sourceBuilder.from(from);
    sourceBuilder.size(size);
    sourceBuilder.trackTotalHits(true);
    sourceBuilder.trackTotalHitsUpTo();
    sourceBuilder.sort((SortBuilders.fieldSort("book.eaLastName.keyword").order(SortOrder.ASC)));
    sourceBuilder.query(builder);
    searchRequest.source(sourceBuilder);
// client is instance of RestHighLevelClient        
   response=client.search(searchRequest, RequestOptions.DEFAULT);

   SearchHit[] hits = searchResponse.getHits().getHits();

is there any way where i can make above query to return current logged in user / specific record in top of the response list (current logged user can be identified by squadMemberMongoId field in data)

Sample Data:

{
  "submissionId": "232323237",
  "revision": 2,
  "author": {
  "country": "xyz",
  "email": "john.doe@informa.com",
  "name": "john doe"
},
"book": {
 "eaLastName" : "xyzea"
 "squad": "John",
 "squadMemberMongoId": 12345678
}
....

}

submissionId is the index key here

expected response:

If i have multiple different submissions with different submission ids and different squadMemberMongoId under book , when i make a query i want record with specific squadMemberMongoId to be on top of the response then followed by other records with different id's, entire response set has to be paginated

Nikhil Kamani
  • 850
  • 9
  • 12
  • 1
    Can you please povide index mapping, sample data and expected result from it. – Sagar Patel Dec 26 '22 at 07:30
  • I am able to achieve my case by grouping my queries in FunctionScoreQueryBuilder and providing each set with weight factor, group with highest factor is returned on top in complete result set currently i am testing if it works allways – Nikhil Kamani Dec 27 '22 at 06:11
  • Have you considered something like the "should" query here? https://stackoverflow.com/a/18486519/2691904 If you set minimum_number_should_match to 0 it should still return all records, but will let you give a boost to the on with the matching squadMemberMongoId? – hugh Jan 01 '23 at 12:24

0 Answers0