1
votes

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.

Q2: How can I search and ignore case?

Update 1

Q1:I am saving jobs using SimpleLucene. Here is the 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);
}

JobIndexDefinition file:

public class JobIndexDefinition : IIndexDefinition<LucenceJobModel>
{
    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);
    }
}

I am searching for JobTitle, Description and DatePosted fields. Here is the search code:

public List<LucenceJobModel> 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() > 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<LucenceJobModel>();
    for (int i = 0; i < results.scoreDocs.Length; i++)
    {
        var document = searcher.Doc(results.scoreDocs[i].doc);
        if (i >= (pageInfo.CurrentPage - 1) * pageInfo.ItemsPerPage && i < 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;
}

I want to be able to search for keywords such as "C#" or ".net" without deleting "#" or ".".

Q2: I am searching in Location field. Here is code:

public List<string> 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 => searcher.Doc(x.doc))
                .Select(x => x.GetField("Location").StringValue())
                .Distinct()
                .ToList();
}

I want to search for "New york", "New York" and so on. But I know it searches only if case is right.

2

2 Answers

4
votes

I'm not familiar with "SimpleLucene" but your code looks a lot more complex than it needs to be.

A few things:

Field names are case-sensitive, you store the job title as "jobtitle" but search for it using "JobTitle". They need to match.

You mentioned that you want to search the DatePosted and Location fields but they are "NOT_ANALYZED" in the code you posted. Change it to "ANALYZED" if you want to search those fields.

Try the whitespace analyzer if you want to keep terms like ".net" and "C#". Keep in mind that the whitespace analyzer does not use the lowercase filter, so a search for ".NET" wont match ".net". You may have to write your own analyzer.

A1: All of the built in Analyzers (except keyword and whitespace) strip the period from a term, so it shouldn't matter if you search for: "net", ".net", ".net", ...net...", etc.. If this isn't the case, there's another problem. Post some code and maybe we can help.

If you need to match terms like ".net" and "C#" you will probably have better luck with the Whitespace Analyzer. If that doesn't meet your needs you will probably have to write your own analyzer.

A2: The Standard Analyzers automatically converts upper-case to lower case, so case is already ignored for you.

This page has good examples of what the various Analyzers do to a phrase.

From the page above:

Analzying "The quick brown fox jumped over the lazy dogs"

org.apache.lucene.analysis.WhitespaceAnalyzer:
    [The] [quick] [brown] [fox] [jumped] [over] [the] [lazy] [dogs] 

org.apache.lucene.analysis.SimpleAnalyzer:
    [the] [quick] [brown] [fox] [jumped] [over] [the] [lazy] [dogs] 

org.apache.lucene.analysis.StopAnalyzer:
    [quick] [brown] [fox] [jumped] [over] [lazy] [dogs] 

org.apache.lucene.analysis.standard.StandardAnalyzer:
    [quick] [brown] [fox] [jumped] [over] [lazy] [dogs] 

org.apache.lucene.analysis.snowball.SnowballAnalyzer:
    [quick] [brown] [fox] [jump] [over] [lazi] [dog] 

Analzying "XY&Z Corporation - [email protected]"

   org.apache.lucene.analysis.WhitespaceAnalyzer:
        [XY&Z] [Corporation] [-] [[email protected]] 

    org.apache.lucene.analysis.SimpleAnalyzer:
        [xy] [z] [corporation] [xyz] [example] [com] 

    org.apache.lucene.analysis.StopAnalyzer:
        [xy] [z] [corporation] [xyz] [example] [com] 

    org.apache.lucene.analysis.standard.StandardAnalyzer:
        [xy&z] [corporation] [xyz@example] [com] 

    org.apache.lucene.analysis.snowball.SnowballAnalyzer:
        [xy&z] [corpor] [xyz@exampl] [com] 
2
votes

A1:

This heavily sounds as you would not use the same Analyzer/Tokenizer for indexing as for querying.

You need to make sure that both are the same for the same field. E.g.

If the term .net is produced while indexing and your query analyzer strips points it will not find anything.

A2: The StandardAnalyzer should take care of this