# Java API search returns no results... (JUnit test)

**URL:** <https://discuss.elastic.co/t/java-api-search-returns-no-results-junit-test/3873>\
**Category:** Elasticsearch\
**Created:** [February 3, 2011, 12:35pm UTC](https://discuss.elastic.co/t/java-api-search-returns-no-results-junit-test/3873 "2011-02-03T12:35:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![stelios](https://avatars.discourse-cdn.com/v4/letter/s/8dc957/32.png) [@stelios](https://discuss.elastic.co/u/stelios)\
**Post date:** [February 3, 2011, 12:35pm UTC](https://discuss.elastic.co/t/java-api-search-returns-no-results-junit-test/3873/1 "2011-02-03T12:35:53Z")

</div>

Hi,

I'm using Java API in a JUnit test to evaluate elasticsearch.  
Problem is I can't get any search results...

getting document by id works fine but I see that the fields are not populated...even after explicitly mapping  
one the fields.  
is this the reason why I get totalHits=0? wrong mapping?

I can post the code if needed.

thanks  
stelios

P.S. I'm using 0.14.2 version

---

<div class="post-metadata">

**Author:** ![stelios](https://avatars.discourse-cdn.com/v4/letter/s/8dc957/32.png) [@stelios](https://discuss.elastic.co/u/stelios)\
**Post date:** [February 3, 2011, 1:07pm UTC](https://discuss.elastic.co/t/java-api-search-returns-no-results-junit-test/3873/2 "2011-02-03T13:07:36Z")

</div>

OK I don't get results since I don't have fields.

Is this the correct way of mapping the document I've indexed?

* * *

XContentBuilder data = XContentFactory.jsonBuilder()  
.startObject().startObject("properties")  
.startObject("name")  
.field("type", "string")  
.field("index", "not\_analyzed")  
.field("store", "yes")  
.endObject()  
.endObject().endObject();

```
    CreateIndexResponse mapping = client.admin().indices().create(
            createIndexRequest("resource").mapping("file", data)
    ).actionGet();

```

## and he is the indexed document

IndexResponse resp = client.prepareIndex("resource", "file", "1")  
.setSource(builder.startObject()  
.field("name", "findMe")  
.field("postDate", new Date())  
.field("message", "trying out Elastic Search")  
.endObject())  
.execute().actionGet();

Thanks  
Stelios

---

<div class="post-metadata">

**Author:** ![stelios](https://avatars.discourse-cdn.com/v4/letter/s/8dc957/32.png) [@stelios](https://discuss.elastic.co/u/stelios)\
**Post date:** [February 3, 2011, 3:42pm UTC](https://discuss.elastic.co/t/java-api-search-returns-no-results-junit-test/3873/3 "2011-02-03T15:42:49Z")

</div>

Problem was how I created the mapping, jsonBuilder...  
creating it as a string and putting it the same way works fine...

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [February 7, 2011, 6:05pm UTC](https://discuss.elastic.co/t/java-api-search-returns-no-results-junit-test/3873/4 "2011-02-07T18:05:20Z")

</div>

Hard to tell, if you can gist your simple test class, I can have a look.  
On Thursday, February 3, 2011 at 2:35 PM, stelios wrote:

> Hi,
> 
> I'm using Java API in a JUnit test to evaluate elasticsearch.  
> Problem is I can't get any search results...
> 
> getting document by id works fine but I see that the fields are not  
> populated...even after explicitly mapping  
> one the fields.  
> is this the reason why I get totalHits=0? wrong mapping?
> 
> I can post the code if needed.
> 
> thanks  
> stelios
> 
> P.S. I'm using 0.14.2 version
> 
> --  
> View this message in context: [http://elasticsearch-users.115913.n3.nabble.com/Java-API-search-returns-no-results-JUnit-test-tp2413072p2413072.html](http://elasticsearch-users.115913.n3.nabble.com/Java-API-search-returns-no-results-JUnit-test-tp2413072p2413072.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![stelios](https://avatars.discourse-cdn.com/v4/letter/s/8dc957/32.png) [@stelios](https://discuss.elastic.co/u/stelios)\
**Post date:** [February 9, 2011, 5:38pm UTC](https://discuss.elastic.co/t/java-api-search-returns-no-results-junit-test/3873/5 "2011-02-09T17:38:01Z")

</div>

yeah I had a wrong mapping there...  
Results are fine now

thanks  
stelios

On 7 February 2011 19:05, kimchy [via Elasticsearch Users] \<  
[ml-node+2445435-1952517049-341730@n3.nabble.com](mailto:ml-node+2445435-1952517049-341730@n3.nabble.com)\> wrote:

> Hard to tell, if you can gist your simple test class, I can have a look.
> 
> On Thursday, February 3, 2011 at 2:35 PM, stelios wrote:
> 
> Hi,
> 
> I'm using Java API in a JUnit test to evaluate elasticsearch.  
> Problem is I can't get any search results...
> 
> getting document by id works fine but I see that the fields are not  
> populated...even after explicitly mapping  
> one the fields.  
> is this the reason why I get totalHits=0? wrong mapping?
> 
> I can post the code if needed.
> 
> thanks  
> stelios
> 
> P.S. I'm using 0.14.2 version
> 
> --  
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/Java-API-search-returns-no-results-JUnit-test-tp2413072p2413072.html](http://elasticsearch-users.115913.n3.nabble.com/Java-API-search-returns-no-results-JUnit-test-tp2413072p2413072.html)[http://elasticsearch-users.115913.n3.nabble.com/Java-API-search-returns-no-results-JUnit-test-tp2413072p2413072.html?by-user=t](http://elasticsearch-users.115913.n3.nabble.com/Java-API-search-returns-no-results-JUnit-test-tp2413072p2413072.html?by-user=t)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).
> 
> * * *
> 
> If you reply to this email, your message will be added to the discussion  
> below:
> 
> [http://elasticsearch-users.115913.n3.nabble.com/Java-API-search-returns-no-results-JUnit-test-tp2413072p2445435.html](http://elasticsearch-users.115913.n3.nabble.com/Java-API-search-returns-no-results-JUnit-test-tp2413072p2445435.html)  
> To unsubscribe from Java API search returns no results... (JUnit test), click  
> here[http://elasticsearch-users.115913.n3.nabble.com/template/NamlServlet.jtp?macro=unsubscribe\_by\_code&node=2413072&code=c3RldG9naWFzQGdtYWlsLmNvbXwyNDEzMDcyfC0xMDc4NTcwNTM2](http://elasticsearch-users.115913.n3.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=2413072&code=c3RldG9naWFzQGdtYWlsLmNvbXwyNDEzMDcyfC0xMDc4NTcwNTM2).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 4:12am UTC](https://discuss.elastic.co/t/java-api-search-returns-no-results-junit-test/3873/6 "2017-07-06T04:12:18Z")

</div>


