Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform full text search and indexing using ASP.NET Core?

I was looking at how to perform full-text searching and indexing similar to Whoosh in python.

I have looked at Lucene.NET but it looks like it's not compatible with ASP.NET Core (2.0 or higher).

Are there any other alternatives for a full-text search engine in this tech stack?

like image 657
Daniel Avatar asked Nov 16 '18 17:11

Daniel


2 Answers

Entity Framework Core 2.1.0 introduced Full Text Search Compatibility using FreeText and EF Core 2.2.0 introduced Contains.

EF and LINQ using Contains:

string criteria = "Find This";

var items = Inventory.Where(x => EF.Functions.Contains(x.KeySearchField, criteria)).ToList();
like image 153
jmalatia Avatar answered Nov 14 '22 03:11

jmalatia


You can use this nuget package Bsa.Search.Core.

This package compatible with .Net Core 3.1 and has no dependencies.

The library contains 3 index types:

  • MemoryDocumentIndex - fast memory index
  • DiskDocumentIndex stores the index on disk
  • DiskShardDocumentIndex stores large indexes on disk of more than 3 million documents

Example of using Memory index

var field = "*"; // search in any field
var query = "(first & \"second and four*\") | (four* ~3 \'six\')"; //search word: first and phrase with wildcard or four- wildcard on distance 3 with six
var documentIndex = new MemoryDocumentIndex();// instance of new memory index
var content = "six first second four"; // text that is indexed
var searchService = new SearchServiceEngine(documentIndex);//service engine for working with indexes
var doc = new IndexDocument("ExternalId");//the document to be indexed and externalId
doc.Add("content".GetField(content)); // adding a new field: content
searchService.Index(new IndexDocument[]
{
  doc// saving the document to the index
});

var parsed = query.Parse(field); // parsing the text and making a Boolean query
var request = new SearchQueryRequest() // 
{
     Query = parsed,
     Field = field,
};
var result = searchService.Search(request); // 

Result will be

You can use this nuget package Bsa.Search.Core but under .net core 3.1 or .net framework 472

like image 24
Stanislav Avatar answered Nov 14 '22 04:11

Stanislav