首页 文章

使elasticsearch只返回某些字段?

提问于
浏览
290

我正在使用elasticsearch索引我的文档 .

是否可以指示它只返回特定字段而不是它存储的整个json文档?

10 回答

  • 5

    response_filtering

    所有REST API都接受filter_path参数,该参数可用于减少elasticsearch返回的响应 . 此参数采用逗号分隔的过滤器列表,以点表示法表示 .

    https://stackoverflow.com/a/35647027/844700

  • 1

    这是另一种解决方案,现在使用匹配表达式

    Source filtering Allows to control how the _source field is returned with every hit.

    使用Elastiscsearch 5.5版测试

    关键字 "includes" 定义了细节字段 .

    GET /my_indice/my_indice_type/_search
    {
        "_source": {
            "includes": [ "my_especific_field"]
            },
            "query": {
            "bool": {
                    "must": [
                    {"match": {
                        "_id": "%my_id_here_without_percent%"
                        }
                    }
                ]
            }
        }
    }
    
  • 15
    For the ES versions 5.X and above you can a ES query something like this
    
        GET /.../...
        {
          "_source": {
            "includes": [ "FIELD1", "FIELD2", "FIELD3" ... " ]
          },
          .
          .
          .
          .
        }
    
  • 1
    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" }
            }
        }
    
  • 6

    可以使用'_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
                }
            }
        ]
    }
    

    }

  • 3

    是的!使用source filter . 如果你're searching with JSON it' ll看起来像这样:

    {
        "_source": ["user", "message", ...],
        "query": ...,
        "size": ...
    }
    

    在ES 2.4及更早版本中,您还可以使用fields option to the search API

    {
        "fields": ["user", "message", ...],
        "query": ...,
        "size": ...
    }
    

    这在ES 5中已弃用 . 无论如何,源过滤器更强大!

  • 75

    在java中,您可以像这样使用setFetchSource:

    client.prepareSearch(index).setTypes(type)
                .setFetchSource(new String[] { "field1", "field2" }, null)
    
  • 1

    是的,使用源过滤器可以完成此操作,这里是文档source-filtering

    示例请求

    POST index_name/_search
     {
       "_source":["field1","filed2".....] 
     }
    

    输出将是

    {
      "took": 57,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": 1,
        "max_score": 1,
        "hits": [
          {
            "_index": "index_name",
            "_type": "index1",
            "_id": "1",
            "_score": 1,
            "_source": {
              "field1": "a",
              "field2": "b"
            },
            {
              "field1": "c",
              "field2": "d"
            },....
          }
        ]
      }
    }
    
  • 401

    我发现 get api 的文档很有帮助 - 尤其是两个部分, Source filteringFieldshttp://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-get.html

    他们陈述了源过滤:

    如果您只需要完整_source中的一个或两个字段,则可以使用_source_include&_source_exclude参数来包含或过滤掉您需要的部分 . 这对于大型文档尤其有用,其中部分检索可以节省网络开销

    这完全适合我的用例 . 我最终只是简单地过滤源(使用速记):

    {
        "_source": ["field_x", ..., "field_y"],
        "query": {      
            ...
        }
    }
    

    仅供参考,他们在有关 fields 参数的文档中说明:

    get操作允许指定将通过传递fields参数返回的一组存储字段 .

    它似乎迎合了专门存储的字段,它将每个字段放在一个数组中 . 如果尚未存储指定的字段,它将从_source中获取每个字段,这可能导致“较慢”的检索 . 我也很难尝试让它返回类型对象的字段 .

    总而言之,您有两种选择,无论是源过滤还是[存储]字段 .

  • 2

    在Elasticsearch 5.x中,不推荐使用上述方法 . 您可以使用_source方法,但在某些情况下,存储字段是有意义的 . 例如,如果您有一个包含 Headers ,日期和非常大的内容字段的文档,您可能只想检索 Headers 和日期,而无需从大型_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总是很开心!

相关问题