Note that there are some explanatory texts on larger screens.

plurals
  1. POLucene.net cant search ".net"
    text
    copied!<p>Q1: I am making a search using Lucene. Everything works fine and quickly. When I tried to search for the phrase ".net", it didn't find anything. Maybe you know how can I cope with this.</p> <p>Q2: How can I search and ignore case?</p> <p><strong>Update 1</strong></p> <p>Q1:I am saving jobs using <a href="http://simplelucene.codeplex.com/" rel="nofollow">SimpleLucene</a>. Here is the code:</p> <pre><code>DirectoryIndexWriter _indexWriter = new DirectoryIndexWriter(new DirectoryInfo(indexPath), true); using (var indexService = new IndexService(_indexWriter)) { var result = indexService.IndexEntities(jobsTempArray, new JobIndexDefinition()); Console.WriteLine("{0} products indexed in {1} milliseconds.", result.Count, result.ExecutionTime); } </code></pre> <p>JobIndexDefinition file:</p> <pre><code>public class JobIndexDefinition : IIndexDefinition&lt;LucenceJobModel&gt; { public Document Convert(LucenceJobModel job) { var document = new Document(); document.Add(new Field("jobtitle", job.JobTitle, Field.Store.YES, Field.Index.ANALYZED)); document.Add(new Field("AreaCode", job.AreaCode.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); document.Add(new Field("Company", job.Company, Field.Store.YES, Field.Index.NOT_ANALYZED)); var dateValue = DateTools.DateToString(job.DatePosted.Value, DateTools.Resolution.MILLISECOND); document.Add(new Field("DatePosted", dateValue, Field.Store.YES, Field.Index.NOT_ANALYZED)); document.Add(new Field("Description", job.Description, Field.Store.YES, Field.Index.ANALYZED)); document.Add(new Field("Expierence", job.Expierence, Field.Store.YES, Field.Index.NOT_ANALYZED)); document.Add(new Field("JobType", job.JobType, Field.Store.YES, Field.Index.NOT_ANALYZED)); document.Add(new Field("Link", job.Link, Field.Store.YES, Field.Index.NOT_ANALYZED)); document.Add(new Field("LinkId", job.LinkId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); document.Add(new Field("Location", job.Location, Field.Store.YES, Field.Index.NOT_ANALYZED)); document.Add(new Field("PayRate", job.PayRate, Field.Store.YES, Field.Index.NOT_ANALYZED)); document.Add(new Field("Source", job.Source, Field.Store.YES, Field.Index.NOT_ANALYZED)); document.Add(new Field("TaxTerm", job.TaxTerm, Field.Store.YES, Field.Index.NOT_ANALYZED)); document.Add(new Field("Term", job.Term, Field.Store.YES, Field.Index.NOT_ANALYZED)); document.Add(new Field("Title", job.Title, Field.Store.YES, Field.Index.NOT_ANALYZED)); return document; } public Term GetIndex(LucenceJobModel job) { return new Term("Link", job.Link); } } </code></pre> <p>I am searching for JobTitle, Description and DatePosted fields. Here is the search code:</p> <pre><code>public List&lt;LucenceJobModel&gt; JobsSearch(string keyword, string location, PageInfo pageInfo) { string[] words = keyword.Split(new[] { ' ' }); IndexReader reader = IndexReader.Open(SmartSearch.Instance.GetDirectory(), true); var searcher = new IndexSearcher(reader); var standardAnalyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29); var fields = new[] { "JobTitle", "Description", "DatePosted" }; var searchQuery = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_29, fields, standardAnalyzer); //searchQuery.SetAllowLeadingWildcard(true); // perform the search var query = new BooleanQuery(); foreach (var word in words) { if (!String.IsNullOrEmpty(word)) { var qTemp = searchQuery.Parse(word); var q = searchQuery.Parse(qTemp.ToString().Substring(qTemp.ToString().LastIndexOf(":") + 1) + "*"); query.Add(q, BooleanClause.Occur.MUST); } } int maxDocs = 1; if (reader.MaxDoc() &gt; 0) maxDocs = reader.MaxDoc(); var results = searcher.Search(query, filter, maxDocs); foreach (var scoreDoc in results.scoreDocs) { var document = searcher.Doc(scoreDoc.doc); } var jobs = new List&lt;LucenceJobModel&gt;(); for (int i = 0; i &lt; results.scoreDocs.Length; i++) { var document = searcher.Doc(results.scoreDocs[i].doc); if (i &gt;= (pageInfo.CurrentPage - 1) * pageInfo.ItemsPerPage &amp;&amp; i &lt; pageInfo.CurrentPage * pageInfo.ItemsPerPage) { jobs.Add(LucenceJobModel.ConvertFromDoc(document)); } itemsForGroup.Add(new ItemGroupFor { Company = document.GetField("Company").StringValue(), DatePosted = DateTools.StringToDate(document.GetField("DatePosted").StringValue()), JobType = document.GetField("JobType").StringValue(), Location = document.GetField("Location").StringValue(), Source = document.GetField("Source").StringValue(), Title = document.GetField("Title").StringValue() }); } pageInfo.TotalItems = results.scoreDocs.Length; return jobs; } </code></pre> <p>I want to be able to search for keywords such as "C#" or ".net" without deleting "#" or ".". </p> <p>Q2: I am searching in Location field. Here is code:</p> <pre><code>public List&lt;string&gt; GetLocations(string term) { IndexReader reader = IndexReader.Open(SmartSearch.Instance.GetDirectory(), true); var searcher = new IndexSearcher(reader); var standardAnalyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29); QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "Location", standardAnalyzer); string str = parser.Parse(term).ToString().Substring(parser.Parse(term).ToString().LastIndexOf(":") + 1); PrefixQuery q = new PrefixQuery(new Term("Location", string.Format("{0}", str))); TopDocs results = searcher.Search(q, 5000); return results .scoreDocs .Select(x =&gt; searcher.Doc(x.doc)) .Select(x =&gt; x.GetField("Location").StringValue()) .Distinct() .ToList(); } </code></pre> <p>I want to search for "New york", "New York" and so on. But I know it searches only if case is right. </p>
 

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