Unable to query elasticsearch on complex objects

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Unable to query elasticsearch on complex objects

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

 2) 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 [hidden email].
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.
Reply | Threaded
Open this post in threaded view
|

Re: Unable to query elasticsearch on complex objects

karthick r
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 <a href="https://twitter.com/search-advanced" target="_blank" onmousedown="this.href='https://www.google.com/url?q\75https%3A%2F%2Ftwitter.com%2Fsearch-advanced\46sa\75D\46sntz\0751\46usg\75AFQjCNGHcCcOuf9LGm7Lt-ktAvx1v1liTg';return true;" onclick="this.href='https://www.google.com/url?q\75https%3A%2F%2Ftwitter.com%2Fsearch-advanced\46sa\75D\46sntz\0751\46usg\75AFQjCNGHcCcOuf9LGm7Lt-ktAvx1v1liTg';return true;">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: <a href="http://pastebin.com/ep0eBxXY" target="_blank" onmousedown="this.href='http://www.google.com/url?q\75http%3A%2F%2Fpastebin.com%2Fep0eBxXY\46sa\75D\46sntz\0751\46usg\75AFQjCNE30ByH_XpBAXTCtRILqZvphF7s7A';return true;" onclick="this.href='http://www.google.com/url?q\75http%3A%2F%2Fpastebin.com%2Fep0eBxXY\46sa\75D\46sntz\0751\46usg\75AFQjCNE30ByH_XpBAXTCtRILqZvphF7s7A';return true;">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

 2) 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.

<a href="http://localhost:9200/twitter/_search?q=hashtag_text:MH192&amp;pretty=true" target="_blank" onmousedown="this.href='http://www.google.com/url?q\75http%3A%2F%2Flocalhost%3A9200%2Ftwitter%2F_search%3Fq%3Dhashtag_text%3AMH192%26pretty%3Dtrue\46sa\75D\46sntz\0751\46usg\75AFQjCNEFd22C_XJvDkBj2Njcp6xsPF2oXg';return true;" onclick="this.href='http://www.google.com/url?q\75http%3A%2F%2Flocalhost%3A9200%2Ftwitter%2F_search%3Fq%3Dhashtag_text%3AMH192%26pretty%3Dtrue\46sa\75D\46sntz\0751\46usg\75AFQjCNEFd22C_XJvDkBj2Njcp6xsPF2oXg';return true;">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 [hidden email].
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.
Reply | Threaded
Open this post in threaded view
|

Re: Unable to query elasticsearch on complex objects

Binh Ly-2
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 [hidden email].
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.