0
votes

I am looking for some way that I want to perform search on my index with NativeSearchQueryBuilder from Elastic java api but I want to add the following things while search. Index details:

  1. Filter type EdgeNgram
  2. White space tokenizer

I am looking for autocomplete functionality so here i want to apply the search keyword on multiple fields but it should apply using prefix to improve the performance, also I want to the results needs to be returned if they reach my specified page limit instead of keep on searching the index even it found enough results.

Ex: "albert einstein" is there in my index, now if I search "alb" it should return the result or if I search "ein" it should return the result.

    NativeSearchQueryBuilder sb = new NativeSearchQueryBuilder()
            .withIndices(Constants.ES_INDEX_NAME)
            //.withPageable(pageable)
            .withSourceFilter(new FetchSourceFilterBuilder().withIncludes("id").build())
            .withTypes(Constants.USERS_TYPE)
            .withQuery(multiMatchQuery("alb", new String[]{"userFirstName","userLastName","userMobile", "userEmail"}))
            .withFilter(boolQuery()
                    .must(termQuery("userCityName", "Chicago")));

Please someone help me on this, how to add prefix and limit to my Multimatch Query builder.

1

1 Answers

2
votes

What you are looking for is match_phrase_prefix

int limit = 100; //Set your limit

NativeSearchQueryBuilder sb = new NativeSearchQueryBuilder()
            .withIndices(Constants.ES_INDEX_NAME)
            .withPageable(new PageRequest(0, limit))
            .withSourceFilter(new FetchSourceFilterBuilder().withIncludes("id").build())
            .withTypes(Constants.USERS_TYPE)
            .withQuery(QueryBuilders.multiMatchQuery("alb", "userFirstName","userLastName","userMobile", "userEmail")
                                    .type(MatchQueryBuilder.Type.PHRASE_PREFIX))
            .withFilter(boolQuery()
                    .must(termQuery("userCityName", "Chicago")));