38

I know to use the fields setting to include just the fields I want in a search http://www.elasticsearch.org/guide/reference/api/search/fields/

... but I was wondering if I could do the opposite ... somehow specify one or two fields that I don't want included in the query results (like an attachment, for example). It just seems painful to have to type out all the fields I want minus one or two, when I could just specify fields to exclude

concept47
  • 30,257
  • 12
  • 52
  • 74
  • 2
    Just to be clear, the "fields" parameter you linked to doesn't affect the fields the query is run against, just what is returned... (I think you know this, more for other people reading.) – ramseykhalaf Aug 15 '13 at 11:07
  • Yes I knew that, thanks for clarifying though. – concept47 Aug 17 '13 at 23:33
  • Does this answer your question? [Make elasticsearch only return certain fields?](https://stackoverflow.com/questions/9605292/make-elasticsearch-only-return-certain-fields) – Aaron_ab Dec 16 '19 at 12:24

2 Answers2

42

You can use Source Filtering (Tested in v. 1.6 and v. 1.7): https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-source-filtering.html

{
    "_source": {
        "include": [ "obj1.*", "obj2.*" ],
        "exclude": [ "*.description" ]
    },
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

You can also use it in GET request:

curl "localhost:9200/myindex/mytype/66a8f299870b4cab?_source_exclude=file._content&pretty"

The previous example exclude the file content in an attachment field.

Roberto
  • 8,586
  • 3
  • 42
  • 53
  • Could you explain, please, the wildcard selector in this line: `"exclude": [ "*.description" ]`? Is `*.description` the same as (rewritten in regex): `.*\.description`? Thanks! – crollywood Apr 18 '17 at 12:10
  • Hi @crollywood, the example is taken from ES official doc, It excludes all "description" subfields, so I think that your regexp would be correct. – Roberto Apr 19 '17 at 09:45
  • `include` and `exclude` are deprecated now in the `_source` object, they are replaced by `includes` and `excludes` respectively. – gaborsch Feb 23 '22 at 10:32
  • My understanding is that _source only affects which fields are returned in the results, not which field are actually searched. – David Medinets Jan 10 '23 at 18:27
8

Did you see the documentation for ‛partial‛ on the same page you linked in your question? That allows you to do what you want, albeit only on ‛_source‛ fields I believe. See https://www.elastic.co/guide/en/elasticsearch/reference/1.7/search-request-fields.html

When loading data from _source, partial fields can be used to use wildcards to control what part of the _source will be loaded based on include and exclude patterns.

Both include and exclude support multiple patterns:

{
    "query" : {
        "match_all" : {}
    },
    "partial_fields" : {
        "partial1" : {
            "include" : ["obj1.obj2.*", "obj1.obj4.*"],
            "exclude" : "obj1.obj3.*"
        }
    }
}
Magnus Reftel
  • 967
  • 6
  • 19
James Addison
  • 3,086
  • 1
  • 17
  • 16
  • 1
    Well you could quote that documentation at the very least. Even if the OP missed something obvious, this is more of a comment than an answer. – Paul Bellora Aug 14 '13 at 05:16
  • Yeah I saw that, tried it but it didn't work, so I'm probably doing something wrong, just don't know what :\ – concept47 Aug 15 '13 at 01:43
  • 1
    @PaulBellora - fair enough. I was on a mobile device at the time of posting, so apologies to concept47. – James Addison Aug 17 '13 at 03:15
  • @concept47, did you consider doing an `exclude` as you have in your Google Groups posting, but also do an `include` for `*`? Not sure if that's possible, but maybe? (It's a reach, I'll admit) – James Addison Aug 17 '13 at 03:17
  • 1
    Well the problem was that all the fields were being returned anyway, so I didn't think that would impact it, but I'll try it out too – concept47 Aug 17 '13 at 23:34
  • I am very confused, I just tried out the posted answer, both with and without the `"include"` section, and it worked for me. Can you post a Gist if you still need help. – ramseykhalaf Aug 18 '13 at 05:44
  • I had to use the "query" close to enclose all of it, otherwise "exclude" doesn't do anything. – szeitlin Apr 04 '22 at 20:32