Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

How to exclude certain fields from the _source field in ElasticSearch

I have following simple snippet:

PUT /lib36
{
  "mappings": {
    "_source": {"enabled": false}, 
    "properties": {
      "name": {
        "type": "text"
      },
      "description":{
        "type": "text"
      }
      
    }
  }
}

PUT /lib36/_doc/1
{
  "name":"abc",
  "description":"xyz"
}


POST /lib36/_search
{
  "query": {
    "match": {
      "name": "abc"
    }
  }
}

With "_source": {"enabled": false}, the queried result doesn't include _source field.

I would to know how to write the query that the query result has the _source field ,but only contain name field, but not description field.

Thanks!

question from:https://stackoverflow.com/questions/66060933/how-to-exclude-certain-fields-from-the-source-field-in-elasticsearch

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use the source filtering of elasticsearch, but for that first you need to have _source enabled in your mapping.

You need to have below key-value in your search query JSON. below search will exclude all other fields apart from name.

{
  "query": {
    "match": {
      "name": "abc"
    }
  },
  "_source": [
    "name"
  ],
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...