4

Trying to use a fields=[...] paramater in pyes .search is failing

Here is a simple test script to illustrate the problem: http://pastebin.com/LiRMC3ib

Using the current release of pyes 0.19.1 this script outputs {} as a result of the

print resultset[0]

However using a previous 'old' unstable 0.19.1 version of pyes which I have - 0.19.1 (unstable) the result of

print resultset[0]

is the expected:

{u'name': u'Joe Tester'}

Using fields in an ES.get call does work.

Anyone else seen this or have some pointers as to what's up?

epoz
  • 43
  • 3
  • I have same problem. Have you succeed to solve it? – Milan Kocic Jan 15 '13 at 16:23
  • Note, I have since switched to using the client at http://www.elasticsearch.org/guide/en/elasticsearch/client/python-api/current/ it doesn't abstract away the raw JSON API, which actually makes it a lot _easier_ to use. – epoz Jul 24 '14 at 08:45

5 Answers5

2

rewrite

resultset = ES.search(query=q, indices='oolong', fields=["name"])

to

resultset = ES.search(Search(q, fields=['name']), indices='oolong'))
Popieluch
  • 180
  • 1
  • 8
1

One thing I notice in your pastebin code that may explain the unexpected behaviour - the refresh (line 37) should be before the search (line 36). Otherwise there's a race condition if the document has been committed to the index yet.

barnybug
  • 643
  • 1
  • 5
  • 8
1

I'm with the same problem using pyes 0.19.1, but I'm able to retrieve a single field from the result set.

Replace this line:

resultset = ES.search(query=q, indices='oolong', fields=["name"])

to this one:

resultset = ES.search(query=q, indices='oolong', fields="name")

This works for me. I haven't figured out how to retrieve multiple fields though. When I pass a list to fields it always returns empty dictionaries.

JCJS
  • 3,031
  • 3
  • 19
  • 25
0

Have a look at:

class pyes.query.Search(...)

There you can set a fields array. Fields on ES.search is not working.

es_connection = ES(server=[('http', 'localhost', '9200')])
q = Search(fields=['field1', 'field2'], .....)
resultset = es_connection.search(
    q,
    ....
)
Julian Hille
  • 669
  • 5
  • 11
0

Use this

resultset = ES.search(query=q, indices='oolong', fields="name")

or in case you have more than one field use

resultset = ES.search(query=q, indices='oolong', fields="name,id")