# Java basics not working for me

**URL:** <https://discuss.elastic.co/t/java-basics-not-working-for-me/51273>\
**Category:** Elasticsearch\
**Created:** [May 30, 2016, 6:11am UTC](https://discuss.elastic.co/t/java-basics-not-working-for-me/51273 "2016-05-30T06:11:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![daveyx](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/daveyx/32/35298_2.png) [@daveyx](https://discuss.elastic.co/u/daveyx)\
**Post date:** [May 30, 2016, 6:11am UTC](https://discuss.elastic.co/t/java-basics-not-working-for-me/51273/1 "2016-05-30T06:11:57Z")

</div>

Hi,

i am trying out elasticsearch with the java api, but i dont get it to work. I can index documents, but searching for them does not work. I tried termQuery(), termsQuery() and matchQuery(). But documents are never returned.

What is wrong in the code below?

Kind regards  
David

```
 final Client client = TransportClient.builder().build()
		        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
		client.admin().indices().create(new CreateIndexRequest("myindex")).actionGet();

		final XContentBuilder product1 = XContentFactory.jsonBuilder().prettyPrint()
			    .startObject()
		        	.field("id", "prod1")
			        .field("name", "prod1")
			        .field("productUrl", "man-prod1")
			        .field("price", "9.99")
			    .endObject();

		final XContentBuilder product2 = XContentFactory.jsonBuilder().prettyPrint()
			    .startObject()
	        		.field("id", "prod2")
			        .field("name", "prod2")
			        .field("productUrl", "man-prod2")
			        .field("price", "19.99")
			    .endObject();
		
		IndexResponse response1 = client.prepareIndex("myindex", "products", "prod1")
		        .setSource(product1).get();
		System.out.println(response1.isCreated()); // gives true
		
		IndexResponse response2 = client.prepareIndex("myindex", "products", "prod2")
		        .setSource(product2).get();
		System.out.println(response2.isCreated()); // gives true
		
		final SearchResponse searchResponse = client.prepareSearch("myindex")
				.setTypes("products")
		        .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
		        .setQuery(QueryBuilders.termQuery("productUrl", "man-prod2"))
		        .execute()
		        .actionGet();
		
		final SearchHit[] searchHits = searchResponse.getHits().getHits();
```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [May 30, 2016, 6:45am UTC](https://discuss.elastic.co/t/java-basics-not-working-for-me/51273/2 "2016-05-30T06:45:44Z")

</div>

You need to refresh the index before being able to search for docs you just indexed.

Don't do that in PROD but just in the context of tests.

Also be careful with your analyzers. I think that your field `productUrl` is indexed as `man`, `prod2` so a term query with `man-prod2` won't match.

---

<div class="post-metadata">

**Author:** ![daveyx](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/daveyx/32/35298_2.png) [@daveyx](https://discuss.elastic.co/u/daveyx)\
**Post date:** [May 30, 2016, 7:03am UTC](https://discuss.elastic.co/t/java-basics-not-working-for-me/51273/3 "2016-05-30T07:03:12Z")

</div>

Thanks for the answer. After indexing the two documents, i do a  
`client.admin().indices().prepareRefresh("myindex").get();`

now termQuery() and matchQuery() give me the two documents.

so i should refresh the index always after indexing, right ?

should i also refresh the index after values of a document were modified?

Next step is to mark the productUrl as not indexed, so i don't want run into the issue you've described.

kind regards  
David

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [May 30, 2016, 7:38am UTC](https://discuss.elastic.co/t/java-basics-not-working-for-me/51273/4 "2016-05-30T07:38:43Z")

</div>

> so i should refresh the index always after indexing, right ?

Refreshes take place automatically every few seconds (configurable), but if you want recently indexed documents to be immediately searchable (like on your case above) an explicit refresh is necessary to guarantee that searches will include the recently indexed document(s).

---

<div class="post-metadata">

**Author:** ![daveyx](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/daveyx/32/35298_2.png) [@daveyx](https://discuss.elastic.co/u/daveyx)\
**Post date:** [May 30, 2016, 7:44am UTC](https://discuss.elastic.co/t/java-basics-not-working-for-me/51273/5 "2016-05-30T07:44:01Z")

</div>

thanks for answer.

so a refresh is also not neccessary after modification of data...

---

<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 5, 2017, 10:48pm UTC](https://discuss.elastic.co/t/java-basics-not-working-for-me/51273/6 "2017-07-05T22:48:11Z")

</div>


