Martin
Martin

Reputation: 648

Get all fields of a document in ElasticSearch search query

How can I get all fields in documents matched by search query? ES documentation on fields says that using *, one can get all fields: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-fields.html

Having this document and this query, I get hit in result, but no fields are returned:

Put document:

curl -XPUT http://localhost:9200/idx/t/doc1 -d '{
  "f": "value"
}'

Search it:

curl -XPOST http://localhost:9200/idx/_search?pretty -d '{
  "fields": "*",
  "query": { "term" : { "f" : "value" }}
}'

I tried also ["*"], but the result is the same, only default fields (_id and _type) are returned. The hits part of response looks like this:

"hits" : {
    "total" : 1,
    "max_score" : 0.30685282,
    "hits" : [ {
      "_index" : "idx",
      "_type" : "t",
      "_id" : "doc1",
      "_score" : 0.30685282
    } ]
  }

Upvotes: 2

Views: 11629

Answers (2)

children1987
children1987

Reputation: 41

I am facing this problem, too. I found out that if I just search the text or keyword fields, everything is OK. Hope this may help you.

Upvotes: 1

Alain Collins
Alain Collins

Reputation: 16362

The doc actually says:

"* can be used to load all stored fields from the document."

The core types doc says that the default for storing fields is 'false'.

Since by default ElasticSearch stores all fields of the source document in the special _source field, this option is primarily useful when the _source field has been disabled in the type definition. Defaults to false.

If you don't specify 'fields' in your search, you can see what's in _source.

So, if you want to return it as a field, change your mapping to store the field.

Upvotes: 3

Related Questions