Cannot get a simple example to work

8 views
Skip to first unread message

Doug

unread,
May 5, 2010, 9:35:15 PM5/5/10
to activelucenenet-discuss
I've not used Lucene.Net before, so I'm probably doing something
silly, but I thought an example might help others (assuming we can get
it working). I am using version 0.1.11.7807 (the most recent, I
believe).

The following is a console application. Run without parameters, it
adds a few documents into Lucene. Run with parameters, it performs a
search. It returns zero hits.

Any help would be appreciated.

------


using System;

using ActiveLucene.Net;

using Lucene.Net.Analysis.Standard;
using Lucene.Net.Documents;
using Lucene.Net.QueryParsers;
using Lucene.Net.Search;


namespace LuceneTest1
{
public class SearchRecord
{
[LuceneField("text")]
public string FullText { get; set; }
}



class Program
{
private const string IndexDir = "LuceneIndex";

static void Main(string[] args)
{
try
{
if (args.Length > 0)
{
DoSearch(string.Join(" ", args));
}
else
{
DoCreate();
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}



static void DoSearch(string queryString)
{
Console.WriteLine("Searching for: {0}", queryString);

var analyzer = new
StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_CURRENT);
var indexManager = new IndexManager(IndexDir, analyzer);
indexManager.Open();

QueryParser parser = new
QueryParser(Lucene.Net.Util.Version.LUCENE_CURRENT, "text", analyzer);
Query query = parser.Parse(queryString);

using (var indexSearcher =
indexManager.GetIndexSearcher())
{
TopDocs topDocs = indexSearcher.Search(query, 10);

Console.WriteLine("{0} hits", topDocs.totalHits);

foreach (ScoreDoc scoreDoc in topDocs.scoreDocs)
{
Document doc = indexSearcher.Doc(scoreDoc.doc);

SearchRecord record =
LuceneMediator<SearchRecord>.ToRecord(doc);

Console.Write("--> {0}", record.FullText);
}
}

indexManager.Close();
}



static void DoCreate()
{
var analyzer = new
StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_CURRENT);
var indexManager = new IndexManager(IndexDir, analyzer);
indexManager.Open();

using (DisposableIndexWriter indexWriter =
indexManager.GetIndexWriter())
{

indexWriter.AddDocument(LuceneMediator<SearchRecord>.ToDocument(new
SearchRecord { FullText = "Joseph Swisher" }));

indexWriter.AddDocument(LuceneMediator<SearchRecord>.ToDocument(new
SearchRecord { FullText = "George Swisher" }));

indexWriter.AddDocument(LuceneMediator<SearchRecord>.ToDocument(new
SearchRecord { FullText = "Martin Sheue" }));

indexWriter.AddDocument(LuceneMediator<SearchRecord>.ToDocument(new
SearchRecord { FullText = "George Byers" }));
}

indexManager.Close();
}
}
}

Tim S

unread,
May 6, 2010, 12:27:13 PM5/6/10
to activelucenenet-discuss
You're not getting any results because the default for a
LuceneFieldAttribute is to store and do not index...

http://code.google.com/p/activelucenenet/source/browse/trunk/src/ActiveLucene.Net/LuceneAttributes.cs

In this case, you probably want something like this:

[LuceneField("text", StorageBehavior.Store,
IndexBehavior.Analyze)]
public string FullText { get; set; }


Doug

unread,
May 8, 2010, 2:51:43 PM5/8/10
to activelucenenet-discuss
Sorry it took so long to verify, but you're exactly right. After
making the change, the search returns the results I expect. Thanks!

On May 6, 11:27 am, Tim S <t...@schmidthole.com> wrote:
> You're not getting any results because the default for a
> LuceneFieldAttribute is to store and do not index...
>
>        http://code.google.com/p/activelucenenet/source/browse/trunk/src/Acti...
Reply all
Reply to author
Forward
0 new messages