# Search by cardinality of a field

**URL:** <https://discuss.elastic.co/t/search-by-cardinality-of-a-field/18909>\
**Category:** Elasticsearch\
**Created:** [July 25, 2014, 10:03pm UTC](https://discuss.elastic.co/t/search-by-cardinality-of-a-field/18909 "2014-07-25T22:03:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric\_Brunson](https://avatars.discourse-cdn.com/v4/letter/e/41988e/32.png) [@Eric\_Brunson](https://discuss.elastic.co/u/Eric_Brunson)\
**Post date:** [July 25, 2014, 10:03pm UTC](https://discuss.elastic.co/t/search-by-cardinality-of-a-field/18909/1 "2014-07-25T22:03:45Z")

</div>

I have a doc type which includes a field that is a list of strings. I'd  
like to query/filter based on the number of items in the list, either  
exactly equal to n or greater than/less than. Is that possible? I haven't  
found anything in the Query DSL that seems to lend itself to that.

Thanks!

--  
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/57f440c9-ae9a-4dd6-80ab-cef92348ce5c%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/57f440c9-ae9a-4dd6-80ab-cef92348ce5c%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Eric\_Brunson](https://avatars.discourse-cdn.com/v4/letter/e/41988e/32.png) [@Eric\_Brunson](https://discuss.elastic.co/u/Eric_Brunson)\
**Post date:** [July 28, 2014, 4:36pm UTC](https://discuss.elastic.co/t/search-by-cardinality-of-a-field/18909/2 "2014-07-28T16:36:04Z")

</div>

I found what I think should work in a script filter, but I get an access  
exception trying to use it.

Adding the following filter:

```
    {
      "filtered": {
        "filter": {
          "script": {
            "lang": "mvel",
            "script": "doc['currentPatchSet.parents'].values.length < 

```

param1",  
"params": {  
"param1": 2  
}  
}  
}  
}  
}

Ends up with the following error(s) at the bottom of the traceback:

{  
"error": "SearchPhaseExecutionException[Failed to execute phase [query],  
all shards failed; shardFailures {[yCmFfug8TdK15SxsAUSrww][gerrit\_v2][0]:  
QueryPhaseExecutionException[[gerrit\_v2][0]: query[filtered(+status:merged  
+ConstantScore(cache(BooleanFilter(currentPatchSet.parents:[\* TO _])))  
+ConstantScore(ScriptFilter(doc['currentPatchSet.parents'].values.length \<  
param1)))-\>cache(\_type:changes)],from[0],size[10]: Query Failed [Failed to  
execute main query]]; nested:  
IllegalAccessError[org/elasticsearch/index/fielddata/ScriptDocValues$Strings$1];  
}{[in46F5VZQLCoUF0NyBv\_Kg][gerrit\_v2][1]: RemoteTransportException[[En  
Sabah Nur][inet[/10.226.73.179:9300]][search/phase/query]]; nested:  
QueryPhaseExecutionException[[gerrit\_v2][1]: query[filtered(+status:merged  
+ConstantScore(cache(BooleanFilter(currentPatchSet.parents:[_ TO _])))  
+ConstantScore(ScriptFilter(doc['currentPatchSet.parents'].values.length \<  
param1)))-\>cache(type:changes)],from[0],size[10]: Query Failed [Failed to  
execute main query]]; nested:  
IllegalAccessError[org/elasticsearch/index/fielddata/ScriptDocValues$Strings$1];  
}{[gEShLR-SnK2d7RiQaaMjA][gerrit\_v2][3]:  
RemoteTransportException[[Bounty][inet[/10.226.73.178:9300]][search/phase/query]];  
nested: QueryPhaseExecutionException[[gerrit\_v2][3]:  
query[filtered(+status:merged  
+ConstantScore(cache(BooleanFilter(currentPatchSet.parents:[_ TO _])))  
+ConstantScore(ScriptFilter(doc['currentPatchSet.parents'].values.length \<  
param1)))-\>cache(\_type:changes)],from[0],size[10]: Query Failed [Failed to  
execute main query]]; nested:  
IllegalAccessError[org/elasticsearch/index/fielddata/ScriptDocValues$Strings$1];  
}{[o6oUK9rhRSinAyFDaAni5g][gerrit\_v2][2]:  
RemoteTransportException[[Jolt][inet[/10.226.73.177:9300]][search/phase/query]];  
nested: QueryPhaseExecutionException[[gerrit\_v2][2]:  
query[filtered(+status:merged  
+ConstantScore(cache(BooleanFilter(currentPatchSet.parents:[_ TO _])))  
+ConstantScore(ScriptFilter(doc['currentPatchSet.parents'].values.length \<  
param1)))-\>cache(\_type:changes)],from[0],size[10]: Query Failed [Failed to  
execute main query]]; nested:  
IllegalAccessError[org/elasticsearch/index/fielddata/ScriptDocValues$Strings$1];  
}{[in46F5VZQLCoUF0NyBv\_Kg][gerrit\_v2][4]: RemoteTransportException[[En  
Sabah Nur][inet[/10.226.73.179:9300]][search/phase/query]]; nested:  
QueryPhaseExecutionException[[gerrit\_v2][4]: query[filtered(+status:merged  
+ConstantScore(cache(BooleanFilter(currentPatchSet.parents:[_ TO \*])))  
+ConstantScore(ScriptFilter(doc['currentPatchSet.parents'].values.length \<  
param1)))-\>cache(\_type:changes)],from[0],size[10]: Query Failed [Failed to  
execute main query]]; nested:  
_IllegalAccessError[org/elasticsearch/index/fielddata/ScriptDocValues$Strings$1]_;  
}]",  
"status": 500  
}

