1
votes

EDIT

Ok I worked it out. When I was adding the terms one at a time to the PhraseQuery object it was retaining the common words. In the case it was "The".

What I have done instead is used the QueryParser object to parse the query (including the quotation marks). This drops the common words and the phrase query now works like a charm.

List<string> searchList = Regex.Matches(searchTerms, @"(?<match>\w+)|\""(?<match>[\w\s]*)""")
                   .Cast<Match>()
                   .Select(m => m.Groups["match"].Value)
                   .ToList();

QueryParser parser = new QueryParser(LuceneFields.BODY, Analyzer);
BooleanQuery booleanQuery = new BooleanQuery();

// go through each term
foreach (string term in searchList)
{
  Query query = null;
  if (term.Contains(" ")) // multi word phrase
    query = parser.Parse("\"" + term + "\"");
  else
    query = parser.Parse(term);
  if (query.ToString() != "")
    booleanQuery.Add(query, BooleanClause.Occur.MUST);
}

I am creating a simple search using Lucene.NET and I am having a bit of trouble getting phrase searching to work properly as I am combining it with a boolean query.

The following code is being used to search:

List<string> searchList = Regex.Matches(searchTerms, @"(?<match>\w+)|\""(?<match>[\w\s]*)""")
                   .Cast<Match>()
                   .Select(m => m.Groups["match"].Value)
                   .ToList();

QueryParser parser = new QueryParser(LuceneFields.BODY, Analyzer);
BooleanQuery booleanQuery = new BooleanQuery();

// go through each term
foreach (string term in searchList)
{
  Query query = null;

  if (term.Contains(" ")) // multi word phrase
  {
    query = new PhraseQuery();
    foreach (string str in term.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
    {
      ((PhraseQuery)query).Add(new Term(LuceneFields.BODY, str));
    }
  }
  else
    query = parser.Parse(term);

  string strQuery = query.ToString();

  if (query.ToString() != "")
    booleanQuery.Add(query, BooleanClause.Occur.MUST);
}

I have checked the query that is being created and it looks OK to me:

+body:"The following table"

And I have also confirmed this text is actually in the Lucene index as you can see from a search result just searching for "table"

enter image description here

I'm at a loss really for what could be the problem.

I have used the following code to create the index:

Directory = FSDirectory.Open(new System.IO.DirectoryInfo(IndexDirectory));
Analyzer = new StandardAnalyzer(Version); 

using (IndexWriter indexWriter = new IndexWriter(Directory, Analyzer, new IndexWriter.MaxFieldLength(Int32.MaxValue)))
{
  Response.Write("Adding document...");
  Document document = new Document();

  // Store the IDDataContent
  document.Add(new Field(LuceneFields.ID, id.ToString(), Field.Store.YES, Field.Index.ANALYZED));

  // store the url to the file itself
  document.Add(new Field(LuceneFields.HREF, FileURL, Field.Store.YES, Field.Index.ANALYZED));

  //document.Add(new Field(LuceneFields.TITLE, Article.Title, Field.Store.YES, Field.Index.ANALYZED));

  // store the text of the PDF
  document.Add(new Field(LuceneFields.BODY, PdfContents, Field.Store.YES, Field.Index.ANALYZED));
  indexWriter.AddDocument(document);
  indexWriter.Optimize();
  indexWriter.Commit();
}
2
did you try running the phrase query using Luke?Mikos
The query using Luke returns the expected resultRollcredit

2 Answers

1
votes
  • Check if you are using same analyzer while indexing and search.
  • Check whether the terms you are searching for actually exist in index. (using luke)
1
votes

Try to use whitespaceanalyzer as standardanalyzer won't work in such a case