I have documents with loads of fields:
{
"a1":"v1",
"a2":null,
"f1":"v3",
"f2":"",
"f3":"v3"
}
I would like to get a list of existing fields
for the above the result would be : a1,f1,f3
The closest I came is to specify all the fields by hand into an exists query like this:
"a1_fields": {
"filter": {
"exists": {
"field": "a1"
}
}
} ..etc
this will return :
"a1_fields": {
"doc_count": 1
} ..etc
however I dont want to specify the fields by hand, but using wildcard the result won’t contain the breakdowns by the fieldnames:
"all_fields": {
"filter": {
"exists": {
"field": "*"
}
}
} ..etc
So the question is how to get all the existing field names for a set of documents?
4
On _search
On search you can specify in the body of the request fields: ["*"]
. This will return all field & value indexed for the current document.
demo
PUT 79250824
{
"mappings": {
"dynamic": false,
"properties": {
"a1":{"type":"keyword"},
"a2":{"type":"keyword"},
"a3":{"type":"keyword", "index": false}
}
}
}
POST 79250824/_bulk
{"index":{}}
{"a1": "v1", "a2": "v2", "a3": "v3"}
{"index":{}}
{"a1": "v1", "a3": "v3"}
{"index":{}}
{"a1": "v1", "a2": null, "a3": "v3"}
{"index":{}}
{"a1": "v1", "new_field": "c2"}
GET 79250824/_search
{
"query": {"match_all":{}},
"fields": [
"*"
]
}
You should get per document the fields that have been indexed mapped:
{
"took": 8,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "79250824",
"_id": "Lf-BkZMBD2NlCdx5IRpu",
"_score": 1,
"_source": {
"a1": "v1",
"a2": "v2",
"a3": "v3"
},
"fields": {
"a1": [
"v1"
],
"a2": [
"v2"
],
"a3": [
"v3"
]
}
},
{
"_index": "79250824",
"_id": "Lv-BkZMBD2NlCdx5IRpu",
"_score": 1,
"_source": {
"a1": "v1",
"a3": "v3"
},
"fields": {
"a1": [
"v1"
],
"a3": [
"v3"
]
}
},
{
"_index": "79250824",
"_id": "L_-BkZMBD2NlCdx5IRpu",
"_score": 1,
"_source": {
"a1": "v1",
"a2": null,
"a3": "v3"
},
"fields": {
"a1": [
"v1"
],
"a3": [
"v3"
]
}
},
{
"_index": "79250824",
"_id": "MP-BkZMBD2NlCdx5IRpu",
"_score": 1,
"_source": {
"a1": "v1",
"new_field": "c2"
},
"fields": {
"a1": [
"v1"
]
}
}
]
}
}
4