如果在Elasticsearch查询中使用script_fields,将不再返回_source。

27

我正在运行一个简单的查询,例如:

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

问题是一旦我加入了script_fields,我的结果中就不再有_source

我尝试过:

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

并且

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

但是它们并没有产生任何区别。有没有一种方法可以在返回script_fields的同时也返回_source

2个回答

33
fields数组中,使其加载_source
{
  "stored_fields": [
    "_source"
  ],
  "query": {
    "term": {
      "statuses": "active"
    }
  },
  "script_fields": {
    "email": {
      "script": "_source.name"
    }
  }
}

有人知道这是为什么吗?这和 https://github.com/elastic/elasticsearch/issues/20068 有关吗? - Henry C

1
这对我来说有效:
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"
    }
  }
}'

输出:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接