It that some sort of typing I can get around?

Thanks for any help.

Sincerely,  
e.

On Friday, July 25, 2014 4:03:45 PM UTC-6, Eric Brunson wrote:

> I have a doc type which includes a field that is a list of strings. I'd  
> like to query/filter based on the number of items in the list, either  
> exactly equal to n or greater than/less than. Is that possible? I haven't  
> found anything in the Query DSL that seems to lend itself to that.
> 
> Thanks!

--  
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/de428aca-b17f-4eff-b9c4-0653ee261301%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/de428aca-b17f-4eff-b9c4-0653ee261301%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Eric\_Brunson](https://avatars.discourse-cdn.com/v4/letter/e/41988e/32.png) [@Eric\_Brunson](https://discuss.elastic.co/u/Eric_Brunson)\
**Post date:** [July 28, 2014, 7:36pm UTC](https://discuss.elastic.co/t/search-by-cardinality-of-a-field/18909/3 "2014-07-28T19:36:29Z")

</div>

In case anyone else needs the answer, I was able to make it work with:

```
    {
      "filtered": {
        "filter": {
          "script": {
            "script": "doc['currentPatchSet.parents'].values.size() == 

```

1"  
}  
}  
}  
}

Hope that helps.

e.

On Monday, July 28, 2014 10:36:05 AM UTC-6, Eric Brunson wrote:

