Lucene复杂结构搜索

发布于 2024-08-30 08:18:26 字数 978 浏览 1 评论 0原文

基本上,我确实有非常简单的数据库,我想用 Lucene 对其进行索引。 域是:

// Person domain
class Person {
  Set<Pair> keys;
}

// Pair domain
class Pair {
  KeyItem keyItem;
  String value;
}

// KeyItem domain, name is unique field within the DB (!!)
class KeyItem{
  String name;
}

我有数千万个配置文件和数亿个 Pair,但是,由于大多数 KeyItem 的“名称”字段都是重复的,因此只有几十个 KeyItem 实例。 到达该结构以保存 KeyItem 实例。

基本上任何具有任何字段的配置文件都可以保存到该结构中。 假设我们有带有属性的配置文件

- name: Andrew Morton
- eduction:  University of New South Wales, 
- country: Australia, 
- occupation: Linux programmer.

为了存储它,我们将有单个 Profile 实例、4 个 KeyItem 实例:姓名、教育、国家/地区和职业,以及 4 个带有值的 Pair 实例:“Andrew Morton”、“新南威尔士大学” ”、“澳大利亚”和“Linux 程序员”。

所有其他个人资料将引用(全部或部分)KeyItem 的相同实例:姓名、教育、国家/地区和职业。

我的问题是,如何对所有这些进行索引,以便我可以在 Profile 中搜索 KeyItem::name 和 Pair::value 的某些特定值。理想情况下,我希望这种查询能够工作:

姓名:Andrew* AND 职业:Linux*

我应该创建自定义索引器和搜索器吗?或者我可以使用标准的,并以某种方式将 KeyItem 和 Pair 映射为 Lucene 组件?

Basically I do have pretty simple database that I'd like to index with Lucene.
Domains are:

// Person domain
class Person {
  Set<Pair> keys;
}

// Pair domain
class Pair {
  KeyItem keyItem;
  String value;
}

// KeyItem domain, name is unique field within the DB (!!)
class KeyItem{
  String name;
}

I've tens of millions of profiles and hundreds of millions of Pairs, however, since most of KeyItem's "name" fields duplicates, there are only few dozens KeyItem instances.
Came up to that structure to save on KeyItem instances.

Basically any Profile with any fields could be saved into that structure.
Lets say we've profile with properties

- name: Andrew Morton
- eduction:  University of New South Wales, 
- country: Australia, 
- occupation: Linux programmer.

To store it, we'll have single Profile instance, 4 KeyItem instances: name, education,country and occupation, and 4 Pair instances with values: "Andrew Morton", "University of New South Wales", "Australia" and "Linux Programmer".

All other profile will reference (all or some) same instances of KeyItem: name, education, country and occupation.

My question is, how to index all of that so I can search for Profile for some particular values of KeyItem::name and Pair::value. Ideally I'd like that kind of query to work:

name:Andrew* AND occupation:Linux*

Should I create custom Indexer and Searcher? Or I could use standard ones and just map KeyItem and Pair as Lucene components somehow?

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

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

发布评论

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

评论(2

在巴黎塔顶看东京樱花 2024-09-06 08:18:26

我相信您可以使用标准的 Lucene 方法。
我会:

  • 将每个配置文件翻译为 Lucene 文档。
  • 将每一对翻译为本文档中的一个字段。所有字段都需要建立索引,但不一定要存储。
  • 将带有配置文件 ID 的存储字段添加到文档中。
  • 与您的示例类似,使用名称:值对进行搜索。

如果您选择裸 Lucene,您将需要自定义索引器和搜索器,但它们并不难构建。
使用 Solr 可能会更容易,您需要更少的编程。但是,我不知道 Solr 是否允许像我所描述的那样的开放式模式 - 我相信您必须预定义所有字段名称,因此这可能会阻止您使用 Solr。

I believe you can use standard Lucene methodology.
I would:

  • Translate every profile to a Lucene Document.
  • Translate every Pair to a Field in this Document. All Fields need to be indexed, but not necessarily stored.
  • Add a stored Field with a profile id to the Document.
  • Search using name:value pairs similarly to your example.

If you choose bare Lucene, you will need a custom Indexer and Searcher, but they are not hard to build.
It may be easier for you to use Solr, where you need less programming. However, I do not know if Solr allows an open-ended schema like the one I described - I believe you have to predefine all field names, so this may prevent you from using Solr.

半寸时光 2024-09-06 08:18:26

Lucene 基本上根据关键字的出现返回命中文档的列表,而不管查询的类型如何。基本段读取器检查整个索引数据库中而不是感兴趣的特定领域中是否存在关键字。

建议引入执行以下操作的自定义搜索器。

1.使用文档id阅读入围文档。 (我猜想可以重写collect()方法来从IndexSearcher类的search()传递文档ID)。
2.获取字段值并检查是否存在预期的关键字。
3. 仅当文档符合您的自定义标准时才对文档进行评分。

注意:可以修改默认的标准搜索器,而不是从头开始编写自定义搜索器。

Lucene returns the list of hit documents essentially based on the occurence of the keyword/s regardless of the type of query. The fundamental segment reader checks for the presence of keywords in the entire index database rather than in specific field of interest.

Suggest to introduce a custom searcher that performs the following.

1.Read the short-listed documents using the document id. ( I guess the collect() method may be overridden to pass the document id from search() of IndexSearcher class ).
2.Get the field value and check the presence of expected keywords.
3.Subject the document for scoring only if the document meets your custom criteria.

Note : The default standard searcher can be modified rather than writing a custom seacher from scratch.

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