Note that there are some explanatory texts on larger screens.

plurals
  1. POLuke Lucene BooleanQuery
    primarykey
    data
    text
    <p>In Luke, the following search expression returns 23 results:</p> <pre><code>docurl:www.siteurl.com docfile:Tomatoes* </code></pre> <p>If I pass this same expression into my C# Lucene.NET app with the following implementation:</p> <pre><code> IndexReader reader = IndexReader.Open(indexName); Searcher searcher = new IndexSearcher(reader); try { QueryParser parser = new QueryParser("docurl", new StandardAnalyzer()); BooleanQuery bquery = new BooleanQuery(); Query parsedQuery = parser.Parse(query); bquery.Add(parsedQuery, Lucene.Net.Search.BooleanClause.Occur.MUST); int _max = searcher.MaxDoc(); BooleanQuery.SetMaxClauseCount(Int32.MaxValue); TopDocs hits = searcher.Search(parsedQuery, _max) ... } </code></pre> <p>I get 0 results</p> <p>Luke is using StandardAnalyzer and this is what the Explain Structure window looks like: <img src="https://i.stack.imgur.com/YlOx4.png" alt="Luke Query Structure"></p> <p>Must I manually create <code>BooleanClause</code> objects for each field I search on, specifying <code>Should</code> for each one then add them to the <code>BooleanQuery</code> object with <code>.Add()</code>? I thought the <code>QueryParser</code> would do this for me. What am I missing?</p> <p><strong>Edit:</strong> Simplifying a tad, <code>docfile:Tomatoes*</code> returns 23 docs in Luke, yet 0 in my app. Per Gene's suggestion, I've changed from <code>MUST</code> to <code>SHOULD</code>:</p> <pre><code> QueryParser parser = new QueryParser("docurl", new StandardAnalyzer()); BooleanQuery bquery = new BooleanQuery(); Query parsedQuery = parser.Parse(query); bquery.Add(parsedQuery, Lucene.Net.Search.BooleanClause.Occur.SHOULD); int _max = searcher.MaxDoc(); BooleanQuery.SetMaxClauseCount(Int32.MaxValue); TopDocs hits = searcher.Search(parsedQuery, _max); </code></pre> <p>parsedQuery is simply <code>docfile:tomatoes*</code></p> <p><strong>Edit2:</strong></p> <p>I think I've finally gotten to the root problem:</p> <pre><code> QueryParser parser = new QueryParser("docurl", new StandardAnalyzer()); Query parsedQuery = parser.Parse(query); </code></pre> <p>In the second line, <code>query</code> is <code>"docfile:Tomatoes*"</code>, but <code>parsedQuery</code> is <code>{docfile:tomatoes*}</code>. Notice the difference? Lower case 't' in the parsed query. I never noticed this before. If I change the value in the IDE to 'T', 23 results return. </p> <p>I've verified that <code>StandardAnalyzer</code> is being used when indexing and reading the index. How do I force <code>queryParser</code> to keep the case of the value of <code>query</code>?</p> <p><strong>Edit3:</strong> Wow, how frustrating. According to the <a href="http://lucene.apache.org/java/2_3_2/api/org/apache/lucene/queryParser/QueryParser.html#setLowercaseExpandedTerms%28boolean%29" rel="nofollow noreferrer">documentation</a>, I can accomplish this with:</p> <p>parser.setLowercaseExpandedTerms(false);</p> <blockquote> <p>Whether terms of wildcard, prefix, fuzzy and range queries are to be automatically lower-cased or not. Default is true.</p> </blockquote> <p>I won't argue whether that's a sensible default or not. I suppose SimpleAnalyzer should have been used to lowercase everything in and out of the index. The frustrating part is, at least with the version I'm using, Luke defaults the other way! At least I learned a bit more about Lucene. </p>
    singulars
    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