Exclude a field on a Elasticsearch query

前端 未结 2 1428
悲哀的现实
悲哀的现实 2021-01-21 06:03

Having the following mapping:

curl -XPUT \'localhost:9200/testidx?pretty=true\' -d \'{
  \"mappings\": {
    \"items\": {
       \"dynamic\": \"strict\",
                


        
相关标签:
2条回答
  • 2021-01-21 06:12

    Up to elasticsearch 6.0.0 you can set "include_in_all": false to your index field properties, see e.g. https://www.elastic.co/guide/en/elasticsearch/reference/5.5/include-in-all.html.

    (This of course needs a reindexing of the data.)

    0 讨论(0)
  • 2021-01-21 06:17

    The _source/exclude setting is only useful for not returning the body field in the response, but that doesn't exclude that field from being searched.

    What you can do is to specify all the fields you want to search instead (whitelist approach)

    curl -XGET 'localhost:9200/testidx/items/_search?pretty=true' -d '{
      "query" : {
         "query_string": {
              "fields": ["title", "field2", "field3"],      <-- add this
              "query": "Titulo"
         }},
         "_source" : {
              "exclude" : ["*.body"]
         }
    }'
    

    Another thing you can do is to explicitly specify that body should not be matched with -body:Titulo

    curl -XGET 'localhost:9200/testidx/items/_search?pretty=true' -d '{
      "query" : {
         "query_string": {
              "query": "Titulo AND -body:Titulo"                <-- modify this
         }},
         "_source" : {
              "exclude" : ["*.body"]
         }
    }'
    
    0 讨论(0)
提交回复
热议问题