简体   繁体   中英

Elasticsearch not returning any hits in Java search API

I'm stuck with elasticsearch search API. Can anyone tell me what am I doing wrong here? Can't make search API working

I use test classes from elasticsearch 1.0.1

1.Get the client

Client testClient = ElasticsearchIntegrationTest.client();

2.Insert some data

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

3.Get inserted data (this works)

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

4.Search for inserted data (this doesn't work).

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

I've tried all sorts of QueryBuilders with no luck. Number of hits returned is always zero.

Solved it, I had to refresh the indices for search to work

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

First of all .setFrom() is used to declare an offset, for exemple setFrom(10) will skip the first 10 documents return. Your setFrom is useless. I do not know whats is doing .setExplain does but maybe you should try a more simple request like following :

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

Then could you give use your mapping ? Are you sure your documents is well stored in ElasticSearch ( use elasticsearch-head if you do not)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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