# Best way to search scoped within a single array element?

**URL:** <https://discuss.elastic.co/t/best-way-to-search-scoped-within-a-single-array-element/8111>\
**Category:** Elasticsearch\
**Created:** [June 14, 2012, 11:26pm UTC](https://discuss.elastic.co/t/best-way-to-search-scoped-within-a-single-array-element/8111 "2012-06-14T23:26:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew\_Cholakian](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrew_cholakian/32/1712_2.png) [@Andrew\_Cholakian](https://discuss.elastic.co/u/Andrew_Cholakian)\
**Post date:** [June 14, 2012, 11:26pm UTC](https://discuss.elastic.co/t/best-way-to-search-scoped-within-a-single-array-element/8111/1 "2012-06-14T23:26:46Z")

</div>

I've got a bunch of data like this:

curl -X POST "[http://localhost:9200/articles/article](http://localhost:9200/articles/article)" -d '{"title" : "A  
test", "tags" : ["brand\_gucci,cat\_purse,color\_red", "brand\_forever  
21,color\_orange,cat\_socks"]}'

I'd like to find articles that only have gucci brand purses. So, I want a  
query that will match only if the terms appear all in one array element. If  
the terms span two elements in the 'tags' array they cannot be considered a  
match.

I've tried a query like:  
{  
"terms" : {  
"tags" : ["brand\_gucci", "cat\_purse"],  
"minimum\_match" : 2  
}

but that will match a document like:

curl -X POST "[http://localhost:9200/articles/article](http://localhost:9200/articles/article)" -d '{"title" : "A  
test", "tags" : ["brand\_gucci,cat\_hat,color\_red", "brand\_forever  
21,color\_purse,cat\_socks"]}'

which I don't want. Does anyone have any idea what the best way to do this  
would be? Help would be much appreciated.

BTW, I should mention, I'm using a custom analyzer that splits on ',' to  
make sure we tokenize tags correctly

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [June 15, 2012, 10:42am UTC](https://discuss.elastic.co/t/best-way-to-search-scoped-within-a-single-array-element/8111/2 "2012-06-15T10:42:57Z")

</div>

Hiya

> curl -X POST "[http://localhost:9200/articles/article](http://localhost:9200/articles/article)" -d '{"title" :  
> "A test", "tags" : ["brand\_gucci,cat\_purse,color\_red", "brand\_forever  
> 21,color\_orange,cat\_socks"]}'
> 
> I'd like to find articles that only have gucci brand purses. So, I  
> want a query that will match only if the terms appear all in one array  
> element. If the terms span two elements in the 'tags' array they  
> cannot be considered a match.

Then you need to use 'nested' fields (which internally are indexed as  
separate documents), otherwise the tokens are flattened into a single  
field.

clint

> I've tried a query like:  
> {  
> "terms" : {  
> "tags" : ["brand\_gucci", "cat\_purse"],  
> "minimum\_match" : 2  
> }
> 
> but that will match a document like:
> 
> curl -X POST "[http://localhost:9200/articles/article](http://localhost:9200/articles/article)" -d '{"title" :  
> "A test", "tags" : ["brand\_gucci,cat\_hat,color\_red", "brand\_forever  
> 21,color\_purse,cat\_socks"]}'
> 
> which I don't want. Does anyone have any idea what the best way to do  
> this would be? Help would be much appreciated.
> 
> BTW, I should mention, I'm using a custom analyzer that splits on ','  
> to make sure we tokenize tags correctly

---

<div class="post-metadata">

**Author:** ![Andrew\_Cholakian](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrew_cholakian/32/1712_2.png) [@Andrew\_Cholakian](https://discuss.elastic.co/u/Andrew_Cholakian)\
**Post date:** [June 15, 2012, 3:27pm UTC](https://discuss.elastic.co/t/best-way-to-search-scoped-within-a-single-array-element/8111/3 "2012-06-15T15:27:03Z")

</div>

Ah, thanks! Works like a charm!

On Friday, June 15, 2012 3:42:57 AM UTC-7, Clinton Gormley wrote:

> Hiya
> 
> > curl -X POST "[http://localhost:9200/articles/article](http://localhost:9200/articles/article)" -d '{"title" :  
> > "A test", "tags" : ["brand\_gucci,cat\_purse,color\_red", "brand\_forever  
> > 21,color\_orange,cat\_socks"]}'
> > 
> > I'd like to find articles that only have gucci brand purses. So, I  
> > want a query that will match only if the terms appear all in one array  
> > element. If the terms span two elements in the 'tags' array they  
> > cannot be considered a match.
> 
> Then you need to use 'nested' fields (which internally are indexed as  
> separate documents), otherwise the tokens are flattened into a single  
> field.
> 
> clint
> 
> > I've tried a query like:  
> > {  
> > "terms" : {  
> > "tags" : ["brand\_gucci", "cat\_purse"],  
> > "minimum\_match" : 2  
> > }
> > 
> > but that will match a document like:
> > 
> > curl -X POST "[http://localhost:9200/articles/article](http://localhost:9200/articles/article)" -d '{"title" :  
> > "A test", "tags" : ["brand\_gucci,cat\_hat,color\_red", "brand\_forever  
> > 21,color\_purse,cat\_socks"]}'
> > 
> > which I don't want. Does anyone have any idea what the best way to do  
> > this would be? Help would be much appreciated.
> > 
> > BTW, I should mention, I'm using a custom analyzer that splits on ','  
> > to make sure we tokenize tags correctly

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 3:23am UTC](https://discuss.elastic.co/t/best-way-to-search-scoped-within-a-single-array-element/8111/4 "2017-07-06T03:23:54Z")

</div>