> I found what I think should work in a script filter, but I get an access  
> exception trying to use it.
> 
> Adding the following filter:
> 
> ```
> {
> "filtered": {
> "filter": {
> "script": {
> "lang": "mvel",
> "script": "doc['currentPatchSet.parents'].values.length < 
> 
> ```
> 
> param1",  
> "params": {  
> "param1": 2  
> }  
> }  
> }  
> }  
> }
> 
> Ends up with the following error(s) at the bottom of the traceback:
> 
> {  
> "error": "SearchPhaseExecutionException[Failed to execute phase  
> [query], all shards failed; shardFailures  
> {[yCmFfug8TdK15SxsAUSrww][gerrit\_v2][0]:  
> QueryPhaseExecutionException[[gerrit\_v2][0]: query[filtered(+status:merged  
> +ConstantScore(cache(BooleanFilter(currentPatchSet.parents:[\* TO _])))  
> +ConstantScore(ScriptFilter(doc['currentPatchSet.parents'].values.length \<  
> param1)))-\>cache(\_type:changes)],from[0],size[10]: Query Failed [Failed to  
> execute main query]]; nested:  
> IllegalAccessError[org/elasticsearch/index/fielddata/ScriptDocValues$Strings$1];  
> }{[in46F5VZQLCoUF0NyBv\_Kg][gerrit\_v2][1]: RemoteTransportException[[En  
> Sabah Nur][inet[/10.226.73.179:9300]][search/phase/query]]; nested:  
> QueryPhaseExecutionException[[gerrit\_v2][1]: query[filtered(+status:merged  
> +ConstantScore(cache(BooleanFilter(currentPatchSet.parents:[_ TO _])))  
> +ConstantScore(ScriptFilter(doc['currentPatchSet.parents'].values.length \<  
> param1)))-\>cache(type:changes)],from[0],size[10]: Query Failed [Failed to  
> execute main query]]; nested:  
> IllegalAccessError[org/elasticsearch/index/fielddata/ScriptDocValues$Strings$1];  
> }{[gEShLR-SnK2d7RiQaaMjA][gerrit\_v2][3]:  
> RemoteTransportException[[Bounty][inet[/10.226.73.178:9300]][search/phase/query]];  
> nested: QueryPhaseExecutionException[[gerrit\_v2][3]:  
> query[filtered(+status:merged  
> +ConstantScore(cache(BooleanFilter(currentPatchSet.parents:[_ TO _])))  
> +ConstantScore(ScriptFilter(doc['currentPatchSet.parents'].values.length \<  
> param1)))-\>cache(\_type:changes)],from[0],size[10]: Query Failed [Failed to  
> execute main query]]; nested:  
> IllegalAccessError[org/elasticsearch/index/fielddata/ScriptDocValues$Strings$1];  
> }{[o6oUK9rhRSinAyFDaAni5g][gerrit\_v2][2]:  
> RemoteTransportException[[Jolt][inet[/10.226.73.177:9300]][search/phase/query]];  
> nested: QueryPhaseExecutionException[[gerrit\_v2][2]:  
> query[filtered(+status:merged  
> +ConstantScore(cache(BooleanFilter(currentPatchSet.parents:[_ TO _])))  
> +ConstantScore(ScriptFilter(doc['currentPatchSet.parents'].values.length \<  
> param1)))-\>cache(\_type:changes)],from[0],size[10]: Query Failed [Failed to  
> execute main query]]; nested:  
> IllegalAccessError[org/elasticsearch/index/fielddata/ScriptDocValues$Strings$1];  
> }{[in46F5VZQLCoUF0NyBv\_Kg][gerrit\_v2][4]: RemoteTransportException[[En  
> Sabah Nur][inet[/10.226.73.179:9300]][search/phase/query]]; nested:  
> QueryPhaseExecutionException[[gerrit\_v2][4]: query[filtered(+status:merged  
> +ConstantScore(cache(BooleanFilter(currentPatchSet.parents:[_ TO \*])))  
> +ConstantScore(ScriptFilter(doc['currentPatchSet.parents'].values.length \<  
> param1)))-\>cache(\_type:changes)],from[0],size[10]: Query Failed [Failed to  
> execute main query]]; nested:  
> _IllegalAccessError[org/elasticsearch/index/fielddata/ScriptDocValues$Strings$1]_;  
> }]",  
> "status": 500  
> }
> 
> It that some sort of typing I can get around?
> 
> Thanks for any help.
> 
> Sincerely,  
> e.
> 
> On Friday, July 25, 2014 4:03:45 PM UTC-6, Eric Brunson wrote:
> 
> > I have a doc type which includes a field that is a list of strings. I'd  
> > like to query/filter based on the number of items in the list, either  
> > exactly equal to n or greater than/less than. Is that possible? I haven't  
> > found anything in the Query DSL that seems to lend itself to that.
> > 
> > Thanks!

--  
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/22570a37-b4a4-413a-8bb5-390dba0b7bff%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/22570a37-b4a4-413a-8bb5-390dba0b7bff%40googlegroups.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:12am UTC](https://discuss.elastic.co/t/search-by-cardinality-of-a-field/18909/4 "2017-07-06T01:12:42Z")

</div>


