Eugene Shevchugov
Eugene Shevchugov

Reputation: 77

ElasticSearch Specific JSON

Im not strong in ElasticSearch, and cant find asnwer on my question, so...

I have doc in my index, like this

$ curl -XGET "$ES_URL/please/_search?pretty"
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "please",
        "_type" : "_doc",
        "_id" : "pleaseworkforme14",
        "_score" : 1.0,
        "_source" : {
          "_class" : "com.example.test.ind.info.ElasticData",
          "f_data" : "123",
          "s_data" : "Everyone4",
          "text" : "Blue\n"
        }
      }
    ]
  }
}

And I want to get _score from this doc, but without "_class" field.. So, what I do:

$ curl -XGET "$ES_URL/please/_search?pretty" -H "Content-Type: application/json" -d @json.txt

json.txt:

{
 "script_fields": {
   "total_goals": {
     "script": {
       "lang": "painless",
       "inline": "return params._source;"
     }
   }
 }
}

With this script I actually getting full _source, but I want to take only f_data, s_data and text.

I try to do it with JSON.stringify, but get error

$ curl -XGET "$ES_URL/please/_search?pretty" -H "Content-Type: application/json" -d @json.txt
{
  "error" : {
    "root_cause" : [
      {
        "type" : "script_exception",
        "reason" : "compile error",
        "script_stack" : [
          "return JSON.stringify(params._so ...",
          "       ^---- HERE"
        ],
        "script" : "return JSON.stringify(params._source.f_data);",
        "lang" : "painless",
        "position" : {
          "offset" : 7,
          "start" : 0,
          "end" : 32
        }
      }
    ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query",
    "grouped" : true,
    "failed_shards" : [
      {
        "shard" : 0,
        "index" : "bitwise",
        "node" : "1NsqGhl7QKGXWTqmYXew-g",
        "reason" : {
          "type" : "script_exception",
          "reason" : "compile error",
          "script_stack" : [
            "return JSON.stringify(params._so ...",
            "       ^---- HERE"
          ],
          "script" : "return JSON.stringify(params._source.f_data);",
          "lang" : "painless",
          "position" : {
            "offset" : 7,
            "start" : 0,
            "end" : 32
          },
          "caused_by" : {
            "type" : "illegal_argument_exception",
            "reason" : "Variable [JSON] is not defined."
          }
        }
      }
    ]
  },
  "status" : 400
}

Upvotes: 0

Views: 37

Answers (1)

Val
Val

Reputation: 217274

You don't need to use script_fields to return source fields, you simply need to use source filtering.

What you need to run is simply the following

$ curl -XGET "$ES_URL/please/_search?pretty&_source=f_data,s_data,text"

or if you want to keep your JSON body, it needs to contain the following:

{
  "_source": ["f_data", "s_data", "text"],
  "query": {
    "match_all": {}
  }
}

Upvotes: 1

Related Questions