Git Product home page Git Product logo

litedbdirectory's Introduction

LiteDBDirectory

A Lucene.net Directory to store the Lucene.net index files in a LiteDB database to secure the indexed data with a LiteDb password connection. The base implementation is taken from LuceneNetSqlDirectory and converted for LiteDb.

The solution is only for desktop and small ASP.NET applications. For large applications hosted on webfarms, it is recommended to use LuceneNetSqlDirectory.

Nuget Package

Install-Package LuceneLiteDbDirectory

Using LiteDbDirectory

Initializing LiteDbDirectory

string connectionString = $"Filename={Path.Combine(Environment.CurrentDirectory, "MyIndex.Db")};password=somepassword;";

using (var db = new LiteDatabase(connectionString))
{
    LiteDbDirectory liteDbDirectory = new LiteDbDirectory(db);
    try
    {
        liteDbDirectory.CheckRequiredCollection();
    }
    catch (ConfigurationErrorsException e)
    {
        LiteDbDirectory.CreateRequiredCollections(db, dropExisting: true);
    }
}

Using LiteDbDirectory for indexing data

using Lucene.Net.Analysis.Standard;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.Store.LiteDbDirectory;
/* Indexing code */
using (var db = new LiteDatabase(connectionString))
{
	var indexWriter = new IndexWriter(directory, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30),
		!IndexReader.IndexExists(directory),
		new IndexWriter.MaxFieldLength(IndexWriter.DEFAULT_MAX_FIELD_LENGTH));

	indexWriter.SetRAMBufferSizeMB(500);

	var bookPages = _libraryService.GetAllBookPages();  // You service layer to load data

	foreach(var page in bookPages)
	{
		var bookPageDoc = new Document();
		doc.Add(new Field("id", page.Id, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO));
		doc.Add(new Field("book-title", page.Title, Field.Store.NO, Field.Index.ANALYZED, Field.TermVector.NO));
		doc.Add(new Field("book-page", page.Text, Field.Store.NO, Field.Index.ANALYZED, Field.TermVector.NO));
			
		indexWriter.AddDocument(bookPageDoc);
	}
	indexWriter.Flush(true, true, true);
	indexWriter.Commit();
	indexWriter.Dispose();
}

Using LiteDbDirectory for Searching data

using Lucene.Net.Analysis.Standard;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.Store.LiteDbDirectory;
/* searching code */
using (var db = new LiteDatabase(connectionString))
{
	LiteDbDirectory liteDbDirectory = new LiteDbDirectory(db);
    
	IndexSearcher searcher = new IndexSearcher(liteDbDirectory);
	var parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "book-page", new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30));
	var query = parser.Parse("text to search");
	hits = searcher.Search(query, 100);

	Console.WriteLine("Found {0} results for {1}", hits.TotalHits, phrase);
	foreach (var hitsScoreDoc in hits.ScoreDocs)
	{
	  var doc = searcher.IndexReader[hitsScoreDoc.Doc];
	  Console.WriteLine("Book id: {0}", doc.GetValues("id")[0]);
	}
}

litedbdirectory's People

Contributors

sheryever avatar smartcaveman avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.