Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API: Add response filtering with filter_path parameter #10980

Merged
merged 1 commit into from May 26, 2015

Commits on May 26, 2015

  1. API: Add response filtering with filter_path parameter

    This change adds a new "filter_path" parameter that can be used to filter and reduce the responses returned by the REST API of elasticsearch.
    
    For example, returning only the shards that failed to be optimized:
    ```
    curl -XPOST 'localhost:9200/beer/_optimize?filter_path=_shards.failed'
    {"_shards":{"failed":0}}%
    ```
    
    It supports multiple filters (separated by a comma):
    ```
    curl -XGET 'localhost:9200/_mapping?pretty&filter_path=*.mappings.*.properties.name,*.mappings.*.properties.title'
    ```
    
    It also supports the YAML response format. Here it returns only the `_id` field of a newly indexed document:
    ```
    curl -XPOST 'localhost:9200/library/book?filter_path=_id' -d '---hello:\n  world: 1\n'
    ---
    _id: "AU0j64-b-stVfkvus5-A"
    ```
    
    It also supports wildcards. Here it returns only the host name of every nodes in the cluster:
    ```
    curl -XGET 'http://localhost:9200/_nodes/stats?filter_path=nodes.*.host*'
    {"nodes":{"lvJHed8uQQu4brS-SXKsNA":{"host":"portable"}}}
    ```
    
    And "**" can be used to include sub fields without knowing the exact path. Here it returns only the Lucene version of every segment:
    ```
    curl 'http://localhost:9200/_segments?pretty&filter_path=indices.**.version'
    {
      "indices" : {
        "beer" : {
          "shards" : {
            "0" : [ {
              "segments" : {
                "_0" : {
                  "version" : "5.2.0"
                },
                "_1" : {
                  "version" : "5.2.0"
                }
              }
            } ]
          }
        }
      }
    }
    ```
    
    Note that elasticsearch sometimes returns directly the raw value of a field, like the _source field. If you want to filter _source fields, you should consider combining the already existing _source parameter (see Get API for more details) with the filter_path parameter like this:
    
    ```
    curl -XGET 'localhost:9200/_search?pretty&filter_path=hits.hits._source&_source=title'
    {
      "hits" : {
        "hits" : [ {
          "_source":{"title":"Book elastic#2"}
        }, {
          "_source":{"title":"Book elastic#1"}
        }, {
          "_source":{"title":"Book elastic#3"}
        } ]
      }
    }
    ```
    tlrx committed May 26, 2015
    Copy the full SHA
    ce63590 View commit details
    Browse the repository at this point in the history