# Search a field using analyzer specific to type

**URL:** <https://discuss.elastic.co/t/search-a-field-using-analyzer-specific-to-type/5673>\
**Category:** Elasticsearch\
**Created:** [October 25, 2011, 2:43am UTC](https://discuss.elastic.co/t/search-a-field-using-analyzer-specific-to-type/5673 "2011-10-25T02:43:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Wilson](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@James\_Wilson](https://discuss.elastic.co/u/James_Wilson)\
**Post date:** [October 25, 2011, 2:43am UTC](https://discuss.elastic.co/t/search-a-field-using-analyzer-specific-to-type/5673/1 "2011-10-25T02:43:44Z")

</div>

Suppose I have two types in the same index, foo and bar. They both  
define a field called title. In foo, it makes sense to use stemming  
and stop words, so I use the "analyzer": "snowball" in the mapping for  
title. In bar, it is better to just use the whitespace analyzer.

I'd like to be able to search for this field and have the query  
analyzed using snowball for foo, and whitespace for bar. It appears  
that using a query like

{ "query" : { "text" : { "title" : \<...\> } } }

won't do this when I search the whole index. I can give an analyzer  
explicitly but it will be bad for searching on one of the types.

Is there a way to construct a query and/or schema to do what I want?

I think a similar issue came up in [https://github.com/elasticsearch/elasticsearch/issues/1391](https://github.com/elasticsearch/elasticsearch/issues/1391),  
but I don't know if that change also addresses this situation.

---

<div class="post-metadata">

**Author:** ![James\_Wilson](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@James\_Wilson](https://discuss.elastic.co/u/James_Wilson)\
**Post date:** [October 25, 2011, 6:24pm UTC](https://discuss.elastic.co/t/search-a-field-using-analyzer-specific-to-type/5673/2 "2011-10-25T18:24:15Z")

</div>

I found using a dis\_max of the same query under two analyzers achieved  
good results. Is this the preferred way to this though?

On Oct 24, 7:43 pm, James Wilson [jwilson...@gmail.com](mailto:jwilson...@gmail.com) wrote:

> Suppose I have two types in the same index, foo and bar. They both  
> define a field called title. In foo, it makes sense to use stemming  
> and stop words, so I use the "analyzer": "snowball" in the mapping for  
> title. In bar, it is better to just use the whitespace analyzer.
> 
> I'd like to be able to search for this field and have the query  
> analyzed using snowball for foo, and whitespace for bar. It appears  
> that using a query like
> 
> { "query" : { "text" : { "title" : \<...\> } } }
> 
> won't do this when I search the whole index. I can give an analyzer  
> explicitly but it will be bad for searching on one of the types.
> 
> Is there a way to construct a query and/or schema to do what I want?
> 
> I think a similar issue came up inhttps://github.com/elasticsearch/elasticsearch/issues/1391,  
> but I don't know if that change also addresses this situation.

---

<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:** [October 26, 2011, 3:04am UTC](https://discuss.elastic.co/t/search-a-field-using-analyzer-specific-to-type/5673/3 "2011-10-26T03:04:56Z")

</div>

You can specify the field prefixed by the type, something like foo.title and  
bar.title, this will make sure to use the respective analyzer (and  
automatically filter the query to execute only the relevant type).

So, yea, you can use either bool query with two should clauses, one on  
foo.title, and one on bar.title, or a dis max one.

On Tue, Oct 25, 2011 at 8:24 PM, James Wilson [jwilson556@gmail.com](mailto:jwilson556@gmail.com) wrote:

> I found using a dis\_max of the same query under two analyzers achieved  
> good results. Is this the preferred way to this though?
> 
> On Oct 24, 7:43 pm, James Wilson [jwilson...@gmail.com](mailto:jwilson...@gmail.com) wrote:
> 
> > Suppose I have two types in the same index, foo and bar. They both  
> > define a field called title. In foo, it makes sense to use stemming  
> > and stop words, so I use the "analyzer": "snowball" in the mapping for  
> > title. In bar, it is better to just use the whitespace analyzer.
> > 
> > I'd like to be able to search for this field and have the query  
> > analyzed using snowball for foo, and whitespace for bar. It appears  
> > that using a query like
> > 
> > { "query" : { "text" : { "title" : \<...\> } } }
> > 
> > won't do this when I search the whole index. I can give an analyzer  
> > explicitly but it will be bad for searching on one of the types.
> > 
> > Is there a way to construct a query and/or schema to do what I want?
> > 
> > I think a similar issue came up inhttps://  
> > [When searching against an index/type, use the type information to derive different search aspects · Issue #1391 · elastic/elasticsearch · GitHub](http://github.com/elasticsearch/elasticsearch/issues/1391),  
> > but I don't know if that change also addresses this situation.

---

<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, 3:50am UTC](https://discuss.elastic.co/t/search-a-field-using-analyzer-specific-to-type/5673/4 "2017-07-06T03:50:58Z")

</div>


