# Problems retrieving term payload in custom query

**URL:** <https://discuss.elastic.co/t/problems-retrieving-term-payload-in-custom-query/37881>\
**Category:** Elasticsearch\
**Created:** [December 23, 2015, 6:50pm UTC](https://discuss.elastic.co/t/problems-retrieving-term-payload-in-custom-query/37881 "2015-12-23T18:50:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rjprice72](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@rjprice72](https://discuss.elastic.co/u/rjprice72)\
**Post date:** [December 23, 2015, 6:50pm UTC](https://discuss.elastic.co/t/problems-retrieving-term-payload-in-custom-query/37881/1 "2015-12-23T18:50:42Z")

</div>

In elasticsearch 2.1.0 we have a plugin with a custom Lucene TokenFilter that adds a payload to a special term during indexing analysis, and we also have custom QueryParser that produces Lucene Query/Weight/Scorer classes that compare this payload against some query data to decide if a document matches a query or not.

I know that the payloads are being written as desired since I can easily see them via elasticsearch's termvector API.

However, while our Scorer can see all of the documents with this special term, I can never get access to the payload.

The approach to doing this in the Scorer is simply:

docs = leafReaderContext.reader().postings(specialTerm, PostingsEnum.PAYLOADS);  
Then, walking through the docs via nextDoc() and trying to retrieve each doc's payload via:

BytesRef payloadRef = docs.getPayload();  
if (payloadRef != null) {  
byte[] payload = payloadRef.bytes;  
}

I have logging in place so that I see that every document I have in a test index is being looked at, but for every document, the payloadRef comes back null.

What am I doing wrong? Is there some setting I need to enable somewhere to allow the postings method to do what it says it can do?

Is there a better way to do this?

Any advice would be appreciated!

Bob

---

<div class="post-metadata">

**Author:** ![rjprice72](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@rjprice72](https://discuss.elastic.co/u/rjprice72)\
**Post date:** [December 24, 2015, 4:28pm UTC](https://discuss.elastic.co/t/problems-retrieving-term-payload-in-custom-query/37881/2 "2015-12-24T16:28:02Z")

</div>

I got a reply on the Lucene users mailing list that solved my problem, and may help someone else, so I will give it here:

```
Adrien Grand via lucene.apache.org 

Note that payloads are stored per position, not per document. Maybe the
problem is that you never call docs.nextPosition()?

```

For me, since I know that my special term only appears once, all I had to do was add a call to docs.nextPosition() before trying to retrieve the payload.

---

<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:28pm UTC](https://discuss.elastic.co/t/problems-retrieving-term-payload-in-custom-query/37881/3 "2017-07-05T23:28:46Z")

</div>


