# How can I do intersection or union operation with two facets filter？

**URL:** <https://discuss.elastic.co/t/how-can-i-do-intersection-or-union-operation-with-two-facets-filter/18503>\
**Category:** Elasticsearch\
**Created:** [July 7, 2014, 10:26am UTC](https://discuss.elastic.co/t/how-can-i-do-intersection-or-union-operation-with-two-facets-filter/18503 "2014-07-07T10:26:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![11157](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/11157/32/1293_2.png) [@11157](https://discuss.elastic.co/u/11157)\
**Post date:** [July 7, 2014, 10:26am UTC](https://discuss.elastic.co/t/how-can-i-do-intersection-or-union-operation-with-two-facets-filter/18503/1 "2014-07-07T10:26:33Z")

</div>

## Dear All! I have some docs: {"field\_A":"aaa","field\_B":"bbb"} {"field\_A":"aaa","field\_B":"ccc"} {"field\_A":"bbb","field\_B":"bbb"} {"field\_A":"bbb","field\_B":"bbb"} {"field\_A":"bbb","field\_B":"eee"} {"field\_A":"aaa","field\_B":"aaaa"} {"field\_A":"ccc","field\_B":"aaaa"} first step: { "query":{ "filtered" : { "filter" : { "bool" : { "must" : { "term" : { "field\_B" : "bbb" } } } } } }, "facets" : { "tag" : { "terms" : { "field" : "field\_A" } } } } first result: { ... {"term":"aaa","count":1}, {"term":"bbb","count":2}, ... }

## second step: the second facets: { "query":{ "filtered" : { "filter" : { "bool" : { "must" : { "term" : { "field\_B" : "aaaa" } } } } } }, "facets" : { "tag" : { "terms" : { "field" : "field\_A" } } } } second result: { ... {"term":"aaa","count":1}. {"term":"ccc","count":1} ... }

third step:  
combine the two result with interesction operation with "term":  
{"term":"aaa","count":"I don't care the count value."}

* * *

Now, How can i combine the three steps in one filter facets or othen  
method??

Thx All!!

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/53BA75D9.1080502%40gmail.com](https://groups.google.com/d/msgid/elasticsearch/53BA75D9.1080502%40gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Harish\_Ved](https://avatars.discourse-cdn.com/v4/letter/h/f4b2a3/32.png) [@Harish\_Ved](https://discuss.elastic.co/u/Harish_Ved)\
**Post date:** [July 7, 2014, 6:07pm UTC](https://discuss.elastic.co/t/how-can-i-do-intersection-or-union-operation-with-two-facets-filter/18503/2 "2014-07-07T18:07:34Z")

</div>

Did you try the following query?  
"query":{  
"filtered" : {  
"filter" : {  
"bool" : {  
"should" : [  
{"term" : { "field\_B" : "aaaa" }},  
{"term": {"field\_B": "bbb"}}  
]  
}  
}  
}  
},  
"facets" : {  
"tag" : {  
"terms" : {  
"field" : "field\_A"  
}  
}  
}  
}

Plz confirm if it works for you.

On Monday, 7 July 2014 15:56:53 UTC+5:30, 闫旭 wrote:

> ## Dear All! I have some docs: {"field\_A":"aaa","field\_B":"bbb"} {"field\_A":"aaa","field\_B":"ccc"} {"field\_A":"bbb","field\_B":"bbb"} {"field\_A":"bbb","field\_B":"bbb"} {"field\_A":"bbb","field\_B":"eee"} {"field\_A":"aaa","field\_B":"aaaa"} {"field\_A":"ccc","field\_B":"aaaa"} first step: { "query":{ "filtered" : { "filter" : { "bool" : { "must" : { "term" : { "field\_B" : "bbb" } } } } } }, "facets" : { "tag" : { "terms" : { "field" : "field\_A" } } } } first result: { ... {"term":"aaa","count":1}, {"term":"bbb","count":2}, ... }
> 
> second step:  
> the second facets:  
> {  
> "query":{  
> "filtered" : {  
> "filter" : {  
> "bool" : {  
> "must" : {  
> "term" : { "field\_B" : "aaaa" }  
> }  
> }  
> }  
> }  
> },  
> "facets" : {  
> "tag" : {  
> "terms" : {  
> "field" : "field\_A"  
> }  
> }  
> }  
> }  
> second result:  
> {  
> ...  
> {"term":"aaa","count":1}.  
> {"term":"ccc","count":1}  
> ...  
> }
> 
> * * *
> 
> third step:  
> combine the two result with interesction operation with "term":  
> {"term":"aaa","count":"I don't care the count value."}
> 
> * * *
> 
> Now, How can i combine the three steps in one filter facets or othen  
> method??
> 
> Thx All!!

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/93d5adb6-bcd1-49b6-8c28-be4d456f92d8%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/93d5adb6-bcd1-49b6-8c28-be4d456f92d8%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![11157](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/11157/32/1293_2.png) [@11157](https://discuss.elastic.co/u/11157)\
**Post date:** [July 8, 2014, 2:05am UTC](https://discuss.elastic.co/t/how-can-i-do-intersection-or-union-operation-with-two-facets-filter/18503/3 "2014-07-08T02:05:00Z")

</div>

It doesn't work, the result is:  
{"term":"bbb","count":2},  
{"term":"aaa","count":1 },  
{"term":"ccc","count":1 }  
于 2014年07月08日 02:07, Harish Ved 写道:

> Did you try the following query?  
> "query":{  
> "filtered" : {  
> "filter" : {  
> "bool" : {  
> "should" : [  
> {"term" : { "field\_B" : "aaaa" }},  
> {"term": {"field\_B": "bbb"}}  
> ]  
> }  
> }  
> }  
> },  
> "facets" : {  
> "tag" : {  
> "terms" : {  
> "field" : "field\_A"  
> }  
> }  
> }  
> }
> 
> Plz confirm if it works for you.
> 
> On Monday, 7 July 2014 15:56:53 UTC+5:30, 闫旭 wrote:
> 
> ```
> Dear All!
> I have some docs:
> {"field_A":"aaa","field_B":"bbb"}
> {"field_A":"aaa","field_B":"ccc"}
> {"field_A":"bbb","field_B":"bbb"}
> {"field_A":"bbb","field_B":"bbb"}
> {"field_A":"bbb","field_B":"eee"}
> {"field_A":"aaa","field_B":"aaaa"}
> {"field_A":"ccc","field_B":"aaaa"}
> first step:
> {
> "query":{
> "filtered" : {
> "filter" : {
> "bool" : {
> "must" : {
> "term" : { "field_B" : "bbb" }
> }
> }
> }
> }
> },
> "facets" : {
> "tag" : {
> "terms" : {
> "field" : "field_A"
> }
> }
> }
> }
> first result:
> {
> ...
> {"term":"aaa","count":1},
> {"term":"bbb","count":2},
> ...
> }
> -------------------------------------------------------------------------
> second step:
> the second facets:
> {
> "query":{
> "filtered" : {
> "filter" : {
> "bool" : {
> "must" : {
> "term" : { "field_B" : "aaaa" }
> }
> }
> }
> }
> },
> "facets" : {
> "tag" : {
> "terms" : {
> "field" : "field_A"
> }
> }
> }
> }
> second result:
> {
> ...
> {"term":"aaa","count":1}.
> {"term":"ccc","count":1}
> ...
> }
> -----------------------------------------------------------------------------
> third step:
> combine the two result with interesction operation with "term":
> {"term":"aaa","count":"I don't care the count value."}
> 
> -----------------------------------------------------------------------------
> Now, How can i combine the three steps in one filter facets or
> othen method??
> 
> Thx All!!
> 
> ```
> 
> --  
> You received this message because you are subscribed to the Google  
> Groups "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send  
> an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com)  
> [mailto:elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/93d5adb6-bcd1-49b6-8c28-be4d456f92d8%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/93d5adb6-bcd1-49b6-8c28-be4d456f92d8%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/93d5adb6-bcd1-49b6-8c28-be4d456f92d8%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/93d5adb6-bcd1-49b6-8c28-be4d456f92d8%40googlegroups.com?utm_medium=email&utm_source=footer).  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/53BB51CC.3080005%40gmail.com](https://groups.google.com/d/msgid/elasticsearch/53BB51CC.3080005%40gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 1:17am UTC](https://discuss.elastic.co/t/how-can-i-do-intersection-or-union-operation-with-two-facets-filter/18503/4 "2017-07-06T01:17:27Z")

</div>


