# Java Api percolate with aggregations

**URL:** <https://discuss.elastic.co/t/java-api-percolate-with-aggregations/64363>\
**Category:** Elasticsearch\
**Created:** [October 30, 2016, 5:56am UTC](https://discuss.elastic.co/t/java-api-percolate-with-aggregations/64363 "2016-10-30T05:56:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lupova](https://avatars.discourse-cdn.com/v4/letter/l/46a35a/32.png) [@lupova](https://discuss.elastic.co/u/lupova)\
**Post date:** [October 30, 2016, 5:56am UTC](https://discuss.elastic.co/t/java-api-percolate-with-aggregations/64363/1 "2016-10-30T05:56:55Z")

</div>

Hi,  
I am running percolate action and it's works greate but.. when I am trying to add aggregation to percolate request but its returns with no matches.  
like:

> PercolateResponse response = client.preparePercolate()  
> .setIndices("myIndexName")  
> .setDocumentType("myDocumentType")  
> .setSource(docBuilder).addAggregation(AggregationBuilders.terms("ruleNames").field("ruleName")).execute().actionGet();

My percolator mapping is:

```
  PUT news-feed-index/.percolator/_mapping
{
        ".percolator" : {
                "properties" : {
                        "query" : {
                                "type" : "object",
                                "enabled" : false
                        },
                        "usersRuleState" : { 
                            "type": "nested",
                            "properties": {
                                "user": {"type": "string"},
                                "isActive": {"type": "boolean"}
                            }
                        },
                        "filter": {"type": "string"},
                        "owner": {"type": "string"},
                        "ruleName": {"type": "string"}
                                                         
                        }
                }
        }

```

and I am trying to do this but in Java Api:

```
POST /news-feed-index/news/_percolate/
{
  "doc": {
 "subject": "Murder in Mexico",
 "body": "",
 "priority":1
  },
  "size": 0, 
"aggs" : { 
    "users" : { 
        "terms" : { 
          "field" : "user"
        }
    }
}
}

```

Any help?

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [October 31, 2016, 10:06am UTC](https://discuss.elastic.co/t/java-api-percolate-with-aggregations/64363/2 "2016-10-31T10:06:39Z")

</div>

Can you try to use PercolateSourceBuilder the build request body and then set that directly on the request? the addAggregation(...) and other builder methods undo what has been set via setSource(...) method.

Something like this:

```java
PercolateSourceBuilder sourceBuilder = new PercolateSourceBuilder()
                .setDoc(docBuilder)
                .addAggregation(AggregationBuilders.terms("ruleNames").field("ruleName"));
        setSource(sourceBuilder);
client.preparePercolate()
   .setIndices("myIndexName")
   .setDocumentType("myDocumentType")
   .setSource(sourceBuilder)

```

---

<div class="post-metadata">

**Author:** ![lupova](https://avatars.discourse-cdn.com/v4/letter/l/46a35a/32.png) [@lupova](https://discuss.elastic.co/u/lupova)\
**Post date:** [October 31, 2016, 2:03pm UTC](https://discuss.elastic.co/t/java-api-percolate-with-aggregations/64363/3 "2016-10-31T14:03:41Z")

</div>

Thanks!

I solved it other way.  
After I read [https://github.com/elastic/elasticsearch/issues/11162](https://github.com/elastic/elasticsearch/issues/11162) I understand that the problem is in setSource().

```
   PercolateResponse response = client.preparePercolate()
.setIndices("myIndexName")
.setDocumentType("myDocumentType")
.setPercolateDoc(new PercolateSourceBuilder.DocBuilder().setDoc(doc) )
.addAggregation(AggregationBuilders.terms("ruleNames").field("ruleName")).execute().actionGet();

```

This is solved it.  
😇

---

<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 5, 2017, 10:08pm UTC](https://discuss.elastic.co/t/java-api-percolate-with-aggregations/64363/4 "2017-07-05T22:08:16Z")

</div>


