# Python dsl script\_fields example

**URL:** <https://discuss.elastic.co/t/python-dsl-script-fields-example/94147>\
**Category:** Elasticsearch\
**Created:** [July 21, 2017, 6:47pm UTC](https://discuss.elastic.co/t/python-dsl-script-fields-example/94147 "2017-07-21T18:47:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rugenl](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rugenl/32/12887_2.png) [@rugenl](https://discuss.elastic.co/u/rugenl)\
**Post date:** [July 21, 2017, 6:47pm UTC](https://discuss.elastic.co/t/python-dsl-script-fields-example/94147/1 "2017-07-21T18:47:11Z")

</div>

I've tried to pass a script\_fields to using the python DSL but can't figure it out, does anyone have an example?

Thanks

---

<div class="post-metadata">

**Author:** ![rugenl](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rugenl/32/12887_2.png) [@rugenl](https://discuss.elastic.co/u/rugenl)\
**Post date:** [July 21, 2017, 7:04pm UTC](https://discuss.elastic.co/t/python-dsl-script-fields-example/94147/2 "2017-07-21T19:04:52Z")

</div>

I've tried this:  
s.script\_fields(tscript={  
'script': "doc['offset'].value / 1024"  
})

It runs without error, but none of this appears in the debug log query, so it's not even getting passed to ES.

---

<div class="post-metadata">

**Author:** ![rugenl](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rugenl/32/12887_2.png) [@rugenl](https://discuss.elastic.co/u/rugenl)\
**Post date:** [July 21, 2017, 7:21pm UTC](https://discuss.elastic.co/t/python-dsl-script-fields-example/94147/3 "2017-07-21T19:21:25Z")

</div>

I found conflicting doc, I basing my code on the comments in the library [search.py](http://search.py):  
s = s.script\_fields(times\_two="doc['field'].value \* 2")

but I found this on github in test\_search.py

def test\_complex\_example():  
s = search.Search()  
s = s.query('match', title='python')   
.query(~Q('match', title='ruby'))   
.filter(Q('term', category='meetup') | Q('term', category='conference'))   
.post\_filter('terms', tags=['prague', 'czech'])   
.script\_fields(more\_attendees="doc['attendees'].value + 42")

```
s.aggs.bucket('per_country', 'terms', field='country')\
    .metric('avg_attendees', 'avg', field='attendees')

s.query.minimum_should_match = 2

s = s.highlight_options(order='score').highlight('title', 'body', fragment_size=50)

```

I've got my test getting passed, but still not what I need. Why does the code sometimes start with s.xxxx but other times have s = s.xxxx?

What I need to do is shorten a text field doc["afield"].value to just be the first 30 characters. The field may contain 0 to 100 (or so) characters.

---

<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:** [August 18, 2017, 7:21pm UTC](https://discuss.elastic.co/t/python-dsl-script-fields-example/94147/4 "2017-08-18T19:21:56Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
