Note that there are some explanatory texts on larger screens.

plurals
  1. POLucene.NET Phrase search returning no results
    primarykey
    data
    text
    <p>EDIT</p> <p>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".</p> <p>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.</p> <pre><code>List&lt;string&gt; searchList = Regex.Matches(searchTerms, @"(?&lt;match&gt;\w+)|\""(?&lt;match&gt;[\w\s]*)""") .Cast&lt;Match&gt;() .Select(m =&gt; 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); } </code></pre> <hr> <p>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.</p> <p>The following code is being used to search:</p> <pre><code>List&lt;string&gt; searchList = Regex.Matches(searchTerms, @"(?&lt;match&gt;\w+)|\""(?&lt;match&gt;[\w\s]*)""") .Cast&lt;Match&gt;() .Select(m =&gt; 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); } </code></pre> <p>I have checked the query that is being created and it looks OK to me: </p> <pre><code>+body:"The following table" </code></pre> <p>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"</p> <p><img src="https://i.stack.imgur.com/aa3T3.gif" alt="enter image description here"></p> <p>I'm at a loss really for what could be the problem.</p> <p>I have used the following code to create the index:</p> <pre><code>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(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload