2014-03-12 65 views
3

我坚持使用elasticsearch搜索API。谁能告诉我我在这里做错了什么?不能使搜索API工作Elasticsearch不返回Java搜索API中的任何匹配

我使用的测试类从elasticsearch 1.0.1

1.Get客户

Client testClient = ElasticsearchIntegrationTest.client(); 

2.插入一些数据

client.prepareIndex("elastic_index", "elastic_type", "1") 
     .setSource(jsonBuilder() 
       .startObject() 
       .field("ID", "1") 
       .field("value", "big") 
       .endObject()) 
     .execute() 
     .actionGet(); 

3。获取插入数据(此作品)

GetResponse response = client.prepareGet("elastic_index", "elastic_type", "1") 
     .execute() 
     .actionGet(); 

4.搜索插入的数据(这不起作用)。

SearchResponse searchResponse = client.prepareSearch("elastic_index") 
     .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) 
     .setQuery(matchQuery("value", "big")) 
     .setFrom(0).setSize(60).setExplain(true) 
     .setTypes("elastic_type") 
     .execute() 
     .actionGet(); 

我试过各种QueryBuilders没有运气。返回的匹配数总是零。

回答

4

解决了这个问题,我不得不刷新索引搜索工作

client.admin() 
    .indices() 
    .prepareRefresh() 
    .execute() 
    .actionGet(); 
+0

当从一个集成测试中运行('ESIntegTestCase'),'this.refresh()'也一样。 –

0

首先.setFrom()用于声明一个偏移量,例如setFrom(10)会跳过前10个文件返回。你的setFrom是无用的。 我不知道什么是做.setExplain做,但也许你应该尝试像下面一个更简单的要求:

SearchResponse searchResponse = client.prepareSearch("elastic_index") 
      .setTypes("elastic_type") 
      .setSearchType(SearchType.QUERY_THEN_FETCH) 
      .setQuery(matchQuery("value", "big")).execute().actionGet(); 

然后你可以给使用映射?你确定你的文件很好地保存在ElasticSearch(使用elasticsearch头,如果你不这样做)

相关问题