# Is there a way to do exact and full-text searching without creating two different fields?

**URL:** <https://discuss.elastic.co/t/is-there-a-way-to-do-exact-and-full-text-searching-without-creating-two-different-fields/21252>\
**Category:** Elasticsearch\
**Created:** [December 14, 2014, 11:45pm UTC](https://discuss.elastic.co/t/is-there-a-way-to-do-exact-and-full-text-searching-without-creating-two-different-fields/21252 "2014-12-14T23:45:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![am1](https://avatars.discourse-cdn.com/v4/letter/a/ecd19e/32.png) [@am1](https://discuss.elastic.co/u/am1)\
**Post date:** [December 14, 2014, 11:45pm UTC](https://discuss.elastic.co/t/is-there-a-way-to-do-exact-and-full-text-searching-without-creating-two-different-fields/21252/1 "2014-12-14T23:45:01Z")

</div>

Is there a way to do exact and full text searches without having to create  
two different fields?

The documentation  
([http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/\_finding\_exact\_values.html](http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/_finding_exact_values.html))  
states fields must have the mapping "not\_analyzed" in order to avoid  
tokenization. This allows exact searches to be done.

In my case, I would like both full text search and exact searches. For  
example:

When searching for book titles, a user can input either:

I like ElasticSearch

-OR-

exact="I like ElasticSearch"

The first case will return results from a full text search.

The second case will return results only if the book title is exactly "I  
like ElasticSearch". Case sensitivity does not matter.

To do this, I think I will have to create two fields called "book\_title"  
and "book\_title\_exact" where "book\_title\_exact" will have a field mapping  
"not\_analyzed" so that I can do exact matches.

Is this the proper way of handling my use case? Or is there a simpler way  
in ES without having to store a title twice?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/ac298481-b0f0-4052-a115-388e9db92f50%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/ac298481-b0f0-4052-a115-388e9db92f50%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [December 14, 2014, 11:48pm UTC](https://discuss.elastic.co/t/is-there-a-way-to-do-exact-and-full-text-searching-without-creating-two-different-fields/21252/2 "2014-12-14T23:48:58Z")

</div>

Look at multifields. They let you send the field once and analyze it  
multiple times. You also might want to use keyword ananlyzer and lowercase  
filter rather than not\_analyzed. Folks are used to case insensitivity.

Nik  
Is there a way to do exact and full text searches without having to create  
two different fields?

The documentation (  
[http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/\_finding\_exact\_values.html](http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/_finding_exact_values.html))  
states fields must have the mapping "not\_analyzed" in order to avoid  
tokenization. This allows exact searches to be done.

In my case, I would like both full text search and exact searches. For  
example:

When searching for book titles, a user can input either:

I like ElasticSearch

-OR-

exact="I like ElasticSearch"

The first case will return results from a full text search.

The second case will return results only if the book title is exactly "I  
like ElasticSearch". Case sensitivity does not matter.

To do this, I think I will have to create two fields called "book\_title"  
and "book\_title\_exact" where "book\_title\_exact" will have a field mapping  
"not\_analyzed" so that I can do exact matches.

Is this the proper way of handling my use case? Or is there a simpler way  
in ES without having to store a title twice?

--  
You received this message because you are subscribed to the Google Groups  
"elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an  
email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit  
[https://groups.google.com/d/msgid/elasticsearch/ac298481-b0f0-4052-a115-388e9db92f50%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/ac298481-b0f0-4052-a115-388e9db92f50%40googlegroups.com)  
[https://groups.google.com/d/msgid/elasticsearch/ac298481-b0f0-4052-a115-388e9db92f50%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/ac298481-b0f0-4052-a115-388e9db92f50%40googlegroups.com?utm_medium=email&utm_source=footer)  
.  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAPmjWd3tzungzKTtCrxeLJrLpFJEdiGokqP7%2BuQS5ZB388-mTQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAPmjWd3tzungzKTtCrxeLJrLpFJEdiGokqP7%2BuQS5ZB388-mTQ%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![am1](https://avatars.discourse-cdn.com/v4/letter/a/ecd19e/32.png) [@am1](https://discuss.elastic.co/u/am1)\
**Post date:** [December 15, 2014, 2:00am UTC](https://discuss.elastic.co/t/is-there-a-way-to-do-exact-and-full-text-searching-without-creating-two-different-fields/21252/3 "2014-12-15T02:00:52Z")

</div>

Ah, thanks. I've set this up (using ES python bindings):

es.indices.put\_mapping(index="myindex",  
doc\_type="books",  
body={  
"books": {  
"properties": {  
"title": {  
"type": "string",  
"fields": {  
"name": { "type"  
: "string" },  
"raw": { "type":  
"string", "index": "not\_analyzed" }  
} } } } } )

Then I try to search for exact match:

es.search(index='myindex', doc\_type="books", body={"query": { "filtered": {  
"filter": { "term": { "title": { "raw" : "I like Elasticsearch" } } } } } }  
)

But I get an ES error stating

nested: QueryParsingException[[myindex] [term] filter does not support  
[raw]]; }]')

It seems like I'm not searching for the raw correctly. How would I specify  
to search for the raw title (exact matching)?

On Sunday, December 14, 2014 6:49:09 PM UTC-5, Nikolas Everett wrote:

> Look at multifields. They let you send the field once and analyze it  
> multiple times. You also might want to use keyword ananlyzer and lowercase  
> filter rather than not\_analyzed. Folks are used to case insensitivity.
> 
> Nik  
> Is there a way to do exact and full text searches without having to create  
> two different fields?
> 
> The documentation (  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/_finding_exact_values.html))  
> states fields must have the mapping "not\_analyzed" in order to avoid  
> tokenization. This allows exact searches to be done.
> 
> In my case, I would like both full text search and exact searches. For  
> example:
> 
> When searching for book titles, a user can input either:
> 
> I like Elasticsearch
> 
> -OR-
> 
> exact="I like Elasticsearch"
> 
> The first case will return results from a full text search.
> 
> The second case will return results only if the book title is exactly "I  
> like Elasticsearch". Case sensitivity does not matter.
> 
> To do this, I think I will have to create two fields called "book\_title"  
> and "book\_title\_exact" where "book\_title\_exact" will have a field mapping  
> "not\_analyzed" so that I can do exact matches.
> 
> Is this the proper way of handling my use case? Or is there a simpler way  
> in ES without having to store a title twice?
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/ac298481-b0f0-4052-a115-388e9db92f50%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/ac298481-b0f0-4052-a115-388e9db92f50%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/ac298481-b0f0-4052-a115-388e9db92f50%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/ac298481-b0f0-4052-a115-388e9db92f50%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/413133ef-c82e-4bff-9a9f-af9bd9f56815%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/413133ef-c82e-4bff-9a9f-af9bd9f56815%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![am1](https://avatars.discourse-cdn.com/v4/letter/a/ecd19e/32.png) [@am1](https://discuss.elastic.co/u/am1)\
**Post date:** [December 15, 2014, 2:05am UTC](https://discuss.elastic.co/t/is-there-a-way-to-do-exact-and-full-text-searching-without-creating-two-different-fields/21252/4 "2014-12-15T02:05:28Z")

</div>

I think I just figured it out:

{"title.raw" : "I like Elasticsearch"}

instead of

"title: { "raw": "I like Elasticsearch" }

On Sunday, December 14, 2014 9:00:52 PM UTC-5, am wrote:

> Ah, thanks. I've set this up (using ES python bindings):
> 
> es.indices.put\_mapping(index="myindex",  
> doc\_type="books",  
> body={  
> "books": {  
> "properties": {  
> "title": {  
> "type": "string",  
> "fields": {  
> "name": {  
> "type": "string" },  
> "raw": {  
> "type": "string", "index": "not\_analyzed" }  
> } } } } } )
> 
> Then I try to search for exact match:
> 
> es.search(index='myindex', doc\_type="books", body={"query": { "filtered":  
> { "filter": { "term": { "title": { "raw" : "I like Elasticsearch" } } } }  
> } } )
> 
> But I get an ES error stating
> 
> nested: QueryParsingException[[myindex] [term] filter does not support  
> [raw]]; }]')
> 
> It seems like I'm not searching for the raw correctly. How would I specify  
> to search for the raw title (exact matching)?
> 
> On Sunday, December 14, 2014 6:49:09 PM UTC-5, Nikolas Everett wrote:
> 
> > Look at multifields. They let you send the field once and analyze it  
> > multiple times. You also might want to use keyword ananlyzer and lowercase  
> > filter rather than not\_analyzed. Folks are used to case insensitivity.
> > 
> > Nik  
> > Is there a way to do exact and full text searches without having to  
> > create two different fields?
> > 
> > The documentation (  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/_finding_exact_values.html))  
> > states fields must have the mapping "not\_analyzed" in order to avoid  
> > tokenization. This allows exact searches to be done.
> > 
> > In my case, I would like both full text search and exact searches. For  
> > example:
> > 
> > When searching for book titles, a user can input either:
> > 
> > I like Elasticsearch
> > 
> > -OR-
> > 
> > exact="I like Elasticsearch"
> > 
> > The first case will return results from a full text search.
> > 
> > The second case will return results only if the book title is exactly "I  
> > like Elasticsearch". Case sensitivity does not matter.
> > 
> > To do this, I think I will have to create two fields called "book\_title"  
> > and "book\_title\_exact" where "book\_title\_exact" will have a field mapping  
> > "not\_analyzed" so that I can do exact matches.
> > 
> > Is this the proper way of handling my use case? Or is there a simpler way  
> > in ES without having to store a title twice?
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/ac298481-b0f0-4052-a115-388e9db92f50%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/ac298481-b0f0-4052-a115-388e9db92f50%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/ac298481-b0f0-4052-a115-388e9db92f50%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/ac298481-b0f0-4052-a115-388e9db92f50%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/f5ad817d-f36c-495f-99cc-8dbe96e78e3a%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/f5ad817d-f36c-495f-99cc-8dbe96e78e3a%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 12:43am UTC](https://discuss.elastic.co/t/is-there-a-way-to-do-exact-and-full-text-searching-without-creating-two-different-fields/21252/5 "2017-07-06T00:43:46Z")

</div>


