4

Having the following mapping:

curl -XPUT 'localhost:9200/testidx?pretty=true' -d '{
  "mappings": {
    "items": {
       "dynamic": "strict",
       "properties" : {
            "title" : { "type": "string" },
            "body" : { "type": "string" }
}}}}'

I put two items on it:

curl -XPUT 'localhost:9200/testidx/items/1' -d '{
  "title": "Titulo anterior",
  "body": "blablabla blablabla blablabla blablabla blablabla blablabla"
}'

curl -XPUT 'localhost:9200/testidx/items/2' -d '{
  "title": "Joselr",
  "body": "Titulo stuff more stuff" 
}'

Now I want to search the word titulo on every field but body, so what I do is (following this post):

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

It's supposed to show only the 1 item, as the second one has the word Titulo but it's on the body and that's what I want to ignore. How can archive this?

PS: This is just a simple example, I've a mapping with a lot of properties and I want to ignore some of them in some searches.
PS2: I'm using ES 2.3.2

Community
  • 1
  • 1
Avión
  • 7,963
  • 11
  • 64
  • 105

2 Answers2

7

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"]
     }
}'
Val
  • 207,596
  • 13
  • 358
  • 360
  • Thanks, Val. I'm looking for something like the second answer. The first one needs to list the other fields and that's what I want to avoid. The second answer seems valid with a `query_string`, but how can I make the query if I'm using `term` or `terms`? If I'm not wrong, that `-body` to exclude a field is only available in `query_string`, right? – Avión Sep 12 '16 at 13:52
  • Does the second approach mean that the word "Titulo" should not be present in body or it does'nt cares whether it is present in the body or not? – Richa Sinha Oct 10 '18 at 10:45
  • @RichaSinha yes that's correct, the results won't include documents with a body containing Titulo – Val Oct 10 '18 at 10:54
  • Is there any way to just ignore, that means it should not care for whether it is present in body or not. So let us say i am running a query on all fields and i exclude body it should fetch me results where query string is present in the other fields and it should care about whether the query string is present in body or not. – Richa Sinha Oct 11 '18 at 08:53
  • @RichaSinha I'm not sure about your precise requirement, but feel free to create a new question with the exact details ans some sample documents/queries. – Val Oct 11 '18 at 08:54
  • @Val I have created a new question. U might wanna have a look into it - https://stackoverflow.com/questions/52757277/how-to-exclude-a-field-from-getting-searched-by-elasticsearch-6-1 – Richa Sinha Oct 11 '18 at 09:59
0

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.)

dr0i
  • 2,380
  • 2
  • 19
  • 36