-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using a filtered query is generating an empty filter array #115
Comments
Hi there! You tried your best, so no need to apologize. :-) I think there is a simple solution. Can you replace andFilter.Add(termFilter_Timestamps) with andFilter = andFilter.Add(termFilter_Timestamps) The reason why you need to use it as a chain is that Cheers. |
Of course! I should have seen that, it was even in the example you posted for issue #105. Thanks so much for the response, I really appreciate it. If I could dig in, now that everything is being added to its respective filter object properly, I happened to notice that the generated output now looks like:
... which of course fails because there's not supposed to be a (I realize this bends the original issue, and I'd be happy to take my question to a different spot if you'd prefer.) Thanks. |
Sorry, I'm on the road... you could use a BoolFilter with "must" clauses instead. Please try to find the related unit test for BoolFilter so see a usage example. |
Hey, thanks again for the feedback. I ended up figuring out the issue (mine, of course) and got everything working. Just in case anyone digging around for something similar happens to find this, here's where I got tripped up. As expected, it has to do with me fumbling through some of the interesting parts of Go. In the code that I originally posted, I had these two lines: termFilter_Timestamps := elastic.NewTermsFilter("utc_unix_timestamp", utc_unix_timestamps)
termFilter_ExperienceIds := elastic.NewTermsFilter("experience_id", experience_ids) ... however the definition of the NewTermsFilter looks like: func NewTermsFilter(name string, values ...interface{}) TermsFilter {
f := TermsFilter{
name: name,
values: make([]interface{}, 0),
}
f.values = append(f.values, values...)
return f
} ... which you'll note takes a variadic values arguments and not a slice values argument -- I learned a bit more about that here. When I changed my code to: experience_ids_as_interfaces := make([]interface{}, len(experience_ids))
for i, v := range experience_ids {
experience_ids_as_interfaces[i] = v
}
utc_unix_timestamps_as_interfaces := make([]interface{}, len(utc_unix_timestamps))
for i, v := range utc_unix_timestamps {
utc_unix_timestamps_as_interfaces[i] = v
}
termFilter_Timestamps := elastic.NewTermsFilter("utc_unix_timestamp", utc_unix_timestamps_as_interfaces...)
termFilter_ExperienceIds := elastic.NewTermsFilter("experience_id", experience_ids_as_interfaces...) ... all became right with the world (it compiled), and my query began returning the expected results. The final code that I ended up using looks like: experience_ids_as_interfaces := make([]interface{}, len(experience_ids))
for i, v := range experience_ids {
experience_ids_as_interfaces[i] = v
}
utc_unix_timestamps_as_interfaces := make([]interface{}, len(utc_unix_timestamps))
for i, v := range utc_unix_timestamps {
utc_unix_timestamps_as_interfaces[i] = v
}
matchAllQuery := elastic.NewMatchAllQuery()
termFilter_Timestamps := elastic.NewTermsFilter("utc_unix_timestamp", utc_unix_timestamps_as_interfaces...)
termFilter_ExperienceIds := elastic.NewTermsFilter("experience_id", experience_ids_as_interfaces...)
andFilter := elastic.NewAndFilter()
andFilter = andFilter.Add(termFilter_Timestamps)
andFilter = andFilter.Add(termFilter_ExperienceIds)
andFilter = andFilter.Cache(true)
filteredQuery := elastic.NewFilteredQuery(matchAllQuery).Filter(andFilter) I still need to figure out if that's actually a performant way for me to have solved the problem, but that's a totally different issue for a totally different forum. :) Thanks again for the assist, it put me on the right path. |
I'm trying to build a filtered query (that I have working when just issuing curls against my elasticsearch cluster) using the elastic library, but I can't seem to get my "and" filter to be respected.
I searched the docs and issues and while I found a question about range / terms filters (#105) that sort of discussed using a filtered query I wasn't able to find specific examples for doing so.
Here's my elasticsearch query:
... and I'm attempting to recreate it thusly (based on code from @olivere found in the above linked issue)
(The variables
utc_unix_timestamps
andexperience_ids
contain slices populated just as in my sample query above.)... but I've been getting a result set that has all records from my dataset in it. So I investigated with:
... and discovered that the source looks like:
I am a newb in both Go and using this library, so I apologize in advance if I missed some obvious documentation. I'm curious if anyone can help me understand how to get my filter respected by the query.
The text was updated successfully, but these errors were encountered: