Elasticsearch在Java搜索API中没有返回任何匹配

时间:2014-03-12 13:35:28

标签: java elasticsearch

我坚持使用elasticsearch搜索API。谁能告诉我这里我做错了什么?无法使搜索API正常工作

我使用elasticsearch 1.0.1的测试类

1.获取客户

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但没有运气。返回的命中数始终为零。

2 个答案:

答案 0 :(得分:4)

解决了它,我不得不刷新搜索工作的索引

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

答案 1 :(得分: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-head)