No more _source if script_fields is used in elasti

2019-02-03 07:34发布

问题:

I am running a simple query like so:

{
  "query": {
    "term": {
      "statuses": "active"
    }
  },
  "script_fields": {
    "test": {
      "script": "_source.name"
    }
  }
}

The problem is that once I introduce the script_fields, I no longer get _source in my results.

I have tried:

{
  "fields": [
    "_all"
  ],
  "query": {
    "term": {
      "statuses": "active"
    }
  },
  "script_fields": {
    "email": {
      "script": "_source.name"
    }
  }
}

and

{
  "fields": [
    "*"
  ],
  "query": {
    "term": {
      "statuses": "active"
    }
  },
  "script_fields": {
    "email": {
      "script": "_source.name"
    }
  }
}

But they did not make any difference. Is there a way to get _source returned in addition to the script_fields?

回答1:

In the fields array, make it load _source:

{
  "stored_fields": [
    "_source"
  ],
  "query": {
    "term": {
      "statuses": "active"
    }
  },
  "script_fields": {
    "email": {
      "script": "_source.name"
    }
  }
}


回答2:

This works for me:

curl -X DELETE localhost:9200/a

curl -X POST localhost:9200/a/b/c -d '{"title" : "foo"}'

curl -X POST localhost:9200/a/_refresh

echo;

curl localhost:9200/a/_search?pretty -d '{
  "fields": [
    "_source"
  ],
  "query": {
    "match_all": {}
  },
  "script_fields": {
    "title_script": {
      "script": "_source.title"
    }
  }
}'

Output:

"hits" : {
  # ...
  "hits" : [ {
    # ...
    "_source" : {"title" : "foo"},
    "fields" : {
      "title_script" : "foo"
    }
  } ]
}