elasticsearch java API:matchAll搜索查询不返回结果? [英] elasticsearch java API: matchAll search query doesn't return results?
本文介绍了elasticsearch java API:matchAll搜索查询不返回结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
//首先尝试获取请求,确保索引中有一些
GetResponse results = client.prepareGet(INDEX_NAME,INDEX_TYPE,testID)
.execute()
.actionGet();
//这个断言成功了,正如我们预期的那样。
assertThat(results.getId())。isEqualTo(testID);
//下一步,尝试最简单的搜索
SearchResponse s1 = client.prepareSearch(INDEX_NAME).setQuery(matchAllQuery())
.execute()
。 actionGet();
//此断言失败。为什么?答案:当我们有一个内存中的节点时,我们必须
//在提交文档之后手动调用刷新。
assertThat(s1.getHits()。totalHits())。isGreaterThanOrEqualTo(1);
经过一些测试,我认为问题出在我如何设置我的Node和关联的客户端在记忆中):
@BeforeMethod
public void setup(){
//设置弹性搜索在本地运行因为事务
// log需要一个文件系统,所以我们不能像内存中的
//那样运行它,但是我们可以将数据目录设置为target,所以maven将
//清理后事实:http://bit.ly/OTN7Qf
设置设置= 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();
}
解决方案
有人在弹性搜索google小组很友善,可以帮助我。在将内容提交到内存节点后,我需要刷新索引:
node.client()。admin ).indices()prepareRefresh()执行()actionGet()。;
调用刷新修复问题。
I've got an in-memory instance of elastic search running, and doing some exploratory coding to learn the search 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.
// 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):
@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.
这篇关于elasticsearch java API:matchAll搜索查询不返回结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文