# Custom Tokenization

**URL:** <https://discuss.elastic.co/t/custom-tokenization/6369>\
**Category:** Elasticsearch\
**Created:** [January 12, 2012, 9:51pm UTC](https://discuss.elastic.co/t/custom-tokenization/6369 "2012-01-12T21:51:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Bleigh](https://avatars.discourse-cdn.com/v4/letter/m/b3f665/32.png) [@Michael\_Bleigh](https://discuss.elastic.co/u/Michael_Bleigh)\
**Post date:** [January 12, 2012, 9:51pm UTC](https://discuss.elastic.co/t/custom-tokenization/6369/1 "2012-01-12T21:51:25Z")

</div>

I'm a bit new to ElasticSearch so my apologies if this is an obvious thing.  
I'm trying to build an index that will match tag-like structures in a  
consistent fashion. It's important not to do fuzzy matching (it needs to be  
exact) but I also need to support a variety of representations.

What I'm wondering is how I would be able to create an analyzer such that  
these phrases (as an example) would all match identically when queried:

"Heart and Soul", "heart\_and\_soul", "Heart & Soul", "heart-and-soul"

However, I would not want "heart" to match as an example of the specificity  
that I mentioned.

What is the best way to achieve this? It seems like creating a  
pattern\_replace filter could potentially help me, but I can't seem to find  
the documentation for that filter anyway. Help would be much appreciated.

I'm using Ruby and Tire, so bonus points if the explanation is in Ruby but  
not required.

Thanks!

---

<div class="post-metadata">

**Author:** ![Karussell1](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@Karussell1](https://discuss.elastic.co/u/Karussell1)\
**Post date:** [January 13, 2012, 9:57am UTC](https://discuss.elastic.co/t/custom-tokenization/6369/2 "2012-01-13T09:57:43Z")

</div>

If you cannot find docs go to [GitHub - elastic/elasticsearch: Free and Open, Distributed, RESTful Search Engine](https://github.com/elasticsearch/elasticsearch/)  
and type 't' followed by you filter 'patternreplace'

this gives you

[https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/apache/lucene/analysis/pattern/PatternReplaceFilter.java](https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/apache/lucene/analysis/pattern/PatternReplaceFilter.java)

[https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/index/analysis/PatternReplaceTokenFilterFactory.java](https://github.com/elasticsearch/elasticsearch/blob/master/src/main/java/org/elasticsearch/index/analysis/PatternReplaceTokenFilterFactory.java)

so you'll need 'pattern' and 'replacement' and you can use 'flags' and  
'all'.

Peter.

On 12 Jan., 22:51, Michael Bleigh [mble...@gmail.com](mailto:mble...@gmail.com) wrote:

> I'm a bit new to Elasticsearch so my apologies if this is an obvious thing.  
> I'm trying to build an index that will match tag-like structures in a  
> consistent fashion. It's important not to do fuzzy matching (it needs to be  
> exact) but I also need to support a variety of representations.
> 
> What I'm wondering is how I would be able to create an analyzer such that  
> these phrases (as an example) would all match identically when queried:
> 
> "Heart and Soul", "heart\_and\_soul", "Heart & Soul", "heart-and-soul"
> 
> However, I would not want "heart" to match as an example of the specificity  
> that I mentioned.
> 
> What is the best way to achieve this? It seems like creating a  
> pattern\_replace filter could potentially help me, but I can't seem to find  
> the documentation for that filter anyway. Help would be much appreciated.
> 
> I'm using Ruby and Tire, so bonus points if the explanation is in Ruby but  
> not required.
> 
> Thanks!

---

<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:42am UTC](https://discuss.elastic.co/t/custom-tokenization/6369/3 "2017-07-06T03:42:49Z")

</div>


