我正在使用elasticsearch索引我的文档。
是否可以指示它只返回特定字段而不是它存储的整个json文档?
答案 0 :(得分:470)
是的!使用source filter。如果您正在使用JSON进行搜索,它将看起来像这样:
{
"_source": ["user", "message", ...],
"query": ...,
"size": ...
}
在ES 2.4及更早版本中,您还可以使用fields option to the search API:
{
"fields": ["user", "message", ...],
"query": ...,
"size": ...
}
这在ES 5+中已弃用。无论如何,源过滤器更强大!
答案 1 :(得分:77)
我发现get api
的文档很有帮助 - 尤其是两个部分,源过滤和字段:http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-get.html
他们说明了源过滤:
如果您只需要完整_source中的一个或两个字段,则可以 使用_source_include& _source_exclude参数包含或 过滤掉你需要的部分。这可能特别有用 部分检索可以节省网络开销的大型文档
完全适合我的用例。我最终只是像这样过滤源(使用速记):
{
"_source": ["field_x", ..., "field_y"],
"query": {
...
}
}
仅供参考,他们在文档中说明字段参数:
get操作允许指定一组存储的字段 通过传递fields参数返回。
它似乎迎合了专门存储的字段,它将每个字段放在一个数组中。如果指定的字段尚未存储,则会从_source中获取每个字段,这可能导致“更慢”。检索。我也很难尝试让它返回对象类型的字段。
总而言之,您有两种选择,无论是源过滤还是[存储]字段。
答案 2 :(得分:16)
For the ES versions 5.X and above you can a ES query something like this
GET /.../...
{
"_source": {
"includes": [ "FIELD1", "FIELD2", "FIELD3" ... " ]
},
.
.
.
.
}
答案 3 :(得分:9)
在Elasticsearch 5.x中,不推荐使用上述方法。 您可以使用_source方法,但在某些情况下,存储字段是有意义的。例如,如果您的文档包含标题,日期和非常大的内容字段,您可能只想检索标题和日期,而无需从大型_source字段中提取这些字段:
在这种情况下,您可以使用:
{
"size": $INT_NUM_OF_DOCS_TO_RETURN,
"stored_fields":[
"doc.headline",
"doc.text",
"doc.timestamp_utc"
],
"query":{
"bool":{
"must":{
"term":{
"doc.topic":"news_on_things"
}
},
"filter":{
"range":{
"doc.timestamp_utc":{
"gte":1451606400000,
"lt":1483228800000,
"format":"epoch_millis"
}
}
}
}
},
"aggs":{
}
}
请参阅有关如何索引存储字段的文档。 对Upvote感到高兴!
答案 4 :(得分:7)
所有REST API都接受可用于的filter_path参数 减少elasticsearch返回的响应。这个参数需要一个 逗号分隔的过滤器列表,用点表示法表示。
答案 5 :(得分:6)
这是另一种解决方案,现在使用匹配表达式
Source filtering
Allows to control how the _source field is returned with every hit.
使用Elastiscsearch 5.5版进行测试
关键字“包含”定义细节字段。
GET /my_indice/my_indice_type/_search
{
"_source": {
"includes": [ "my_especific_field"]
},
"query": {
"bool": {
"must": [
{"match": {
"_id": "%my_id_here_without_percent%"
}
}
]
}
}
}
答案 6 :(得分:5)
here you can specify whichever field you want in your output and also which you don't.
POST index_name/_search
{
"_source": {
"includes": [ "field_name", "field_name" ],
"excludes": [ "field_name" ]
},
"query" : {
"match" : { "field_name" : "value" }
}
}
答案 7 :(得分:3)
可以使用' _source'进行REST API GET请求。参数。
示例请求
http://localhost:9200/opt_pr/_search?q=SYMBOL:ITC AND OPTION_TYPE=CE AND TRADE_DATE=2017-02-10 AND EXPIRY_DATE=2017-02-23&_source=STRIKE_PRICE
响应
{
"took": 59,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 104,
"max_score": 7.3908954,
"hits": [
{
"_index": "opt_pr",
"_type": "opt_pr_r",
"_id": "AV3K4QTgNHl15Mv30uLc",
"_score": 7.3908954,
"_source": {
"STRIKE_PRICE": 160
}
},
{
"_index": "opt_pr",
"_type": "opt_pr_r",
"_id": "AV3K4QTgNHl15Mv30uLh",
"_score": 7.3908954,
"_source": {
"STRIKE_PRICE": 185
}
},
{
"_index": "opt_pr",
"_type": "opt_pr_r",
"_id": "AV3K4QTgNHl15Mv30uLi",
"_score": 7.3908954,
"_source": {
"STRIKE_PRICE": 190
}
},
{
"_index": "opt_pr",
"_type": "opt_pr_r",
"_id": "AV3K4QTgNHl15Mv30uLm",
"_score": 7.3908954,
"_source": {
"STRIKE_PRICE": 210
}
},
{
"_index": "opt_pr",
"_type": "opt_pr_r",
"_id": "AV3K4QTgNHl15Mv30uLp",
"_score": 7.3908954,
"_source": {
"STRIKE_PRICE": 225
}
},
{
"_index": "opt_pr",
"_type": "opt_pr_r",
"_id": "AV3K4QTgNHl15Mv30uLr",
"_score": 7.3908954,
"_source": {
"STRIKE_PRICE": 235
}
},
{
"_index": "opt_pr",
"_type": "opt_pr_r",
"_id": "AV3K4QTgNHl15Mv30uLw",
"_score": 7.3908954,
"_source": {
"STRIKE_PRICE": 260
}
},
{
"_index": "opt_pr",
"_type": "opt_pr_r",
"_id": "AV3K4QTgNHl15Mv30uL5",
"_score": 7.3908954,
"_source": {
"STRIKE_PRICE": 305
}
},
{
"_index": "opt_pr",
"_type": "opt_pr_r",
"_id": "AV3K4QTgNHl15Mv30uLd",
"_score": 7.381078,
"_source": {
"STRIKE_PRICE": 165
}
},
{
"_index": "opt_pr",
"_type": "opt_pr_r",
"_id": "AV3K4QTgNHl15Mv30uLy",
"_score": 7.381078,
"_source": {
"STRIKE_PRICE": 270
}
}
]
}
}
答案 8 :(得分:2)
是的,通过使用源过滤器,您可以完成此操作,这是文档source-filtering
示例请求
<UserControl.Resources><local:IndexToBoolConverter x:Key="IndexToBoolConverter"/></UserControl.Resources>
输出将为
POST index_name/_search
{
"_source":["field1","filed2".....]
}
答案 9 :(得分:1)
在java中,您可以像这样使用setFetchSource:
client.prepareSearch(index).setTypes(type)
.setFetchSource(new String[] { "field1", "field2" }, null)
答案 10 :(得分:1)
有多种方法可用于实现特定领域的结果。 一种是通过来源方法。 另一种方法是filter_path:
文档 Json:
"hits" : [
{
"_index" : "xxxxxx",
"_type" : "_doc",
"_id" : "xxxxxx",
"_score" : xxxxxx,
"_source" : {
"year" : 2020,
"created_at" : "2020-01-29",
"url" : "www.github.com/mbarr0987",
"name":"github"
}
}
查询:
GET bot1/_search?filter_path=hits.hits._source.url
{
"query": {
"bool": {
"must": [
{"term": {"name.keyword":"github" }}
]
}
}
}
输出:
{
"hits" : {
"hits" : [
{
"_source" : {
"url" : "www.github.com/mbarr0987"
}
}
]
}
}
答案 11 :(得分:0)
例如,您有一个包含三个字段的文档:
PUT moive/_doc/1
{
"name":"The Lion King",
"language":"English",
"score":"9.3"
}
如果要返回name
和score
,可以使用以下命令:
GET moive/_doc/1?_source_includes=name,score
如果要获取带有模式的字段:
GET moive/_doc/1?_source_includes=*re
也许除了某些字段:
GET moive/_doc/1?_source_excludes=score
答案 12 :(得分:0)
如果您知道sql,请编写查询以获取代码的值,例如sql查询等效项和elasticsearch查询
POST /_sql/translate
{
"query": "select name,surname from users"
}
结果是,请仔细查看包含键
{
"size" : 1000,
"_source" : {
"includes" : [
"name",
"surname"
],
"excludes" : [ ]
},
"sort" : [
{
"_doc" : {
"order" : "asc"
}
}
]
}