# Percolator - Adding metadata to query

**URL:** <https://discuss.elastic.co/t/percolator-adding-metadata-to-query/28421>\
**Category:** Elasticsearch\
**Created:** [September 1, 2015, 10:44am UTC](https://discuss.elastic.co/t/percolator-adding-metadata-to-query/28421 "2015-09-01T10:44:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![raunak](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raunak/32/4446_2.png) [@raunak](https://discuss.elastic.co/u/raunak)\
**Post date:** [September 1, 2015, 10:44am UTC](https://discuss.elastic.co/t/percolator-adding-metadata-to-query/28421/1 "2015-09-01T10:44:29Z")

</div>

I'm building my queries using `SearchSourceBuilder` and saving the query into ES as type `.percolator` - works great.

Now, I want to add metadata to my query, so my query looks something like below;

```
{  
  "query":{  
    "match":{  
      "author":"vonnegut"
    },
    "userid":"1"
  }
}

```

However, I don't see an option to do this in `SearchSourceBuilder`. Am I missing something, or there is another Builder I should be using?

PS: The idea is that, if multiple users are interested in 1 topic, it makes sense to me to create one percolate query and save those user's metadata in the query.

---

<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:** [September 1, 2015, 11:50am UTC](https://discuss.elastic.co/t/percolator-adding-metadata-to-query/28421/2 "2015-09-01T11:50:53Z")

</div>

The best way to achieve this is to not use the `SearchSourceBuilder` but use the `XContentBuilder` to construct the percolator query document yourself. In order to add the query you use one of the query builders and add that to the `XContentBuilder` under the `query` field.

---

<div class="post-metadata">

**Author:** ![raunak](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raunak/32/4446_2.png) [@raunak](https://discuss.elastic.co/u/raunak)\
**Post date:** [September 1, 2015, 11:57am UTC](https://discuss.elastic.co/t/percolator-adding-metadata-to-query/28421/3 "2015-09-01T11:57:30Z")

</div>

Thanks @mvg.

What is the best way to extract the additional metadata later on. As far as I can see, the percolate API returns the `_id` field and `_index` field for all matched queries. Is the idea then to query the type `.percolator` with the returned `ids`, or is there a way better way?

---

<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:** [September 1, 2015, 12:04pm UTC](https://discuss.elastic.co/t/percolator-adding-metadata-to-query/28421/4 "2015-09-01T12:04:55Z")

</div>

At the moment the percolator api can only return the `_index` and `_id` of the matched queries.  
At some point the percolator api will be improved to return the entire query or a part of it:

> <https://github.com/elastic/elasticsearch/issues/4317>

---

<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, 11:52pm UTC](https://discuss.elastic.co/t/percolator-adding-metadata-to-query/28421/5 "2017-07-05T23:52:53Z")

</div>


