Home > Net >  How to select or retrieve only specific fields after elasticsearch query?
How to select or retrieve only specific fields after elasticsearch query?

Time:01-12

I am able to query an index in elasticsearch. And, now I want to narrow down the data to some specific fields. But, I am keep getting errors.

Here is my query:

es = Elasticsearch(hosts="myhost", "port":0000)


search_body={
    "bool":{
            "filter":[
                {"exists": {"field": "customer_name"}},
                {"match_phrase": {"city": "chicago"}},
                ]
        }

    }

results = es.search(index="some_index", query=search_body)

I am easily able to get results upto this point. But, since the returned has so many fields, I want to retrieve only specific fields before converting it into a dataframe. I can convert it into a dataframe and then filter, but that is not optimal.


I tried adding _source and field methods as:

search_body={
    "bool":{
            "filter":[
                {"exists": {"field": "customer_name"}},
                {"match_phrase": {"city": "chicago"}},
                ]
        },
    "_source":{"fields": {"includes":["customer_name", "city", "company", "company_address"] }}
    }

and other variants like,

"fields": {"includes":["customer_name", "city", "company", "company_address"] }

# or 

"_source":{"includes":["customer_name", "city", "company", "company_address"] }

# and several others.

I keep getting error:

    raise HTTP_EXCEPTIONS.get(status_code, TransportError)(
elasticsearch.exceptions.RequestError: RequestError(400, 'parsing_exception', '[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]')

I followed:

What am I missing here?

CodePudding user response:

It looks like from the JSON you provided your _source field is landing inside your bool query. Instead you should have it structured like so:

search_body = {
  "query": {
    "bool": [
      {...}
    ]
   },
   "_source": {
     "includes": [...]
   }
}

(disclaimer: I'm the maintainer of the Python Elasticsearch client and work for Elastic)

CodePudding user response:

Try this:

results = es.search(index="some_index", query=search_body, source_includes=[...])

Code is the best documentation (sometimes!)

  •  Tags:  
  • Related