我想在我的项目中使用Lucene搜索,因为它速度快,但存在问题。
我有DB表用户和友谊。我想通过搜索表单找到用户的朋友。使用lucene,我只能通过搜索查询找到所有用户,获取他们的ID,将它们放到实体框架中,然后只接受用户的朋友,但如果系统中的用户数千万,该用户的朋友只是少数呢?所以我需要吸引用户的朋友,然后通过搜索查询过滤它们。我怎样才能做到这一点?我听说Linq到Lucene,但我不确定这是我需要的。请帮助我解决问题。
P.S。对不起我的英语,我来自乌克兰,不会说英语很好:))
我找到了解决方案。我将朋友ID存储在索引中,并通过这些ID找到用户朋友。这不是最好的解决方案,因为索引经常被编辑,但我无法实现另一个想法。
答案 0 :(得分:0)
这可以通过使用filters来完成。这是一个简短的演示,它将结果仅限于那些拥有FriendsWith:Beta的文档。这需要重新索引 受改变的友谊关系影响的文件。可以在contrib包中找到TermsFilter类。
using System;
using System.Collections.Generic;
using Lucene.Net.Analysis;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.Search;
using Lucene.Net.Store;
using Directory = Lucene.Net.Store.Directory;
using Version = Lucene.Net.Util.Version;
namespace ConsoleApplication {
public static class Program {
public static void Main(string[] args) {
var directory = new RAMDirectory();
var analyzer = new KeywordAnalyzer();
using (var writer = new IndexWriter(directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED)) {
var users = new [] { "Alfa", "Beta", "Gamma", "Delta" };
var friendships = new Dictionary<String, String[]> {
{ "Alfa", new [] { "Beta", "Gamma", "Delta" } },
{ "Beta", new [] { "Gamma", "Delta" } },
{ "Gamma", new [] { "Delta" } },
{ "Delta", new String[0] } // Noone likes Delta.
};
foreach (var userName in users) {
var doc = new Document();
doc.Add(new Field("Name", userName, Field.Store.YES, Field.Index.NO));
foreach (var friendName in friendships[userName]) {
doc.Add(new Field("FriendsWith", friendName, Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));
}
writer.AddDocument(doc);
}
writer.Commit();
}
// This should be the real query provided by the user (city, age, description, ...)
var query = new MatchAllDocsQuery();
// Create a filter limiting the result to those being friends with the current user,
// in this example the user "Beta".
var filter = new TermsFilter();
filter.AddTerm(new Term("FriendsWith", "Gamma"));
var reader = IndexReader.Open (directory, readOnly: true);
var searcher = new IndexSearcher(reader);
var result = searcher.Search (query, filter, 10);
foreach(var topDoc in result.ScoreDocs) {
var doc = searcher.Doc(topDoc.Doc);
var foundName = doc.Get ("Name");
Console.WriteLine("Matched user '{0}'", foundName);
}
Console.ReadLine();
}
}
}