Using Facet filter in array

I have an elasticsearch document named "user", which contains an array
field - "companies". I want to get all the company names that contains
"xyz" in their name. I have set the custom mapping which tokenizes the data
in the companies array.
Below is the mapping defined for companies array:

field :companies, type: 'string', index: 'analyzed', analyzer:
'name_analyzer' do
field :companies_lower, type: 'string', index: 'analyzed', analyzer:
'lower_keyword'
field :companies_untouched, type: 'string', index: 'not_analyzed'
end

(name_analyzer is the custom analyzer defined by me)

I have these two ES documents:

{"companies":["xyzz","abc"]}
{"companies":["abcd","abcde"]}

Now, when I perform this query:

{"query"=>{"match_all"=>{}},
"facets"=>
{"tags"=>
{"terms"=>{"field"=>"companies_untouched"},
"facet_filter"=>{"term"=>{"companies"=>"xyz"}}}}}

This is the result that I get:

{"tags"=>
{"_type"=>"terms",
"missing"=>0,
"total"=>3,
"other"=>0,
"terms"=>
[{"term"=>"xyzz", "count"=>1},
{"term"=>"abc", "count"=>1}]}}

But I need only the matching result like below:

{"tags"=>
{"_type"=>"terms",
"missing"=>0,
"total"=>3,
"other"=>0,
"terms"=>
[{"term"=>"xyzz", "count"=>1}]}}

i.e the results should only contain the array values that are matching my
search query. Is there something wrong that I am doing here?

Thanks,
Aash

--

You can filter out unwanted facet values using "Regex Patterns" or "Term
Scripts"
see Elasticsearch Platform — Find real-time answers at scale | Elastic
for more information.

On Wednesday, January 2, 2013 3:38:42 AM UTC-5, geeky_sh wrote:

I have an elasticsearch document named "user", which contains an array
field - "companies". I want to get all the company names that contains
"xyz" in their name. I have set the custom mapping which tokenizes the data
in the companies array.
Below is the mapping defined for companies array:

field :companies, type: 'string', index: 'analyzed', analyzer:
'name_analyzer' do
field :companies_lower, type: 'string', index: 'analyzed', analyzer:
'lower_keyword'
field :companies_untouched, type: 'string', index: 'not_analyzed'
end

(name_analyzer is the custom analyzer defined by me)

I have these two ES documents:

{"companies":["xyzz","abc"]}
{"companies":["abcd","abcde"]}

Now, when I perform this query:

{"query"=>{"match_all"=>{}},
"facets"=>
{"tags"=>
{"terms"=>{"field"=>"companies_untouched"},
"facet_filter"=>{"term"=>{"companies"=>"xyz"}}}}}

This is the result that I get:

{"tags"=>
{"_type"=>"terms",
"missing"=>0,
"total"=>3,
"other"=>0,
"terms"=>
[{"term"=>"xyzz", "count"=>1},
{"term"=>"abc", "count"=>1}]}}

But I need only the matching result like below:

{"tags"=>
{"_type"=>"terms",
"missing"=>0,
"total"=>3,
"other"=>0,
"terms"=>
[{"term"=>"xyzz", "count"=>1}]}}

i.e the results should only contain the array values that are matching my
search query. Is there something wrong that I am doing here?

Thanks,
Aash

--

Also look into "Nested Type" and "Nested
Queries" Elasticsearch Platform — Find real-time answers at scale | Elastic
but note that each array element is stored as a separate document and hence
will take more storage.

On Wednesday, January 2, 2013 12:38:42 AM UTC-8, geeky_sh wrote:

I have an elasticsearch document named "user", which contains an array
field - "companies". I want to get all the company names that contains
"xyz" in their name. I have set the custom mapping which tokenizes the data
in the companies array.
Below is the mapping defined for companies array:

field :companies, type: 'string', index: 'analyzed', analyzer:
'name_analyzer' do
field :companies_lower, type: 'string', index: 'analyzed', analyzer:
'lower_keyword'
field :companies_untouched, type: 'string', index: 'not_analyzed'
end

(name_analyzer is the custom analyzer defined by me)

I have these two ES documents:

{"companies":["xyzz","abc"]}
{"companies":["abcd","abcde"]}

Now, when I perform this query:

{"query"=>{"match_all"=>{}},
"facets"=>
{"tags"=>
{"terms"=>{"field"=>"companies_untouched"},
"facet_filter"=>{"term"=>{"companies"=>"xyz"}}}}}

This is the result that I get:

{"tags"=>
{"_type"=>"terms",
"missing"=>0,
"total"=>3,
"other"=>0,
"terms"=>
[{"term"=>"xyzz", "count"=>1},
{"term"=>"abc", "count"=>1}]}}

But I need only the matching result like below:

{"tags"=>
{"_type"=>"terms",
"missing"=>0,
"total"=>3,
"other"=>0,
"terms"=>
[{"term"=>"xyzz", "count"=>1}]}}

i.e the results should only contain the array values that are matching my
search query. Is there something wrong that I am doing here?

Thanks,
Aash

--