# Exclude searching on specific fields

**URL:** <https://discuss.elastic.co/t/exclude-searching-on-specific-fields/51434>\
**Category:** Elasticsearch\
**Created:** [May 31, 2016, 1:37pm UTC](https://discuss.elastic.co/t/exclude-searching-on-specific-fields/51434 "2016-05-31T13:37:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vikas\_Mestry](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vikas_mestry/32/42301_2.png) [@Vikas\_Mestry](https://discuss.elastic.co/u/Vikas_Mestry)\
**Post date:** [May 31, 2016, 1:37pm UTC](https://discuss.elastic.co/t/exclude-searching-on-specific-fields/51434/1 "2016-05-31T13:37:10Z")

</div>

Hello,

Is there any way to NOT search on a specific field while performing a full text based search using the Query DSL / Query String Query syntax, following is the index i am creating.

{  
"e3c95a54-2971-47ec-b177-1fdbe9e04639": {  
"mappings": {  
"81f71ece-9acc-4750-82f6-53fe47bd4489": {  
"properties": {  
"docfilename": {  
"type": "string"  
},  
"usercomments": {  
"type": "string"  
},  
"filepath": {  
"type": "string",  
"fields": {  
"raw": {  
"type": "string",  
"index": "not\_analyzed"  
}  
}  
},  
"singlewordfield": {  
"type": "string",  
"fields": {  
"raw": {  
"type": "string",  
"index": "not\_analyzed"  
}  
}  
}  
}  
}  
}  
}  
}

{  
"docfilename": "Profile.doc",  
"usercomments": "this is a sample comment entered by the user.",  
"filepath": "\path\Profile.doc",  
"singlewordfield": "apple"  
}

using the following query string syntax i want to search on all the fields except the usercomments field, is this possible using the query string syntax, or is there any other approach to acheive this.

{"query" :{"query\_string":{"query":"profile"}}}

Thanks,  
Vikas.

---

<div class="post-metadata">

**Author:** ![byronvoorbach](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/byronvoorbach/32/8283_2.png) [@byronvoorbach](https://discuss.elastic.co/u/byronvoorbach)\
**Post date:** [May 31, 2016, 1:55pm UTC](https://discuss.elastic.co/t/exclude-searching-on-specific-fields/51434/2 "2016-05-31T13:55:35Z")

</div>

Hi Vikas,

By default, the query-string-query searches within the ["\_all field"](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-all-field.html) field. This is a field which contains all values of your document concatenated into one big string.  
If you want to remove one of your fields from the \_all field, you can do so via your mapping with the 'include\_in\_all' setting, as described [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/include-in-all.html).

Your mapping would look like the following:

```
{
  "mappings": {
    "81f71ece-9acc-4750-82f6-53fe47bd4489": {
      "properties": {
        "docfilename": {
          "type": "string"
        },
        "usercomments": {
          "type": "string",
          "include_in_all" : false
        },
        "filepath": {
          "type": "string",
          "fields": {
            "raw": {
              "type": "string",
              "index": "not_analyzed"
            }
          }
        },
        "singlewordfield": {
          "type": "string",
          "fields": {
            "raw": {
              "type": "string",
              "index": "not_analyzed"
            }
          }
        }
      }
    }
  }
}

```

Since your 'usercomments' data won't exist in the '\_all field', you will not match against data from that field anymore.  
One other solution is to replace your query-string-query with either a multi-match-query or a combination of bool and match queries.

Hope this helps 🙂

---

<div class="post-metadata">

**Author:** ![Vikas\_Mestry](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vikas_mestry/32/42301_2.png) [@Vikas\_Mestry](https://discuss.elastic.co/u/Vikas_Mestry)\
**Post date:** [May 31, 2016, 3:02pm UTC](https://discuss.elastic.co/t/exclude-searching-on-specific-fields/51434/3 "2016-05-31T15:02:11Z")

</div>

Thanks for the superquick response,

I have an additional query on this, how do we go about setting the "include\_in\_all" mapping for a field which would be added in the index at a later stage, since the field name would be decided at that point in time & hence cannot include the future (new) field in the mapping request upfront.

Agree to your alternate solutions of using the multi-match / math queries but I have a constraint of using query-string-query due to the ease of sending search requests, as the requests are floated from a UI based app (the users type in the query-string-query syntax on a UI provided).

Thanks again,  
Vikas.

---

<div class="post-metadata">

**Author:** ![byronvoorbach](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/byronvoorbach/32/8283_2.png) [@byronvoorbach](https://discuss.elastic.co/u/byronvoorbach)\
**Post date:** [June 1, 2016, 1:53pm UTC](https://discuss.elastic.co/t/exclude-searching-on-specific-fields/51434/4 "2016-06-01T13:53:23Z")

</div>

Hi Vikas,

If you know up front which fields you want to query on, you could add the '[fields](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#_multi_field)' parameter to your query string query. The default field is the '\_all' field, but you can also specify a list of fields to search through:

```
GET /_search
{
  "query": {
    "query_string": {
      "query": "this AND that OR thus",
      "fields": [
          "docfilename",
          "filepath",
          "singlewordfield"
        ]
    }
  }
}

```

If that doesn't meet your requirement, there might be a way of identifying prefixes in the fields to be made. For example, if all new fields start with 'foo' (like foo132, foo2bar etc..), you could create a '[default index template](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html)' where you can use wildcards to those fields to be not included:

```
 "dynamic_templates": [
                {
                    "string_template" : { 
                        "match" : "foo*",
                        "mapping": { "type": "string", "index": "not_analyzed", "included_in_all": false },
                        "match_mapping_type" : "string"
                     } 
                 }
             ]
  }

```

Hope this makes sense 🙂

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 10:47pm UTC](https://discuss.elastic.co/t/exclude-searching-on-specific-fields/51434/5 "2017-07-05T22:47:15Z")

</div>


