没有。即使总命中数为非零值,命中也为零

时间:2015-07-21 14:53:58

标签: java elasticsearch

我正在使用Java API发送搜索请求。 System.out.println(searchResponse.getHits().totalHits());给了我一个非零数字,我确认这是正确的,因为我在Sense上检查了它。但是,只要尝试在for (SearchHit hit : searchResponse.getHits()) {...}之类的匹配上运行循环或尝试单独访问匹配searchResponse.getHits().getAt(0).getSource().get("doc_id");,我就会java.lang.ArrayIndexOutOfBoundsException

我有一个约10000条款的清单,我必须为其进行个别搜索。第一个术语给出答案,但是对于第二个术语,我将命中数作为一些非零数字,但SearchHits为空!

public Tuple2<Set<String>, String> getUnstemmedTerms(String indexName, String stem) throws IOException {
        Map<String, Integer> unstemmed = new HashMap<>();
        SearchRequestBuilder searchRequestBuilder = client.getClient().prepareSearch(indexName)
                .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
                .setScroll(new TimeValue(60000))
                .setQuery(QueryStem(stem))
                .setSize(10);
        SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();

        while(true) {
            searchResponse = client.getClient()
                    .prepareSearchScroll(searchResponse.getScrollId())
                    .setScroll(new TimeValue(60000))
                    .execute()
                    .actionGet();
            System.out.println(searchResponse.getHits().totalHits()); // gives 3
            searchResponse.getHits().getAt(0).getSource().get("doc_id"); // gives java.lang.ArrayIndexOutOfBoundsException

            for (SearchHit hit : searchResponse.getHits()) {
            // Do some stuff
            }       
            if (searchResponse.getHits().hits().length == 0)
                break;
        }
        System.out.println(unstemmed.size()); // gives 0
        return new Tuple2<>(unstemmed.keySet(), term);
    }

我不知道为什么会这样。

0 个答案:

没有答案