Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Lucene: Java-User

Searching both phrase and it's words

 

 

Lucene java-user RSS feed   Index | Next | Previous | View Threaded


sxamt at yahoo

Jun 30, 2012, 1:55 PM

Post #1 of 2 (154 views)
Permalink
Searching both phrase and it's words

Hi,
Suppose we have a query "balcony table". I want results to be returned by
exact match (first priority) and by single words matching as well (for
"balcony" or for "table"). So currently my solution is:

Analyzer analyzer = new SnowballAnalyzer("English",
StopAnalyzer.ENGLISH_STOP_WORDS_SET);
QueryParser contentParser = new QueryParser(Version.LUCENE_29,
"content", analyzer);

Query phraseContentQuery =
contentParser.Parse("\""+QueryParser.Escape(luceneQuery)+"\"");

if (phraseContentQuery is PhraseQuery)
{
((PhraseQuery)phraseContentQuery).SetSlop(100);
}

Query simpleParsedQuery =
contentParser.Parse(QueryParser.Escape(luceneQuery));

BooleanQuery boolQuery = new BooleanQuery();

boolQuery.Add(simpleParsedQuery, BooleanClause.Occur.SHOULD);
boolQuery.Add(phraseContentQuery, BooleanClause.Occur.SHOULD);

TopDocs docs = searcher.Search(boolQuery, 1500);
ScoreDoc[] hits = docs.scoreDocs;

Is it the right way?
Is there another way, without running two queries (simpleParsedQuery and
phraseContentQuery)?

Thanks!

Maxim

--
View this message in context: http://lucene.472066.n3.nabble.com/Searching-both-phrase-and-it-s-words-tp3992276.html
Sent from the Lucene - Java Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe [at] lucene
For additional commands, e-mail: java-user-help [at] lucene


jack at basetechnology

Jun 30, 2012, 2:46 PM

Post #2 of 2 (149 views)
Permalink
Re: Searching both phrase and it's words [In reply to]

You didn't show us your "luceneQuery", but the gist of the solution is to
use MUST clauses for each of the individual terms and then a SHOULD of the
phrase. You can add an additional boost to the phrase, but lucene should
naturally boost documents containing the phrase.

-- Jack Krupansky

-----Original Message-----
From: sxam
Sent: Saturday, June 30, 2012 3:55 PM
To: java-user [at] lucene
Subject: Searching both phrase and it's words

Hi,
Suppose we have a query "balcony table". I want results to be returned by
exact match (first priority) and by single words matching as well (for
"balcony" or for "table"). So currently my solution is:

Analyzer analyzer = new SnowballAnalyzer("English",
StopAnalyzer.ENGLISH_STOP_WORDS_SET);
QueryParser contentParser = new QueryParser(Version.LUCENE_29,
"content", analyzer);

Query phraseContentQuery =
contentParser.Parse("\""+QueryParser.Escape(luceneQuery)+"\"");

if (phraseContentQuery is PhraseQuery)
{
((PhraseQuery)phraseContentQuery).SetSlop(100);
}

Query simpleParsedQuery =
contentParser.Parse(QueryParser.Escape(luceneQuery));

BooleanQuery boolQuery = new BooleanQuery();

boolQuery.Add(simpleParsedQuery, BooleanClause.Occur.SHOULD);
boolQuery.Add(phraseContentQuery, BooleanClause.Occur.SHOULD);

TopDocs docs = searcher.Search(boolQuery, 1500);
ScoreDoc[] hits = docs.scoreDocs;

Is it the right way?
Is there another way, without running two queries (simpleParsedQuery and
phraseContentQuery)?

Thanks!

Maxim

--
View this message in context:
http://lucene.472066.n3.nabble.com/Searching-both-phrase-and-it-s-words-tp3992276.html
Sent from the Lucene - Java Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe [at] lucene
For additional commands, e-mail: java-user-help [at] lucene


---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe [at] lucene
For additional commands, e-mail: java-user-help [at] lucene

Lucene java-user RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.