# "term" type query

**URL:** <https://discuss.elastic.co/t/term-type-query/4792>\
**Category:** Elasticsearch\
**Created:** [July 6, 2011, 2:18pm UTC](https://discuss.elastic.co/t/term-type-query/4792 "2011-07-06T14:18:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ChrisGedrim](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chrisgedrim/32/3181_2.png) [@ChrisGedrim](https://discuss.elastic.co/u/ChrisGedrim)\
**Post date:** [July 6, 2011, 2:18pm UTC](https://discuss.elastic.co/t/term-type-query/4792/1 "2011-07-06T14:18:52Z")

</div>

Hi all,

First and foremost: I searched the archives and the only post I found which is similar to my issue was this [http://elasticsearch-users.115913.n3.nabble.com/Search-query-issue-using-quot-term-quot-and-spaces-td1020831.html](http://elasticsearch-users.115913.n3.nabble.com/Search-query-issue-using-quot-term-quot-and-spaces-td1020831.html)

I'm a new user of ElasticSearch and am currently trying to wrap my head around being able to search for moth exact and partial matches.

Partial matches I can do 🙂

The problem I have is exact matches; take for example the following (very simple) records:

{ "name" : "Chris Gedrim" }  
{ "name" : "Peter Gedrim" }

Both of which live in the 'test' index with a type of 'people'.

I run the following query (for partial matching):

{  
"query" : {  
"query\_string" : {  
"query" : "name:Gedrim"  
}  
}  
}

Which returns:

"hits" : {  
"total" : 2,  
"max\_score" : 0.19178301,  
"hits" : [ {  
"\_index" : "test",  
"\_type" : "chrisgedrim",  
"\_id" : "t-eRmndRQ3uwKI4c01v2Aw",  
"\_score" : 0.19178301, "\_source" : {name:"Peter Gedrim"}  
}, {  
"\_index" : "test",  
"\_type" : "chrisgedrim",  
"\_id" : "vbWG0Py0SdCSGnW2kfeaZg",  
"\_score" : 0.19178301, "\_source" : {name:"Chris Gedrim"}  
} ]  
}

All of which is fine.

I then run the following (for an exact match):

{  
"query" : {  
"term" : {  
"name" : "Chris Gedrim"  
}  
}  
}

Which returns:

"hits" : {  
"total" : 0,  
"max\_score" : null,  
"hits" : []  
}

From the previously linked-to post I think I should be creating a [multi-field mapping](http://www.elasticsearch.org/guide/reference/mapping/multi-field-type.html) thus:

{  
"people" : {  
"properties" : {  
"name" : {  
"type" : "multi\_field",  
"fields" : {  
"name" : {"type" : "string", "index" : "analyzed"},  
"untouched" : {"type" : "string", "index" : "not\_analyzed"}  
}  
}  
}  
}  
}

Is this correct? If so is there any way of setting _all_ strings to use this mapping?

Also: would the term query above work correctly with this new mapping, or does it need modifying to use the 'untouched' field?

Cheers,

Chris

---

<div class="post-metadata">

**Author:** ![imarcticblue](https://avatars.discourse-cdn.com/v4/letter/i/c67d28/32.png) [@imarcticblue](https://discuss.elastic.co/u/imarcticblue)\
**Post date:** [July 6, 2011, 5:20pm UTC](https://discuss.elastic.co/t/term-type-query/4792/2 "2011-07-06T17:20:40Z")

</div>

If you're using the standard parser for the name field, then it will lower-case and split on white space. If you want to search on proper case, then not\_analyzed would work or using the multi-field mapping and searching on the untouched field would work.

When index, you need to pick and analyzer based on how you intend to search that field. You can read over the mapping section if you need more information. [http://www.elasticsearch.org/guide/reference/mapping/](http://www.elasticsearch.org/guide/reference/mapping/)

- Craig

---

<div class="post-metadata">

**Author:** ![ChrisGedrim](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chrisgedrim/32/3181_2.png) [@ChrisGedrim](https://discuss.elastic.co/u/ChrisGedrim)\
**Post date:** [July 7, 2011, 7:59am UTC](https://discuss.elastic.co/t/term-type-query/4792/3 "2011-07-07T07:59:19Z")

</div>

Craig,

Cheers for the reply, but I've already read the documentation and it doesn't seem to answer my question, hence posting it here.

Just to reiterate my main question:

Can I setup a global mapping for string type fields so that they are both analyzed and not\_analyzed ?

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [July 7, 2011, 9:12am UTC](https://discuss.elastic.co/t/term-type-query/4792/4 "2011-07-07T09:12:05Z")

</div>

Hi Chris

> Can I setup a global mapping for string type fields so that they are both  
> analyzed and not\_analyzed ?

If you specify the mapping for any field, it is what you specify.

However, if you want to configure the mapping that is dynamically (ie  
automatically) added for a new field, then you can use dynamic  
templates:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

That said, I think that you should reconsider making all string fields  
analyzed and not\_analyzed by default. It seems a shotgun approach which  
will generate way too many terms

clint

---

<div class="post-metadata">

**Author:** ![ChrisGedrim](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chrisgedrim/32/3181_2.png) [@ChrisGedrim](https://discuss.elastic.co/u/ChrisGedrim)\
**Post date:** [July 7, 2011, 9:43am UTC](https://discuss.elastic.co/t/term-type-query/4792/5 "2011-07-07T09:43:57Z")

</div>

Clinton,

Cheers for the reply, I totally missed the root-object type mapping.

> That said, I think that you should reconsider making all string fields  
> analyzed and not\_analyzed by default. It seems a shotgun approach which  
> will generate way too many terms

I appreciate that having both mappings is a shotgun approach, but there seems no other way of allowing both 'loose' and exact searching… please correct me if I'm wrong though.

Also: is there any way of setting the mapping on an _index_ rather than a _type_? All of the date fields in our application are either stored _yyyy-MM-dd_ or _yyyy-MM-dd HH:mm:ss_ (ie no 'T' before the time portion, which elastic then treats as a string rather than a date)

Cheers,

Chris

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [July 7, 2011, 10:04am UTC](https://discuss.elastic.co/t/term-type-query/4792/6 "2011-07-07T10:04:51Z")

</div>

Hi Chris

> Cheers for the reply, I totally missed the root-object type mapping.

Yeah, it took me a while to find it too 🙂

> I appreciate that having both mappings is a shotgun approach, but there  
> seems no other way of allowing both 'loose' and exact searchingâ¦ please  
> correct me if I'm wrong though.

Sure, but you have string fields for different purposes. Some of them  
will contain text which should be analyzed, and some will contain fixed  
enum-like values.

When do you want to match this exactly:

```
    "Sure, but you have string fields for different purposes. Some
    of  
     them will contain text which should be analyzed, and some
    will  
     contain fixed enum-like values."

```

So what I'm saying is: don't make them ALL analyzed and not\_analzyed.  
Use the right mapping for the right field.

> Also: is there any way of setting the mapping on an /index/ rather than a  
> /type/? All of the date fields in our application are either stored  
> /yyyy-MM-dd/ or /yyyy-MM-dd HH:mm:ss/ (ie no 'T' before the time portion,  
> which elastic then treats as a string rather than a date)

Have a look at:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

clint

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [July 7, 2011, 2:41pm UTC](https://discuss.elastic.co/t/term-type-query/4792/7 "2011-07-07T14:41:37Z")

</div>

One option to do that (make, by default, all string types not analyzed) is to use the dynamic templates option (with match\_type set to string) to create a template for string type fields. See here: [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/root-object-type.html).

On Thursday, July 7, 2011 at 1:04 PM, Clinton Gormley wrote:

> Hi Chris
> 
> > Cheers for the reply, I totally missed the root-object type mapping.
> 
> Yeah, it took me a while to find it too 🙂
> 
> > I appreciate that having both mappings is a shotgun approach, but there  
> > seems no other way of allowing both 'loose' and exact searching… please  
> > correct me if I'm wrong though.
> 
> Sure, but you have string fields for different purposes. Some of them  
> will contain text which should be analyzed, and some will contain fixed  
> enum-like values.
> 
> When do you want to match this exactly:
> 
> "Sure, but you have string fields for different purposes. Some  
> of  
> them will contain text which should be analyzed, and some  
> will  
> contain fixed enum-like values."
> 
> So what I'm saying is: don't make them ALL analyzed and not\_analzyed.  
> Use the right mapping for the right field.
> 
> > Also: is there any way of setting the mapping on an /index/ rather than a  
> > /type/? All of the date fields in our application are either stored  
> > /yyyy-MM-dd/ or /yyyy-MM-dd HH:mm:ss/ (ie no 'T' before the time portion,  
> > which elastic then treats as a string rather than a date)
> 
> Have a look at:
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/conf-mappings.html)
> 
> clint

---

<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 6, 2017, 4:01am UTC](https://discuss.elastic.co/t/term-type-query/4792/8 "2017-07-06T04:01:24Z")

</div>


