简体   繁体   English

elasticsearch java API:matchAll搜索查询不返回结果?

[英]elasticsearch java API: matchAll search query doesn't return results?

I've got an in-memory instance of elastic search running, and doing some exploratory coding to learn the search java API. 我有一个弹性搜索运行的内存实例,并做一些探索性编码来学习搜索java API。 I am able to submit documents to the index and retrieve them using GET, but when I try a simple search query, I am not getting any results. 我能够将文档提交到索引并使用GET检索它们,但是当我尝试一个简单的搜索查询时,我没有得到任何结果。

// first, try a get request, to make sure there is something in the index
GetResponse results = client.prepareGet(INDEX_NAME, INDEX_TYPE, testID)
        .execute()
        .actionGet();
// this assertion succeeds, as we expect it to.
assertThat(results.getId()).isEqualTo(testID);

// next, try the simplest possible search
SearchResponse s1 = client.prepareSearch(INDEX_NAME).setQuery(matchAllQuery())
        .execute()
        .actionGet();
// this assertion fails. why? answer: when we have an in-memory node, we have to 
// manually call refresh on the indexing, after submitting a document.
assertThat(s1.getHits().totalHits()).isGreaterThanOrEqualTo(1);

after some testing, I think the problem is in how I am setting up my Node and associated client (in memory): 经过一些测试,我认为问题在于我如何设置我的Node和相关客户端(在内存中):

@BeforeMethod
    public void setup() {
        // set up elastic search to run locally. since the transaction
        // log needs a filesystem, we can't run it as purely in memory,
        // but we can set the data directories into "target", so that maven will
        // clean up after the fact: http://bit.ly/OTN7Qf
        Settings settings = ImmutableSettings.settingsBuilder()
                .put("node.http.enabled", true)
                .put("path.logs","target/elasticsearch/logs")
                .put("path.data","target/elasticsearch/data")
                .put("gateway.type", "none")
                .put("index.store.type", "memory")
                .put("index.number_of_shards", 1)
                .put("index.number_of_replicas", 1).build();

        node = NodeBuilder.nodeBuilder().local(true).settings(settings).node();
        client = node.client();
    }

Someone on the elastic search google group was kind enough to help me out here. 有弹性搜索谷歌小组的人非常友好地帮助我。 After submitting a document to an in-memory node, I need to refresh the index: 在将文档提交到内存节点后,我需要刷新索引:

        node.client().admin().indices().prepareRefresh().execute().actionGet();

calling refresh fixed the problem. 调用refresh修复了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM