根据得分

时间:2016-11-30 14:53:50

标签: elasticsearch

我创建了一个索引,其中10个文档被编入索引,并且我在其中一个字段中执行了匹配查询。匹配查询命中所有10个文档并显示所有文档。我已根据“from”参数打印查询结果以过滤掉结果。 代码是:

    public static void main(String[] args) {
    Client client = ESUtils.getClient("jagaran", 9300);
    ESUtils.clusterHealth(client);

    if (client.admin().indices().prepareExists("football").execute()
            .actionGet().isExists())
        client.admin().indices().prepareDelete("football").execute()
                .actionGet();

    ESUtils.createIndex(client, "football");

    indexParentDoc(client);
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    SearchResponse response = client.prepareSearch("football")
            .setQuery(QueryBuilders.matchQuery("name", "Manchester"))
            .addSort("_score", SortOrder.ASC).get();

    System.out.println("response5------->" + "\n" + response);
    SearchHits searchHits = response.getHits();

    long totalHits = searchHits.getTotalHits();
    System.out.println("totalhits" + totalHits);

    float fromValue = (float) (0.2 * totalHits);
    BigDecimal bd = new BigDecimal(Float.toString(fromValue));
    int newScale = 0;
    bd = bd.setScale(newScale, BigDecimal.ROUND_CEILING);
    int from = bd.intValueExact();

    SearchResponse response1 = client.prepareSearch("football")
            .setFrom(from)
            .setQuery(QueryBuilders.matchQuery("name", "manchester"))
            .addSort("_score", SortOrder.ASC).execute().actionGet();

    System.out.println("Minimized response:" + "\n" + response1);

}

在上面的代码中,我已经执行了两次查询,这显然效率不高。 在我的第一个查询中,我已经执行以获得总命中数,并且基于搜索命中长度,我已经计算了“从”值来过滤结果。 那么有没有其他选择,以便我可以使用单个查询运行上述逻辑?

0 个答案:

没有答案