Rolando
Rolando

Reputation: 62704

How to filter fields returned after search in elasticsearch?

Assuming I have a documents indexed like the following:

{
    category: "fruits",
    items: [
        {
            name: "Apple",
            shape: "Round",
            color: "red"
        },
        {
            ...
        }
    ]
}

Assuming I want to search across all the fields but only return either:

  1. items field only
  2. items field, but only the name and shape fields (not the color field in the list)

Upvotes: 0

Views: 772

Answers (2)

BlackPOP
BlackPOP

Reputation: 5747

You can restrict using field in query.. Like

   {
    Query:{
                 Bla bla bla
                 }, 
   Fields :["items . name", "items.shape"] 
     } 

Upvotes: 1

pickypg
pickypg

Reputation: 22332

You can filter in/out fields from the source (entire document) by specifying the reserved _source field:

To only retrieve the items, then specify a filter like so:

{
    "_source" : "items.*",
    "query" : ...
}

To retrieve the specific items of interest, then there are multiple ways, but if you know them all (rather than needing wildcard exclude support):

{
    "_source" : [ "items.name", "items.shape" ],
    "query" : ...
}

Alternatively, if it wasn't so simple:

{
    "_source" : {
        "include" : [ "items.*" ],
        "exclude" : [ "items.color" ]
    }
    "query" : ...
}

This gives the most flexibility.

Upvotes: 2

Related Questions