Lucene - 检索文档中多值字段的所有值

2023-06-28Java开发问题
3

本文介绍了Lucene - 检索文档中多值字段的所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我在 Lucene 中添加了一个多值字段:

I added a field in Lucene which is multi-valued as such:

String categoriesForItem = getCategories(); // returns "category1, category2, cat3" from a DB call

String [] categoriesForItems = categoriesForItem.split(","; 
for(String cat : categoriesForItems) {
    doc.add(new StringField("categories", cat , Field.Store.YES)); // doc is a Document 
}

稍后当我在某个类别中搜索项目时,一切都按预期工作,但是当我得到一个文档并执行以下操作时:

later when I am searching for items in a category everything works as expected, but when I get a Document and do:

String categories= doc.getField("categories").stringValue(); 

我只获取该文档的最后插入值,而不是为该文档添加的所有值.

I only get the last inserted value for that document rather than all the values that were added for that document.

如何获取为该文档添加的所有值?

How can I get all the values which were added for that document?

推荐答案

你添加到文档中的不是多值单字段,而是多个同名字段.最后,您只检索一个字段.

What you are adding to the document is not multi-valued single field, but multiple fields with the same name. At the end you are only retrieving one field.

使用 public final ListDocument 的 getFields() 代替.

Use public final List<IndexableField> getFields() of Document instead.

这篇关于Lucene - 检索文档中多值字段的所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13