Filtering on array


(Dan Fairs) #1

Hi,

I have a load of documents indexed, and they have a 'tags' key with an array of, unsurprisingly, tags - all not_analyzed.

I'd like to search my documents, excluding those with a certain tag or tags. However, I'd like a document to be included in search results if it has any further tags. For example, consider the documents:

{ 'tags': ['foo'], '_id': 'a' }
{ 'tags': ['foo', 'bar'], '_id': 'b'}

Searching with the exclusion of 'foo' should still return document id 'b', as it contains the tag 'bar' as well.

Is this something I should be looking at scripts for, or is there a cleaner way within the existing query DSL?

Cheers,
Dan

Dan Fairs | dan.fairs@gmail.com | @danfairs | www.fezconsulting.com

--


(Dan Fairs) #2

I have a load of documents indexed, and they have a 'tags' key with an array of, unsurprisingly, tags - all not_analyzed.

I'd like to search my documents, excluding those with a certain tag or tags. However, I'd like a document to be included in search results if it has any further tags. For example, consider the documents:

{ 'tags': ['foo'], '_id': 'a' }
{ 'tags': ['foo', 'bar'], '_id': 'b'}

Searching with the exclusion of 'foo' should still return document id 'b', as it contains the tag 'bar' as well.

Is this something I should be looking at scripts for, or is there a cleaner way within the existing query DSL?

For the benefit of the archives, the script is the route I went down (native script, as it happened), and it seems to be working well.

There isn't much in the way of documentation out there writing native scripts used as filters - at least, not that I could find. It took a bit of trial and error to get things going. In the end, I subclassed AbstractSearchScript, and overrode run() to return a boolean to indicate whether the document should pass the filter or not.

Cheers,
Dan

Dan Fairs | dan.fairs@gmail.com | @danfairs | www.fezconsulting.com

--


(system) #3