Python dsl script_fields example


(Len Rugen) #1

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


(Len Rugen) #2

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.


(Len Rugen) #3

I found conflicting doc, I basing my code on the comments in the library 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.


(system) #4

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