Problem to search field data using Java API?


(Sam-4) #1

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


(Shay Banon) #2

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 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


(system) #3