Ellery Familia
Ellery Familia

Reputation: 539

Ranked-Search on C# Collections

Is there a way to do a ranked "Full-Text Search" in memory on a C# collection? At any given point, I'd have under 50 objects in the collection, and I don't mind if it take a second to complete.

Anyone know of any libraries out there?

Upvotes: 2

Views: 1145

Answers (2)

Stanislav
Stanislav

Reputation: 470

You can try the library Bsa.Search.Core This package implements fast object search in memory.

Object search - where you can create a list of objects and quickly search them in memory

var selector = new IndexWordSelector();
var morphology = new DefaultMorphology(new WordDictionary(), selector);
var service = new HighlightObjectService(morphology);
service.AddRange(new HighlightObjectInfo[]
    {
        new HighlightObjectInfo()
        {
            StrongSynonyms = @"Elon Musk | E Musk | Elon M".Split('|').ToList(),
            Name = "Elon Musk",
            Id = "1",
            Type = ObjectType.Person,
            Weak = new HighlightObjectWeak()
            {
                WeakSearchContext = @"SpaceX | Tesla | Boring Company".Split('|').ToList(),
                WeakSynonyms = @"CEO| CTO | Founder | Musk | Elon |chief designer ".Split('|').ToList()
            }
        },
    });
var found = service.Search("Elon Musk. He is the founder, CEO, CTO and chief designer of SpaceX; early investor, CEO and product architect of Tesla, Inc. founder of The Boring Company");
//the service will find next object: Elon Musk|founder|CEO|CTO|CEO|founder

Memory Index search:

 var field = "*";

 var query = "(\"Elon Musk\" | \"E Musk\" | \"Elon M\")|((SpaceX | Tesla | \"Boring Compan\") ~10 (CEO| CTO | Founder | Musk | Elon | \"chief designer))";

 var documentIndex = new MemoryDocumentIndex();
 var content = "Elon Musk. He is the founder, CEO, CTO and chief designer of SpaceX; early investor, CEO and product architect of Tesla, Inc. founder of The Boring Company";

 var searchService = new SearchServiceEngine(documentIndex);

 var doc = new IndexDocument("1");

 doc.Add("content".GetField(content));

 searchService.Index(new IndexDocument[]
 {
     doc
 });

 var parsed = query.Parse(field);


 var request = new SearchQueryRequest()
 {
      Query = parsed,
      Field = field,
      ShowHighlight = true,
      OrderField = SortOrderFields.Relevance,
      Order = SortOrder.Desc,
      Size = 20,
  };
        var result = searchService.Search(request);

Upvotes: 0

Wyatt Barnett
Wyatt Barnett

Reputation: 15673

Check out lucene.net. While it won't directly integrate with your collection you can index things in memory using the RAMDirectory.

Upvotes: 3

Related Questions