4

Hi I am new ElasticSearch, I am using spring data. I have 2 API which saves data in article and discourse model using elastic search, now when a client app makes a API call for both article and discourse search it gives all article first and then discourse data. but i want to randomize the response how can i do that?

my article model class as follows

@AllArgsConstructor
@Data
@Document(indexName="articles", createIndex=true)
public class Article implements ITResult {

    private String id;
    private String hostContentId;
    private String title;
    private List<String> categories;
    private String searchResultId;

    @Override
    public String getSummary() {
        return excerpt;
    }
    @Override
    public ContentType getContentType() {
        return ContentType.ARTICLE;
    }
    @Override
    public String getHostContentId() {
        return hostContentId;
    }
    @Override
    public String getUrl() {
        return link;
    }
    @Override
    public String getSearchResultId() {
        return searchResultId;
    }
    public void setSearchResultId(String searchResultId) {
        this.searchResultId = searchResultId;
    }
}

I have done the following

SearchQuery query = new NativeSearchQueryBuilder().withIndices("articles","course")
                .withPageable(new PageRequest(offset,limit))
                .withFilter(multiMatchQuery(string, new String[] { "title", "excerpt", "author_name", "link"}))
                .build();
Suyash
  • 331
  • 1
  • 4
  • 20
  • The builder supports a .withSort() method, which uses a SortBuilder object. There is a ScriptSortBuilder subclass that may allow you to do this. Take a look at the Script class and these examples: https://stackoverflow.com/questions/9796470/random-order-pagination-elasticsearch – Tim Sep 01 '17 at 06:07
  • https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-random – Dave Jarvis Sep 05 '17 at 19:43

0 Answers0