1
votes

I have a lucene .net searcher with this configuration by a ,net app with aspnet core2, with RamDirectory i have the same problem. the infomation is only for test porpouse.

Lucene configuration!!

public TextEngine(double bufferSizeMB)
{
    _analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
    _directory = FSDirectory.Open(@"d:\index");
    _writer = new IndexWriter(_directory, _analyzer, true, 
    IndexWriter.MaxFieldLength.UNLIMITED);
    _writer.SetRAMBufferSizeMB(bufferSizeMB);
   _searcher = new IndexSearcher(_directory);
}

write data on the directory public void Index(Publication publication, string subType, string province) { var document = new Document(); document.Add(new Field( "subType", subType, Field.Store.YES, Field.Index.ANALYZED));

  document.Add(new Field(
                    "province",
                    province,
                    Field.Store.YES,
                    Field.Index.ANALYZED));

  document.Add(new Field(
                     "publicationId",
                     publication.Id.ToString(),
                     Field.Store.YES,
                     Field.Index.NO));

  _writer.AddDocument(document);
}

search index text on subtype field

public string[] SearchTopBy(string index, int top)
{
  var parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "subType", _analyzer);
  var query = new TermQuery(new Term("subType", index));
  var documents = _searcher.Search(query, top);
  var response = new List<string>();
  foreach (var document in documents.ScoreDocs)
  {
    var subtype = _searcher.Doc(document.Doc).Get("subType");
    var vecinity = _searcher.Doc(document.Doc).Get("vecinity");
    response.Add(subtype + " " + vecinity);
  }

  return response.ToArray();
}

when I write a unit test like

var engine = new TextEngine(1024);
var publication = new Publication
{
  Id = 1,
  IndexedDescription = "Test publication"
};

engine.Index(publication,
        "plomero",
        "almagro");

publication.Id = 2;
publication.IndexedDescription = "test 2";
engine.Index(publication,
       "plomero",
       "villa del parque");

var result = engine.SearchTopBy("plomero", 2);
Assert.True(result.Length == 2);

I will expect 2 result items, but i got 0 items. can you help me?

1

1 Answers

0
votes

You should close the writer after writing documents to the index. This commits all changes to the index and closes all associated files.

_writer.AddDocument(document);

_writer.Close();

See Apache documentation.