elasticsearch dynamic query - Add another field to each document returned

前端 未结 1 1402
逝去的感伤
逝去的感伤 2021-02-06 14:11

What I need is very simple, but I am unable to find how to do it in Elasticsearch, possibly because of the complexity of what is required to be done.

Input (two sample J

相关标签:
1条回答
  • 2021-02-06 14:31

    I did this using script fields. Refer:

    http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-script-fields.html

    sample query:

    query: {
    ...
    },
    script_fields: {
      inventory: {
         script: "doc['car'].value + doc['bike'].value"
      }
    }
    

    This would result in a separate fields column with each hit as:

    fields: {
        inventory: [450]
    }
    

    But, since I also wanted this to be sorted, I ended up using the sort:

    query: {
    ...
    },
    sort: {
        _script: {
            script: "doc['car'].value + doc['bike'].value",
            type: "number",
            order: "desc"
        }
    }
    

    which returned me a sort field with each hit, such as:

    sort: [450]
    
    0 讨论(0)
提交回复
热议问题