- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我希望能够根据其索引字段的任何部分找到一个实体,并且这些字段在索引时不得丢失任何内容。
假设我有以下示例实体类:
@Entity
public class E {
private String f;
// ...
}
如果一个实体中的 f
值是 “这是一个不错的字段!”
,我希望能够通过任何这些查询找到它:
最明显的决定是以这种方式注释实体:
@Entity
@Indexed
@AnalyzerDef(name = "a",
tokenizer = @TokenizerDef(factory = KeywordTokenizerFactory.class),
filters = @TokenFilterDef(factory = LowerCaseFilterFactory.class)
)
@Analyzer(definition = "a")
public class E {
@Field
private String f;
// ...
}
然后按以下方式搜索:
String queryString;
// ...
org.apache.lucene.search.Query query = queryBuilder
.keyword()
.wildcard()
.onField("f")
.matching("*" + queryString.toLowerCase() + "*")
.createQuery();
但文档中指出 for performance purposes, it is recommended that the query does not start with either ? or * .
据我了解,这个方法是无效的。
另一个想法是使用像这样的 n-gram:
@Entity
@Indexed
@AnalyzerDef(name = "a",
tokenizer = @TokenizerDef(factory = KeywordTokenizerFactory.class),
filters = {
@TokenFilterDef(factory = LowerCaseFilterFactory.class),
@TokenFilterDef(factory = NGramFilterFactory.class,
params = {
@Parameter(name = "minGramSize", value = "1"),
@Parameter(name = "maxGramSize", value = E.MAX_LENGTH)
})
}
)
@Analyzer(definition = "a")
public class E {
static final String MAX_LENGTH = "42";
@Field
private String f;
// ...
}
并以这种方式创建查询:
String queryString;
// ...
org.apache.lucene.search.Query query = queryBuilder
.keyword()
.onField("f")
.ignoreAnalyzer()
.matching(queryString.toLowerCase())
.createQuery();
这次没有使用通配符查询,并且查询中的分析器被忽略。我不确定忽略分析器是好是坏,但它可以在忽略分析器的情况下工作。
其他可能的解决方案是在使用 n-gram 时使用 WhitespaceTokenizerFactory
而不是 KeywordTokenizerFactory
,然后用空格分割 queryString
并合并搜索每个子字符串使用 MUST 。据我了解,在这种方法中,如果 f
中包含的字符串的长度是 E.MAX_LENGTH
,那么我构建的 n-gram 会少很多,那么必须是有利于性能。我还可以通过例如“hi ield”查询来查找前面描述的实体。那将是理想的。
那么解决我的问题的最佳方法是什么?还是我的想法都不好?
附注使用 n-gram 时是否应该忽略查询中的分析器?
最佳答案
Other possible solution would be to use WhitespaceTokenizerFactory instead of KeywordTokenizerFactory when using n-grams, then split queryString by spaces and combine searches for each substring using MUST. In this approach, as I understand, I will get a lot less n-grams built, if the length of the string contained in f is E.MAX_LENGTH, what must be good for performance. And I will also be able to find the previously described entity by, for example, "hi ield" query. And that would be ideal.
这或多或少是理想的解决方案,除了一件事:查询时不应该忽略分析器。您应该做的是定义另一个不带 ngram 过滤器但带有分词器、小写过滤器等的分析器,并明确指示 Hibernate Search 在查询时使用该分析器。
其他解决方案都太昂贵,无论是查询时的 I/O 和 CPU(第一个解决方案)还是存储空间(第二个解决方案)。请注意,第三种解决方案在存储空间方面可能仍然相当昂贵,具体取决于 E.MAX_LENGTH
的值。通常建议 minGramSize
和 maxGramSize
之间仅相差一到两个,以避免索引过多的克。
只需定义另一个分析器,将其命名为“ngram_query”,当您需要构建查询时,创建查询构建器,如下所示:
QueryBuilder queryBuilder = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(EPCAsset.class)
.overridesForField( "f" /* name of the field */, "ngram_query" )
.get();
然后照常创建您的查询。
请注意,如果您依靠 Hibernate Search 将索引模式和分析器推送到 Elasticsearch,则必须使用 hack 才能推送仅查询分析器:默认情况下仅推送实际使用的分析器在索引期间被推送。请参阅https://discourse.hibernate.org/t/cannot-find-the-overridden-analyzer-when-using-overridesforfield/1043/4
关于java - hibernate 搜索 : Search any part of the field without losing field's content while indexing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56083137/
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 7 年前。 Improve t
我正在处理 SQL 和用户输入。因此,我使用 glue 库来处理参数化查询。 但是,为了保持简洁,我将所有内容包装在一个函数中: safeQuery SELECT * FROM foo WHERE
我的拓扑是这样的:kafka(p:6)->reduce(p:6)->db writer(p:12)(其中p:是并行)。 我让它在单个节点“集群”上运行 taskmanager.numberOfTask
我有一个 Singleton 类,我试图用它来存储从 Internet 下载的图像,以便我可以随时访问它。工作流程是我登录到 Facebook,并将图像下载到 Singleton 类。一切都很好,它显
首先,这是我的声明和初始化: int** GameField = 0; GameField = new int*[mapsize]; for(int i = 0; i
我正在使用 pthreads 编写一个多线程演示程序,其中一个线程将数据加载到 STL 队列中,另一个线程从中读取数据。听起来微不足道,对吧?不幸的是,插入队列的数据正在消失。我对多线程并不陌生,也不
我有一个函数 (GetArgs) 使用可变参数模板对它的参数(每个指针)进行排序,将每个参数发送到另一个函数 (GetArg),该函数已为每种类型重载(目前是整数和 float )。每种类型的重载函数
我成功地使用蓝牙连接了 iPhone 和 Mac。 iPhone 是核心,Mac 是外围设备。连接后,两者都可以通信(iPhone 正在订阅一些特征,由 Mac 宣传)。 但是,经过一段时间(随机?)
这是一个足够简单的问题,令我感到惊讶的是,我找不到任何对之前问过它的人的引用。和this不一样,也不受 this discussion 的保护. 我有一个 4-d 矩阵(尺寸为 16x10x15x39
我正在使用两个组件,并且我正在使用这种模式:子组件应尽可能保持隔离 - 它正在处理自己的验证错误。父组件应该检查子组件之间存在依赖关系的错误。因此,就我而言:密码字段和密码确认字段。 这是我的代码
我有一个应用程序,用户可以通过两种方式浏览 map (例如缩略图和列表中的 map ) /map/browse /map/list 现在,我想将这些 View 限制为仅显示特定用户的 map ,例如通
我有这种类型和这些功能: data Tag a where Tag :: (Show a, Eq a, Ord a, Storable a, Binary a) => a -> BL.Byte
我写了一个非常简单的 Flink 流作业,它使用 FlinkKafkaConsumer082 从 Kafka 获取数据。 protected DataStream getKafkaStream(Str
几天前我不小心删除了我的.bashrc。我尝试重新创建它,但不幸的是我仍然遇到一些问题。我以前使用的 Java 调试器不再正常工作,因为它找不到这个类: aetherboard:Home shwang
我正在尝试序列化/反序列化以下内容 @JsonTypeInfo(use = JsonTypeInfo.Id.NAME) @JsonSubTypes({ @JsonSubTypes.Type(v
我目前正在编写一个非常小的Java程序来实现一次性pad,其中pad(或 key )本身是使用SecureRandom对象生成为一系列字节的,该对象使用一个简单的字符串进行播种SHA-512 算法。
您可以通过在物理设备上下载并运行此代码来重现错误:https://github.com/Hoya/RCCPeakableImageView 它所做的是通过 UIImageView 子类向图像添加视差效
我在使用 google analytis 时遇到了一些问题。我们有一个网站,用户可以在登陆页面上填写个人信息(姓名、地址、电子邮件等)。该网站位于http://link.提交表单后,用户将被定向到 h
我正在创建一个可以使用键盘控件旋转的 3D 立方体。但是在旋转之后,每张脸的部分失去了交互性(鼠标事件不会在包含的元素上注册)。任何人都知道可能导致该问题的原因是什么? 这很难解释,所以这里有一个测试
我今天在调试 SWIG 类型映射时遇到了一个有趣的问题。任何人都想告诉我为什么 Visual C++ 2008 在从 ourLib::Char * 转换为 const ourLib::Char * &
我是一名优秀的程序员,十分优秀!