3
votes

I am wondering if it is possible to return virtual fields somehow in an elasticsearch query, via arguments or the document mapping.

Currently I am trying to use elasticsearch-langdetect plugin for the detection of content languages. It creates subfields that contain the language of the documents. What I want to do is query the documents again and index them in another elasticsearch index, that is language specific, without inserting any documents in any language specific index twice.

In all the examples, the detected language is only queried, never returned.

How can non stored properties, that are not in the original document, be returned via elasticsearch search results? Or is that not possible in elasticsearch. I have searched the documentation, and only found the fields, script_fields and field_data fields, that all do something else and work AFAIK only on stored fields.

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-fields.html

Can I store fields that are calculated from the source data?

1

1 Answers

1
votes

As this inofmration is not present in the _source , you wont be directly able to get hold of it. But then there is an alternative. You can use script field to read this value from field data cache.

{
    "query" : {
        "match_all" : {}
    },
    "script_fields" : {
        "langauge" : {
            "script" : "doc['lang'].value"
        }
    }
}

You can see more info here and here.