使用Spring SearchQuery ElasticSearch进行每次点击的得分 [英] Score of each hit with spring SearchQuery ElasticSearch

查看:301
本文介绍了使用Spring SearchQuery ElasticSearch进行每次点击的得分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过SearchQuery进行搜索时查看并使用每个匹配的invidual _score。除其他事项外,这是了解我的搜索得分的范围。但除了使用searchQuery.withMinScore(float)设置MinScore之外;我找不到任何处理搜索分数的方法。

I'm trying to see and use the invidual _score of each hit when doing a search by a SearchQuery. This is, among other things, to know in what range of scores my searches result in. But other than setting a MinScore using searchQuery.withMinScore(float); I can't find any method for handling the scores of search.

@Override
public Page<Website> listsearch(SearchBody searchBody, int size, int page) {
    BoolQueryBuilder qb = QueryBuilders.boolQuery();

    for(SearchUnit unit:searchBody.getSearchBody()){
        if(unit.isPriority()) {
            qb.must(matchQuery("_all", unit.getWord()).operator(MatchQueryBuilder.Operator.AND)
                    .fuzziness(Fuzziness.AUTO));

        }else {
            qb.should(termQuery("_all", unit.getWord())
                    .boost(unit.getWeight()));
        }
    }

    for(SearchUnit ExUnit:searchBody.getExcludeBody()){
        qb.mustNot(matchPhraseQuery("_all",ExUnit.getWord()));
    }

    SearchQuery searchQuery = new NativeSearchQueryBuilder()
            .withIndices("websites_v1")
            .withTypes("website")
            .withQuery(qb)
            .withMinScore(0.05F)//Magical minscore
            .withPageable(new PageRequest(page, size))
            .build();


    Page<Website> search = searchRepository.search(searchQuery);
    return search;

}

使用的搜索功能来自org.springframework.data。 elasticsearch.repository;定义为

The search function used is from org.springframework.data.elasticsearch.repository; defined as

Page<T> search(SearchQuery var1);

所以我的问题是,无论如何我可以访问页面中每个返回对象的分数?或者我是否需要将查询方法切换为其他内容才能实现?

So my question is there anyway I can access the score of each returned object in the Page? Or do I need to switch my query method to something else to achive that?

推荐答案

使用Spring Data ElasticSearch存储库是不可能的。

This is not possible with the Spring Data ElasticSearch repositories.

您需要自动装配 EntityMapper ElasticSearchTemplate 并自己提取分数。这样的事情应该有效:

You need to autowire an EntityMapper and an ElasticSearchTemplate and extract the score yourself. Something like this should work:

Pageable pageRequest = new PageRequest(0, 10);
Page<Website> result = elasticSearchTemplate.query(searchQuery, new ResultsExtractor<Page<Website>>() {
    @Override
    public Page<Website> extract(SearchResponse response) {
        List<Website> content = new ArrayList<>();
        SearchHit[] hits = response.getHits().getHits();
        for (SearchHit hit : hits) {
            Website website = entityMapper.mapToObject(hit, Website.class);
            content.add(website);
            float documentScore = hit.getScore(); // <---- score of a hit
        }
        return new PageImpl<Website>(content, pageRequest, response.getHits().getTotalHits());
    }
});

这篇关于使用Spring SearchQuery ElasticSearch进行每次点击的得分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