# Problem to search field data using Java API?

**URL:** https://discuss.elastic.co/t/problem-to-search-field-data-using-java-api/3675
**Category:** Elasticsearch
**Created:** [December 17, 2010, 9:20am UTC](https://discuss.elastic.co/t/problem-to-search-field-data-using-java-api/3675 "2010-12-17T09:20:33Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Sam\_4](https://avatars.discourse-cdn.com/v4/letter/s/db5fbb/32.png) [@Sam\_4](https://discuss.elastic.co/u/Sam_4)
#### Post date: [December 17, 2010, 9:20am UTC](https://discuss.elastic.co/t/problem-to-search-field-data-using-java-api/3675/1 "2010-12-17T09:20:33Z")

</div>

Hi

I am new to ES using 0.13.0 version with Java API. i am write the  
following code :

package com.example.elastic;

import org.elasticsearch.index.query.xcontent.XContentQueryBuilder;  
import org.elasticsearch.index.query.QueryBuilder;  
import org.elasticsearch.search.SearchHits;  
import org.elasticsearch.action.search.SearchType;  
import org.elasticsearch.action.search.SearchResponse;  
import org.elasticsearch.action.count.CountResponse;  
import org.elasticsearch.action.get.GetResponse;  
import org.apache.log4j.Logger;  
import org.apache.log4j.xml.DOMConfigurator;  
import java.util.Date;  
import java.io.IOException;  
import org.elasticsearch.action.index.IndexResponse;  
import org.elasticsearch.client.Client;  
import org.elasticsearch.node.Node;  
import static org.elasticsearch.node.NodeBuilder._;  
import static org.elasticsearch.common.xcontent.XContentFactory._;  
import static org.elasticsearch.index.query.xcontent.QueryBuilders.\*;

public class Example1 {

```
Logger logger;

Example1()
{

```

DOMConfigurator.configure(getClass().getClassLoader().getResource("resources/  
log4j.xml"));  
logger =Logger.getLogger(Example1.class);  
logger.info("\*\*\*\*\* In the Example1() constructor");

```
} // end Constructor Example1()

void setup() throws IOException
{
    Node node = nodeBuilder().node();
    Client client = node.client();

    IndexResponse indexResponse = client.prepareIndex("twitter",

```

"tweet", "1")  
.setSource(jsonBuilder()  
.startObject()  
.field("first", "sam")  
.field("last","user")  
.field("msg", "hello")  
.field("msg", "hello")  
.endObject()  
)  
.execute()  
.actionGet();

```
    logger.info(" ******* Creting Json Document Object

```

\*\*\*\*\*\*\*\*\*\*");  
GetResponse getResponse = client.prepareGet("twitter",  
"tweet", "1")  
.execute()  
.actionGet();  
logger.info("Does this Exists: "+getResponse.isExists());

```
     logger.info(" ******* Creting SearchResponse Object

```

\*\*\*\*\*\*\*\*\*\*");  
XContentQueryBuilder qb1 = termQuery("msg", "hello");

```
    SearchResponse searchResponse =

```

client.prepareSearch("twitter")  
.setSearchType(SearchType.DEFAULT)  
.setQuery(qb1)  
.setFrom(0).setSize(60).setExplain(true)  
.execute()  
.actionGet();  
CountResponse countResponse = client.prepareCount("twitter")  
.setQuery(qb1)  
.execute()  
.actionGet();  
logger.info("Total Number of Count is :  
"+countResponse.count());  
SearchHits sh = searchResponse.getHits();  
logger.info("Total Hits are : "+sh.totalHits());  
node.close();  
logger.info("Closing node");

```
} // end of setup()

public static void main(String[] s){
   Example1 ex = new Example1();
   try{ ex.setup(); } catch(Exception e)

```

{ e.printStackTrace(); }  
} // end of main()  
} // end of class

when i execute the following code,i got the following problem which is  
pointed out below:

1. first time when i execute program i got hitcount and count value  
0,when i tried to execute again it got the value for hit count and  
count 1.

2.I created JSon Document with filed first,last,msg.here msg is  
repeated twice when it search the msg field

```
               XContentQueryBuilder qb1 = termQuery("msg",

```

"hello");

i got only one hit count and count,which is wrong.

Please any one guide me to solve my problem where the problem.

Thanks

---

<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: [December 19, 2010, 12:54am UTC](https://discuss.elastic.co/t/problem-to-search-field-data-using-java-api/3675/2 "2010-12-19T00:54:13Z")

</div>

ES is near real time, this menas that when you index a document, you won't  
see it in the result only after a scheduled interval. You can issue a  
refresh request (or set the refresh flag to true on the index request) if  
you want to see it immediately, but it is not recommended to be used in  
heavy indexing scenarios (setting the refresh flag).

On Fri, Dec 17, 2010 at 11:20 AM, sam [mishra.sameek@gmail.com](mailto:mishra.sameek@gmail.com) wrote:

> Hi
> 
> I am new to ES using 0.13.0 version with Java API. i am write the  
> following code :
> 
> package com.example.elastic;
> 
> import org.elasticsearch.index.query.xcontent.XContentQueryBuilder;  
> import org.elasticsearch.index.query.QueryBuilder;  
> import org.elasticsearch.search.SearchHits;  
> import org.elasticsearch.action.search.SearchType;  
> import org.elasticsearch.action.search.SearchResponse;  
> import org.elasticsearch.action.count.CountResponse;  
> import org.elasticsearch.action.get.GetResponse;  
> import org.apache.log4j.Logger;  
> import org.apache.log4j.xml.DOMConfigurator;  
> import java.util.Date;  
> import java.io.IOException;  
> import org.elasticsearch.action.index.IndexResponse;  
> import org.elasticsearch.client.Client;  
> import org.elasticsearch.node.Node;  
> import static org.elasticsearch.node.NodeBuilder._;  
> import static org.elasticsearch.common.xcontent.XContentFactory._;  
> import static org.elasticsearch.index.query.xcontent.QueryBuilders.\*;
> 
> public class Example1 {
> 
> Logger logger;
> 
> Example1()  
> {
> 
> DOMConfigurator.configure(getClass().getClassLoader().getResource("resources/  
> log4j.xml"));  
> logger =Logger.getLogger(Example1.class);  
> logger.info("\*\*\*\*\* In the Example1() constructor");
> 
> } // end Constructor Example1()
> 
> void setup() throws IOException  
> {  
> Node node = nodeBuilder().node();  
> Client client = node.client();
> 
> ```
> IndexResponse indexResponse = client.prepareIndex("twitter",
> 
> ```
> 
> "tweet", "1")  
> .setSource(jsonBuilder()  
> .startObject()  
> .field("first", "sam")  
> .field("last","user")  
> .field("msg", "hello")  
> .field("msg", "hello")  
> .endObject()  
> )  
> .execute()  
> .actionGet();
> 
> ```
> logger.info(" ******* Creting Json Document Object
> 
> ```
> 
> \*\*\*\*\*\*\*\*\*\*");  
> GetResponse getResponse = client.prepareGet("twitter",  
> "tweet", "1")  
> .execute()  
> .actionGet();  
> logger.info("Does this Exists: "+getResponse.isExists());
> 
> ```
> logger.info(" ******* Creting SearchResponse Object
> 
> ```
> 
> \*\*\*\*\*\*\*\*\*\*");  
> XContentQueryBuilder qb1 = termQuery("msg", "hello");
> 
> ```
> SearchResponse searchResponse =
> 
> ```
> 
> client.prepareSearch("twitter")  
> .setSearchType(SearchType.DEFAULT)  
> .setQuery(qb1)  
> .setFrom(0).setSize(60).setExplain(true)  
> .execute()  
> .actionGet();  
> CountResponse countResponse = client.prepareCount("twitter")  
> .setQuery(qb1)  
> .execute()  
> .actionGet();  
> logger.info("Total Number of Count is :  
> "+countResponse.count());  
> SearchHits sh = searchResponse.getHits();  
> logger.info("Total Hits are : "+sh.totalHits());  
> node.close();  
> logger.info("Closing node");
> 
> } // end of setup()
> 
> public static void main(String s){  
> Example1 ex = new Example1();  
> try{ ex.setup(); } catch(Exception e)  
> { e.printStackTrace(); }  
> } // end of main()  
> } // end of class
> 
> when i execute the following code,i got the following problem which is  
> pointed out below:
> 
> 1. first time when i execute program i got hitcount and count value  
> 0,when i tried to execute again it got the value for hit count and  
> count 1.
> 
> 2.I created JSon Document with filed first,last,msg.here msg is  
> repeated twice when it search the msg field
> 
> ```
> XContentQueryBuilder qb1 = termQuery("msg",
> 
> ```
> 
> "hello");
> 
> i got only one hit count and count,which is wrong.
> 
> Please any one guide me to solve my problem where the problem.
> 
> Thanks

---

<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:15am UTC](https://discuss.elastic.co/t/problem-to-search-field-data-using-java-api/3675/3 "2017-07-06T04:15:11Z")

</div>


