Home > Net >  Concat queried values from Elastic Search to avoid creating a new field
Concat queried values from Elastic Search to avoid creating a new field

Time:11-04

I'm querying a field with Elastic search. The field can be "Small", "Medium", "Average", "Big".

But i want "Small" and "Medium" to return "Yes" And "Average" and "Big" to return "No"

Is there any way to do this in the query ? Or using the list component in reactive search ?

Thanks

CodePudding user response:

You can use script_field

{
  "_source": "*", 
   "script_fields": {
     "custom_field": {
       "script": {
         "source":  """
                        if(doc['description.keyword'].value=='Small' || doc['description.keyword'].value=='Medium')
                          return 'Yes';
                        else
                          return 'No';
                    """
       }
     }
   }
}

Result

"hits" : [
      {
        "_index" : "index68",
        "_type" : "_doc",
        "_id" : "GyEhPYQBJutE-yZceoEO",
        "_score" : 1.0,
        "_source" : {
          "description" : "Small",
          "title" : "A"
        },
        "fields" : {
          "custom_field" : [
            "Yes"
          ]
        }
      },
      {
        "_index" : "index68",
        "_type" : "_doc",
        "_id" : "HCEhPYQBJutE-yZcpoGE",
        "_score" : 1.0,
        "_source" : {
          "description" : "Medium",
          "title" : "B"
        },
        "fields" : {
          "custom_field" : [
            "Yes"
          ]
        }
      },
      {
        "_index" : "index68",
        "_type" : "_doc",
        "_id" : "HSEhPYQBJutE-yZctoHu",
        "_score" : 1.0,
        "_source" : {
          "description" : "Average",
          "title" : "B"
        },
        "fields" : {
          "custom_field" : [
            "No"
          ]
        }
      },
      {
        "_index" : "index68",
        "_type" : "_doc",
        "_id" : "HiEhPYQBJutE-yZc04Fx",
        "_score" : 1.0,
        "_source" : {
          "description" : "Big",
          "title" : "C"
        },
        "fields" : {
          "custom_field" : [
            "No"
          ]
        }
      }
    ]
  • Related