具有多个索引和 Lucene.Net 的自动建议/自动完成?

发布于 2024-11-08 01:19:28 字数 177 浏览 0 评论 0原文

在对多个索引构建自动建议/自动完成搜索时,有人有什么建议吗?

更新:我发现这个链接可能是我需要如何实现我的解决方案。

Does anyone have any suggestions when it comes to building an Autosuggestion / Autocomplete search on multiple indexes?

Update: I found this link that may be how I need to approach my solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

や三分注定 2024-11-15 01:19:28

您可以使用 MultiReader 来读取多个阅读器的内容。下面是一个迭代名为“data”的字段中的所有索引术语的示例。您可以在对 .Terms(...) 的调用中指定要开始枚举的位置。您可以指定另一个起点来匹配用户到目前为止输入的内容,以提供术语级别的自动完成功能。

using System;
using Lucene.Net.Analysis;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.Store;

public static class ConsoleApp {
    private static readonly String FieldName = "data";

    public static void Main() {
        var ram1 = Create(prefix: "b");
        var ram2 = Create(prefix: "a");

        var multiReader = new MultiReader(new[] {
            IndexReader.Open(ram1, readOnly: true),
            IndexReader.Open(ram2, readOnly: true)
        });

        var termsEnum = multiReader.Terms(new Term(FieldName));
        do {
            var term = termsEnum.Term();
            if (term.Field() != FieldName)
                break;

            Console.WriteLine(term.Text());
        } while (termsEnum.Next());
    }

    public static Directory Create(String prefix) {
        var dir = new RAMDirectory();

        var writer = new IndexWriter(dir, a: new KeywordAnalyzer(), create: true, mfl: IndexWriter.MaxFieldLength.UNLIMITED);
        for (var i = 0; i < 5; ++i) {
            var doc = new Document();
            doc.Add(new Field(FieldName, prefix + i, Field.Store.NO, Field.Index.NOT_ANALYZED));
            writer.AddDocument(doc);
        }
        writer.Close();

        return dir;
    }
}

You can use a MultiReader to read from several readers. Here's an example to iterate all indexed terms in a field named "data". You specify where you want to start the enumeration in the call to .Terms(...). You could specify another starting point to match what the user has entered so far, to provide autocompletion on a term level.

using System;
using Lucene.Net.Analysis;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.Store;

public static class ConsoleApp {
    private static readonly String FieldName = "data";

    public static void Main() {
        var ram1 = Create(prefix: "b");
        var ram2 = Create(prefix: "a");

        var multiReader = new MultiReader(new[] {
            IndexReader.Open(ram1, readOnly: true),
            IndexReader.Open(ram2, readOnly: true)
        });

        var termsEnum = multiReader.Terms(new Term(FieldName));
        do {
            var term = termsEnum.Term();
            if (term.Field() != FieldName)
                break;

            Console.WriteLine(term.Text());
        } while (termsEnum.Next());
    }

    public static Directory Create(String prefix) {
        var dir = new RAMDirectory();

        var writer = new IndexWriter(dir, a: new KeywordAnalyzer(), create: true, mfl: IndexWriter.MaxFieldLength.UNLIMITED);
        for (var i = 0; i < 5; ++i) {
            var doc = new Document();
            doc.Add(new Field(FieldName, prefix + i, Field.Store.NO, Field.Index.NOT_ANALYZED));
            writer.AddDocument(doc);
        }
        writer.Close();

        return dir;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文