Randomize result from Elasticsearch using NativeSearchQueryBuilder

Hi, I am the new ElasticSearch, I am using spring data. I have 2 APIs that store data in an article and discourse model using elastic search, now that the client application calls the API call to search for articles and discourse, it gives the whole article first, and then the discourse data. but I want to randomize the answer, how can I do this?

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 did 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();
+4
source share

Source: https://habr.com/ru/post/1684667/


All Articles