How to get the searched items?


(cheepu) #1

Hi
I created a JSON Document as follows:

JSONArray jArray = new JArray();
jArray.add("jhon");
jArray.add("jerry");
jArray.add("sam");

JSONObject jObject = new JSONObject();
jObject.add("first",jArray);

For Searching I am writing following code:

SearchResponse searchResponse = client.prepareSearch("twitter")
.setSearchType(SearchType.DEFAULT)
.setQuery(wildcardQuery("first","j*"))
.setFrom(0).setSize(60).setExplain(true)
.execute()
.actionGet();
SearchHits sh = searchResponse.getHits();
System.out.println("Total Hits are sh.totalHits() : "+sh.hits().length);

I have not done any configuration what ever is default.
My Problems are:

  1. I am getting only 1 hit but it should give 2 because (as I understand) j* will search jhon and jerry
  2. How can i get the value of the hit( for eg if jhon is found then how can i get printed jhon)
  3. If i make any change in the JSONObject and run the program the changes are not reflected in one time i need to run once again.

I have spent lots of time in finding the reasons for these but I could not get.
Please help to resolve the problem
May be title doesnot suite the problem, sorry for that

Thanks


(Shay Banon) #2

On Sat, Dec 18, 2010 at 4:31 PM, cheepu cheeputech@gmail.com wrote:

Hi
I created a JSON Document as follows:

JSONArray jArray = new JArray();
jArray.add("jhon");
jArray.add("jerry");
jArray.add("sam");

JSONObject jObject = new JSONObject();
jObject.add("first",jArray);

For Searching I am writing following code:

SearchResponse searchResponse = client.prepareSearch("twitter")
.setSearchType(SearchType.DEFAULT)
.setQuery(wildcardQuery("first","j*"))
.setFrom(0).setSize(60).setExplain(true)
.execute()
.actionGet();
SearchHits sh = searchResponse.getHits();
System.out.println("Total Hits are sh.totalHits() :
"+sh.hits().length);

I have not done any configuration what ever is default.
My Problems are:

  1. I am getting only 1 hit but it should give 2 because (as I understand)
    j*
    will search jhon and jerry

Each document you index is a hit. You might want to index a document per
name in this case.

  1. How can i get the value of the hit( for eg if jhon is found then how can
    i get printed jhon)

From the SearchHits, you can get a SearchHit, and then get back the actual
raw bytes, or a parsed map (of maps) representing the json structure.

  1. If i make any change in the JSONObject and run the program the changes
    are not reflected in one time i need to run once again.

You need to reindex the data. If you want to update a document, make sure
you use the same id. Note that search visibility is near real time, you can
issue a refresh command (client.admin.indices.prepareRefresh) to make sure
you search on what was indexed up to that point.

I have spent lots of time in finding the reasons for these but I could not
get.
Please help to resolve the problem
May be title doesnot suite the problem, sorry for that

Thanks

--
View this message in context:
http://elasticsearch-users.115913.n3.nabble.com/How-to-get-the-searched-items-tp2111149p2111149.html
Sent from the ElasticSearch Users mailing list archive at Nabble.com.


(system) #3