Unable to query elasticsearch on complex objects


(karthick r) #1

Hi,
[ES VERSION: 1.0.1. Default mappings.]

I have an index which holds the twitter data. The user will have the option
to perform the https://twitter.com/search-advanced in the UI that I'm
building. Upon receiving the search request, I query the index and return
the results. [Source: http://pastebin.com/ep0eBxXY]

I have a couple of questions:

  1. How do we query complex objects? ie., if i am interested in a field
    which is within another object.
    Object A
    Field in Object A
  • Object B
    • Field in Object B
  1. For me, even the OR, AND and NOT queries doesn't seem to work. Am I
    missing anything?

              if(null!=request.getAllOfTheseWords()){
                String[] allWords = 
    

request.getAllOfTheseWords().split(",");
FilterBuilder[] allWordsFilter = new
FilterBuilder[allWords.length];
for(int i=0;i<allWords.length;i++){
String word = allWords[i];
allWordsFilter[i] =
FilterBuilders.termFilter("text", word);
}

srb.setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),
FilterBuilders.andFilter(allWordsFilter)));
}
else if(null!=request.getOrWords()){
String[] orWords = request.getOrWords().split(",");
FilterBuilder[] orFilters = new
FilterBuilder[orWords.length];
for(int i=0;i<orWords.length;i++){
String word = orWords[i];
orFilters[i] = FilterBuilders.termFilter("text",
word);
}

srb.setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),
FilterBuilders.orFilter(orFilters)));
}
else if(null!=request.getNoneOfTheseWords()){
String[] noneOfTheseWords =
request.getNoneOfTheseWords().split(",");
FilterBuilder[] noneOfTheseWordsFilter = new
FilterBuilder[noneOfTheseWords.length];
for(int i=0;i<noneOfTheseWords.length;i++){
String word = noneOfTheseWords[i];
noneOfTheseWordsFilter[i] =
FilterBuilders.termFilter("text", word);
}

srb.setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),
FilterBuilders.notFilter(FilterBuilders.orFilter(noneOfTheseWordsFilter))));
}
else if(null!=request.getHashTags()){
String[] hastags = request.getHashTags().split(",");
FilterBuilder[] hashTagFilter = new
FilterBuilder[hastags.length];
for(int i=0;i<hastags.length;i++){
String hashtag = hastags[i];
hashTagFilter[i] =
FilterBuilders.nestedFilter("hashtag",
FilterBuilders.termFilter("hashtag_text", hashtag));
}

srb.setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),
FilterBuilders.orFilter(hashTagFilter)));
}
else if(null!= request.getFromUser()){

srb.setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),
FilterBuilders.termFilter("screen_name", request.getFromUser())));
}
else if(null!=request.getSince() &&
null!=request.getUntil()){

srb.setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),
FilterBuilders.rangeFilter("created_at").from(request.getSince()).to(request.getUntil())));
}
}

However this works fine.

http://localhost:9200/twitter/_search?q=hashtag_text:MH192&pretty=true

Please help me out.

Thanks

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/55f1e4b5-a521-4130-b902-1ce03992402f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(karthick r) #2

Any updates?

On Monday, April 21, 2014 2:14:07 PM UTC+5:30, karthick r wrote:

Hi,
[ES VERSION: 1.0.1. Default mappings.]

I have an index which holds the twitter data. The user will have the
option to perform the https://twitter.com/search-advanced in the UI that
I'm building. Upon receiving the search request, I query the index and
return the results. [Source: http://pastebin.com/ep0eBxXY]

I have a couple of questions:

  1. How do we query complex objects? ie., if i am interested in a field
    which is within another object.
    Object A
    Field in Object A
  • Object B
    • Field in Object B
  1. For me, even the OR, AND and NOT queries doesn't seem to work. Am I
    missing anything?

              if(null!=request.getAllOfTheseWords()){
                String[] allWords = 
    

request.getAllOfTheseWords().split(",");
FilterBuilder[] allWordsFilter = new
FilterBuilder[allWords.length];
for(int i=0;i<allWords.length;i++){
String word = allWords[i];
allWordsFilter[i] =
FilterBuilders.termFilter("text", word);
}

srb.setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),
FilterBuilders.andFilter(allWordsFilter)));
}
else if(null!=request.getOrWords()){
String[] orWords = request.getOrWords().split(",");
FilterBuilder[] orFilters = new
FilterBuilder[orWords.length];
for(int i=0;i<orWords.length;i++){
String word = orWords[i];
orFilters[i] = FilterBuilders.termFilter("text",
word);
}

srb.setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),
FilterBuilders.orFilter(orFilters)));
}
else if(null!=request.getNoneOfTheseWords()){
String[] noneOfTheseWords =
request.getNoneOfTheseWords().split(",");
FilterBuilder[] noneOfTheseWordsFilter = new
FilterBuilder[noneOfTheseWords.length];
for(int i=0;i<noneOfTheseWords.length;i++){
String word = noneOfTheseWords[i];
noneOfTheseWordsFilter[i] =
FilterBuilders.termFilter("text", word);
}

srb.setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),
FilterBuilders.notFilter(FilterBuilders.orFilter(noneOfTheseWordsFilter))));
}
else if(null!=request.getHashTags()){
String[] hastags = request.getHashTags().split(",");
FilterBuilder[] hashTagFilter = new
FilterBuilder[hastags.length];
for(int i=0;i<hastags.length;i++){
String hashtag = hastags[i];
hashTagFilter[i] =
FilterBuilders.nestedFilter("hashtag",
FilterBuilders.termFilter("hashtag_text", hashtag));
}

srb.setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),
FilterBuilders.orFilter(hashTagFilter)));
}
else if(null!= request.getFromUser()){

srb.setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),
FilterBuilders.termFilter("screen_name", request.getFromUser())));
}
else if(null!=request.getSince() &&
null!=request.getUntil()){

srb.setQuery(QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),
FilterBuilders.rangeFilter("created_at").from(request.getSince()).to(request.getUntil())));
}
}

However this works fine.

http://localhost:9200/twitter/_search?q=hashtag_text:MH192&pretty=true

Please help me out.

Thanks

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/f7201ffd-b481-4e70-adc4-21a451f37036%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(Binh Ly-2) #3

ES flattens out your inner objects/fields into individual fields. So for
example, you can reference any inner object field using the fully qualified
path to it, like:

{ "a": { "b": { "c": "value" } } }

And the c field can be accessed using the following field name:

"a.b.c"

And you can do a match, term, or any other query/filter using the above
field name.

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/609e7546-79f9-472b-9149-198aedd7ca21%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(system) #4