Elasticsearch: include specific facet values - elasticsearch

elasticsearch provides parameter to exclude certain facets from facets values like this.
"facets" : {
"tag" : {
"terms" : {
"field" : "tag",
"exclude" : ["term1", "term2"]
}
}
}
Is there any possibility to include certain facets?
I'm trying to get counts for facets that have been already selected by user along with global facets. E,g. you selected word science with count 20 (from autocomplete), i recompute facets to show other words that migh be selected, but the word science would not get to facet results since other words from global facets have count more than 400.
Is there any particular solution for this task?
Thanks for help

You can use scripting for that. The script will be run for each facet entry with the input variable term that contains the current value. The entry will be included or not on the final facet depending on the result of the script. If it returns false it will be excluded, otherwise it will be included.
"facets" : {
"tag" : {
"terms" : {
"field" : "tag",
"script" : "term == 'aaa' ? true : false"
}
}
}

Related

applying query on aggregation results in elasticsearch?

I have an index in ES which has multiple documents. There is a field software_tags for which I wanted to have unique values. I applied term aggregation for that purpose as below:
GET /record_new/_search
{"size":0,
"aggs" : {
"software_tags" : {
"terms" : {
"field" : "software_tags.keyword",
"size" : 10000,
"order" : { "_term" : "asc" }
}
}
}
}
Now once I have all the unique values, I want to use these values as a search functionality, as in, if we have software_tags with "Windows" word in it, I want that the user gets all those tags when he searches as any of such combinations as in:
win, Windows, window, Window, windows
Kind of a search functionality. So basically I want to apply a case insensitive query to search on the aggregate results. How can this be done.

Use of field and script in Facet

I am new in elastic search. I am trying to understand this query but I could not succed in few things like field and script. I read official documents and get that Facet has been removed my aggregation and Attributelabels is a facet name but I could not understand full query. Can anyone explain it to me ?
Thank you
{
"size" : 0,
"facets" : {
"AttributeLabels" : {
"terms" : {
"field" : "field",
"size" : 50,
"script" : "scriptName",
"lang" : "lang"
}
}
}
}
https://www.elastic.co/guide/en/elasticsearch/reference/1.4/search-facets-terms-facet.html
Field - The field that the facet is evaluated on
Size - The number of top terms that are returned
Script - Either returns a string "term + 'aaa'" which becomes the term that is evaluated on or a boolean "term == 'aaa' ? true : false" which includes or excludes it from the facet collection
Lang - The scripting language used
But if you can, I recommend upgrading to Elasticsearch 2. :)

ElasticSearch aggregation: exclude one filter per aggregation

I want to filter out documents whose field 'A' is equal to 'a', and I want to facet the field 'A' at the same time, excluding of course the previous filter.
I know that you can put the filter 'outside' the query in order to get the facets without that filter applied, like:
ElasticSearch
{
"query : { "match_all" : { } },
"filter" : { "term : { "A" : "a" } },
"facets" : {
"A" : { "terms" : { "field" : "A" } } //this should exclude the filter A:a
}
}
SOLR
&q=:*:*
&fq={!tag=Aa}A:a
&facet=true&facet.field={!ex=Aa}A
This is very nice, but what happens if i have multiple filters and facets that each one should exclude each other?
Example:
filter=A:a
filter=B:b
filter=C:c
facet={exclude filter A:a}A
facet={exclude filter B:b}B
facet={exclude filter C:c}C
That is, for facet A I want to keep all filters except A:a, for facet B all except B:b, and so on.
The most obvious way would be to do n queries (one per each of the n facets), but I'd like to stay away from that.
The global scope provides access to every document, you can then add the same filters you used for the main query.
I gave an example with global scope in this related topic
Could you give any feedback about performance issue with post_filter ?

elasticsearch number of facets returned

I have faceted queries working with elasticsearch 0.19.9. However I would like to return all facets that have a count > 0.
According to the documentation I should be able to:
{
"query" : {
"match_all" : { }
},
"facets" : {
"tag" : {
"terms" : {
"field" : "tag",
"all_terms" : true
}
}
}
}
As I understand, this should give me all facets even if count is 0.
However, this is still only returning the top 10 facets by count. Which is the default size. The only thing that seems to affect the number of returned facets is by actually setting "size" : N where N is the number of facets which will be returned.
I could set this to a really high number but that just seems to hack-ish.
Any ideas as to what I may be doing wrong?
You're not doing anything wrong. You figured it out correctly! There is an open issue on github to make the terms facet similar to the Terms Stats facet which allows you to set size=0 in order to get all the terms back. For now you just need to use an high value, which is a bit tricky, I agree. On the other hand be careful not to return too many entries!
{
"query" : {
"match_all" : { }
},
"facets" : {
"tag" : {
"terms" : {
"field" : "tag",
"size" : 2147483647,
"all_terms" : false
}
}
}
}
The only way to remove the "count: 0" is to put "all_terms" as false, and set your size number as high and as impossible as you can in your Elasticsearch instance (the example above is the largest signed value that an integer in PHP can have).
It may not be the best way, but this is the only known approach so far. Facet filter doesn't work with this at present (unless they updated and improved Elasticsearch to do it).

elastic search faceted query returns incorrect count

I need help in aggregate / faceted queries in elastic search. I have used faceted query to group the results but I’m not getting grouped result with correct count.
Please suggest on how to get grouped results from elastic search.
{
"query" : {
"query_string" : {"query" : "pared_cat_id:1"} } ,
"facets" : {
"subcategory" : {
"terms" : {
"field": "sub_cat_id",
"size" : 50,
"order" : "term",
"all_terms" : true
}
}
},
"from" : 0,
"size": 50
}
Trying to get grouped results for sub category id for passed parent category id.
"query_string" : {"query" : "pared_cat_id:1"} } ,
This is applied to overall data and not on the facets counts.
FOr this you need to use facet query in which you can specify same which you are specifying in the main query string.
So facets count which are being shown to you now are based on the results without applying "query_string" : {"query" : "pared_cat_id:1"} } , ie. to the whole data. Incase you want facets counts after applying "query_string" : {"query" : "pared_cat_id:1"} } , provide it in the facet query.
Elasticsearch faceting queries works very well in terms of accuracy, at least I have not seen any problem yet.
Just a few questions:
What field is this string or numeric,give example?
Have you applied any custom mapping or you have used default "standard" analyzer
Please state the kind of inaccuracy like "aa" should have count 100 but its 50 or is there any other kind of inaccuracy?
Elasticsearch facets query returns incorrect count if the number of shards is >1, so as for now Facets are deprecated and will be removed in a future release. You are encouraged to migrate to aggregations instead.
I suggest that you take a look at this blog post in which Alex Brasetvik give a good description along with some examples on how to use the aggregations feature properly.

Resources