Lucene.NET search index approach

Posted by Tim Peel on Stack Overflow See other posts from Stack Overflow or by Tim Peel
Published on 2010-03-30T22:22:57Z Indexed on 2010/03/31 1:13 UTC
Read the original article Hit count: 476

Filed under:
|
|
|

Hi,

I am trying to put together a test case for using Lucene.NET on one of our websites. I'd like to do the following:

Index in a single unique id. Index across a comma delimitered string of terms or tags.

For example.

Item 1: Id = 1 Tags = Something,Separated-Term

I will then be structuring the search so I can look for documents against tag i.e.

tags:something OR tags:separate-term

I need to maintain the exact term value in order to search against it.

I have something running, and the search query is being parsed as expected, but I am not seeing any results. Here's some code.

My parser (_luceneAnalyzer is passed into my indexing service):

var parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_CURRENT, "Tags", _luceneAnalyzer);
parser.SetDefaultOperator(QueryParser.Operator.AND);
return parser;

My Lucene.NET document creation:

var doc = new Document();

var id = new Field(
    "Id",
    NumericUtils.IntToPrefixCoded(indexObject.id),
    Field.Store.YES,
    Field.Index.NOT_ANALYZED,
    Field.TermVector.NO);

var tags = new Field(
    "Tags",
    string.Join(",", indexObject.Tags.ToArray()),
    Field.Store.NO,
    Field.Index.ANALYZED,
    Field.TermVector.YES);

doc.Add(id);
doc.Add(tags);

return doc;

My search:

var parser = BuildQueryParser();
var query = parser.Parse(searchQuery);
var searcher = Searcher;

TopDocs hits = searcher.Search(query, null, max);
IList<SearchResult> result = new List<SearchResult>();
float scoreNorm = 1.0f / hits.GetMaxScore();

for (int i = 0; i < hits.scoreDocs.Length; i++)
{
    float score = hits.scoreDocs[i].score * scoreNorm;
    result.Add(CreateSearchResult(searcher.Doc(hits.scoreDocs[i].doc), score));
}

return result;

I have two documents in my index, one with the tag "Something" and one with the tags "Something" and "Separated-Term". It's important for the - to remain in the terms as I want an exact match on the full value.

When I search with "tags:Something" I do not get any results.

Question

What Analyzer should I be using to achieve the search index I am after? Are there any pointers for putting together a search such as this? Why is my current search not returning any results?

Many thanks

© Stack Overflow or respective owner

Related posts about lucene.net

Related posts about ASP.NET