# Term offsets in scripting returning "-1"

**URL:** <https://discuss.elastic.co/t/term-offsets-in-scripting-returning-1/29221>\
**Category:** Elasticsearch\
**Created:** [September 14, 2015, 9:14am UTC](https://discuss.elastic.co/t/term-offsets-in-scripting-returning-1/29221 "2015-09-14T09:14:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Patrick\_Lam](https://avatars.discourse-cdn.com/v4/letter/p/ed8c4c/32.png) [@Patrick\_Lam](https://discuss.elastic.co/u/Patrick_Lam)\
**Post date:** [September 14, 2015, 9:14am UTC](https://discuss.elastic.co/t/term-offsets-in-scripting-returning-1/29221/1 "2015-09-14T09:14:28Z")

</div>

I'm trying to get the term offsets and positions for a field in my index via scripting following the examples here:

[https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-advanced-scripting.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-advanced-scripting.html)

I was successful in returning the positions of the terms correctly, but when I try to return the offsets using

\_index['FIELD'].get('TERM', \_OFFSETS)  
POS\_OBJECT.startOffset  
POS\_OBJECT.endOffset

with the appropriate fields filled in, I get all "-1" for both the start and end offsets for all terms. The field is mapped correctly with positions and offsets.

Does anybody know why this is happening?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [September 14, 2015, 10:27am UTC](https://discuss.elastic.co/t/term-offsets-in-scripting-returning-1/29221/2 "2015-09-14T10:27:03Z")

</div>

Can you provide a cut-down example of CURL commands that define:

1. the mapping
2. example doc
3. example query that fails

Thanks

---

<div class="post-metadata">

**Author:** ![Patrick\_Lam](https://avatars.discourse-cdn.com/v4/letter/p/ed8c4c/32.png) [@Patrick\_Lam](https://discuss.elastic.co/u/Patrick_Lam)\
**Post date:** [September 14, 2015, 9:54pm UTC](https://discuss.elastic.co/t/term-offsets-in-scripting-returning-1/29221/3 "2015-09-14T21:54:53Z")

</div>

I'm using the elasticsearch-py client within python to do this so hopefully the following will suffice to get at the problem.

Mapping:

```
"mappings": {
"tweet": {
  "properties": {
    "text": {
      "type": "string",
      "term_vector": "with_positions_offsets",
      "store" : True,
      "index_analyzer" : "fulltext_analyzer"
        }
    },
  }
}

```

Example document indexed:

```
es.index(index = "boston", doc_type = "tweet", body = {'text': 'the quick brown fox'})

```

Example query for term start offsets:

```
qterms = {
"query": {
    "query_string": {
        "default_field": "text",
        "query": "brown AND fox",
        "analyzer": "fulltext_analyzer"
    }
},

"script_fields": {
    "term_positions": {
        "script": "def tv = [:]; for (word in doc[field].values){ termInfo = _index[field].get(word,_POSITIONS | _OFFSETS); def pos = []; termInfo.each{ pos.add(it.startOffset); }; tv.put(word,pos); }; return tv;",
        "params": {
            "field": "text"
        }
    }
}
}

es.search(index = "boston", body=qterms)

```

Output (note the -1s):

```
{u'_shards': {u'failed': 0, u'successful': 1, u'total': 1},
 u'hits': {u'hits': [{u'_id': u'AU_Nz2AXPRDwyxJBcoG7',
    u'_index': u'boston',
    u'_score': 5.207162,
    u'_type': u'tweet',
    u'fields': {u'term_positions': [{u'brown': [-1],
       u'fox': [-1],
       u'quick': [-1]}]}}],
  u'max_score': 5.207162,
  u'total': 1},
 u'timed_out': False,
 u'took': 3}

```

In my qterms query above, changing "it.startOffset" to "it.position" correctly returns the positions of the terms:

```
{u'_shards': {u'failed': 0, u'successful': 1, u'total': 1},
 u'hits': {u'hits': [{u'_id': u'AU_Nz2AXPRDwyxJBcoG7',
    u'_index': u'boston',
    u'_score': 5.207162,
    u'_type': u'tweet',
    u'fields': {u'term_positions': [{u'brown': [2],
       u'fox': [3],
       u'quick': [1]}]}}],
  u'max_score': 5.207162,
  u'total': 1},
 u'timed_out': False,
 u'took': 3}
```

---

<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:50pm UTC](https://discuss.elastic.co/t/term-offsets-in-scripting-returning-1/29221/4 "2017-07-05T23:50:23Z")

</div>


