gpt4 book ai didi

java - Elasticsearch java API : matchAll search query doesn't return results?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:06:47 24 4
gpt4 key购买 nike

我有一个内存中的 Elasticsearch 实例正在运行,并进行了一些探索性编码以学习搜索 Java API。我能够将文档提交到索引并使用 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);

经过一些测试,我认为问题在于我如何设置我的节点和关联的客户端(在内存中):

@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();
}

最佳答案

elastic search google group 中的某个人很友好地帮助了我。将文档提交到内存节点后,我需要刷新索引:

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

调用刷新解决了这个问题。

关于java - Elasticsearch java API : matchAll search query doesn't return results?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12692758/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com