# Using StemFilter with PhraseQuery

**URL:** <https://www.ruby-forum.com/t/using-stemfilter-with-phrasequery/137397>\
**Category:** Ferret\
**Created:** [May 12, 2008, 8:35pm UTC](https://www.ruby-forum.com/t/using-stemfilter-with-phrasequery/137397 "2008-05-12T20:35:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![S\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@S\_D](https://www.ruby-forum.com/u/S_D)\
**Post date:** [May 12, 2008, 8:35pm UTC](https://www.ruby-forum.com/t/using-stemfilter-with-phrasequery/137397/1 "2008-05-12T20:35:34Z")

</div>

Hi,

I’m having difficulty getting the StemFilter and PhraseQuery to work  
properly together. When I use a StemFilter with a PhraseQuery, searches  
only  
work if the phrase consists of stems. For example, the search phrase  
“reduces health care” will not work but the phrase “reduce health care”  
will  
work even though the exact text “reduces health care” is contained in  
the  
original document. I’d like to use StemFilter in conjunction with  
PhraseQuery because I need the stemming and I also need to be able to  
use  
the slop feature of PhraseQuery. Below is my use of StemFilter and  
PhraseQuery. Is there anything I’m doing wrong or is the above  
description  
what I should expect? To get the response that I’m expecting I could  
parse  
the phrase and build up a query to be used by QueryParser but I’d like a  
more succinct solution for now.

I use a StemFilter in my analyzer as follows:

```
def token_stream(field, str)
  ...
  ts = LowerCaseFilter.new(ts) if @lower
  ts = StopFilter.new(ts, @stop_words)
  ts = StemFilter.new(ts)
  ...
end

```

My use of PhraseQuery is as follows:

def generate\_query(phrase)  
phrase = phrase.downcase  
phrase\_parts = phrase.split(’ ')  
query = Ferret::Search::PhraseQuery.new(:content, 2)  
phrase\_parts.each do |part|  
# puts “part: “” + part + “””  
query.add\_term(part, 1)  
end  
query  
end

---

<div class="post-metadata">

**Author:** ![S\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@S\_D](https://www.ruby-forum.com/u/S_D)\
**Post date:** [May 13, 2008, 9:11am UTC](https://www.ruby-forum.com/t/using-stemfilter-with-phrasequery/137397/2 "2008-05-13T09:11:44Z")

</div>

Hi!

I think what you get is the expected behaviour. Since you don’t use  
Ferret’s QueryParser but build your queries on your own, you’re also  
responsible for proper tokenization / analysis of your query content.

So running your phrase through your analyzer before constructing the  
phrase query should work as expected.

Cheers,  
Jens

On Mon, May 12, 2008 at 02:33:23PM -0400, S D wrote:

> PhraseQuery. Is there anything I’m doing wrong or is the above description  
> ts = StemFilter.new(ts)  
> # puts “part: "” + part + “"”  
> query.add\_term(part, 1)  
> end  
> query  
> end

> * * *
> 
> Ferret-talk mailing list  
> removed\_email\_address@domain.invalid  
> [http://rubyforge.org/mailman/listinfo/ferret-talk](http://rubyforge.org/mailman/listinfo/ferret-talk)

–  
Jens Krämer  
webit! Gesellschaft für neue Medien mbH  
Schnorrstraße 76 | 01069 Dresden  
Telefon +49 351 46766-0 | Telefax +49 351 46766-66  
removed\_email\_address@domain.invalid | [www.webit.de](http://www.webit.de)

Amtsgericht Dresden | HRB 15422  
GF Sven Haubold
