我正在使用 Hibernate Search(它使用 Lucene)来搜索我在目录中编入索引的一些数据。它工作正常,但我需要进行反向搜索。通过反向搜索,我的意思是我的数据库中存储了一个查询列表,每次创建数据对象时,我都需要检查这些查询中的哪一个与数据对象匹配。当数据对象与他创建的查询匹配时,我需要它来提醒用户。所以我需要索引这个刚刚创建的单个数据对象,并查看我的列表中的哪些查询有这个对象。
我已经看到 Lucene MemoryIndex Class 在内存中创建索引,所以我可以对列表中的每个查询执行类似这个示例的操作(尽管在 Java 查询列表中迭代不会非常有效):
//Iterating over my list<Query>
MemoryIndex index = new MemoryIndex();
//Add all fields
index.addField("myField", "myFieldData", analyzer);
...
QueryParser parser = new QueryParser("myField", analyzer);
float score = index.search(query);
if (score > 0.0f) {
System.out.println("it's a match");
} else {
System.out.println("no match found");
}
Run Code Online (Sandbox Code Playgroud)
这里的问题是这个数据类有几个 Hibernate Search Annotations @Field,@IndexedEmbedded,... 指示字段应该如何被索引,所以当我在 FullTextEntityManager 实例上调用 index() 方法时,它使用这个信息来索引对象在目录中。是否有类似的方法可以使用此信息在内存中对其进行索引?
有没有更有效的方法来进行这种反向搜索?
我没有尝试这件事.我的要求是按照他们的名字搜索记录
以下是我的相关课程:
RecordFolderAnalysis.java
@Indexed
public class RecordFolderAnalysis extends AuditableEntity implements Serializable {
@ManyToOne
@JoinColumn(name = "arrivalId", nullable = false)
@ContainedIn
private RecordFolderArrival recordFolderArrival;
}
Run Code Online (Sandbox Code Playgroud)
RecordFolderArrival.java
@Indexed
public class RecordFolderArrival extends BaseEntity implements Serializable
{
@Column(name="recordName", unique = true, nullable = false)
@Field(index = Index.UN_TOKENIZED)
private String recordName;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "recordFolderArrival", fetch = FetchType.LAZY, targetEntity = RecordFolderAnalysis.class)
@IndexedEmbedded
private List<RecordFolderAnalysis> recordFolderAnalysis=new ArrayList<>();
}
Run Code Online (Sandbox Code Playgroud)
Follwing是我的DAO类方法:
@Override
public List<T> search(final String queryString, final String... fields) {
List searchResult = hibernateTemplate.executeFind(new …Run Code Online (Sandbox Code Playgroud) Hibernate Search(lucene)是否有可能组合两个不同的查询.例如,当我想要搜索具有一个对应匹配值的2个字段时:
firstname - John
lastname - Doe
qBuilder.keyword().onField("firstname").matching("John").createQuery());
qBuilder.keyword().onField("lastname").matching("Doe").createQuery());
Run Code Online (Sandbox Code Playgroud)
是一种从这一个查询?
我正在使用 Hibernate lucene 进行搜索。现在我想搜索具有多对一关系的实体
我有两个类,一个是catalogueBase,另一个是Subject,这里的主题具有多对一关系(它是单向关系)
CatalogueBase.java 类:
@Indexed
@JsonAutoDetect
@Entity
@Table(name="catalogueBase")
public class CatalogueBase extends BaseObject implements Serializable {
// some entities
// ...
private Subject subject;
// setter and get methods
// ...
@Field(index = Index.YES, analyze = Analyze.YES, store = Store.YES)
@ManyToOne
@NotFound(action= NotFoundAction.IGNORE)
@JoinColumn(name = "subject1", insertable = true, updatable=true, nullable = true)
@JsonProperty
public Subject getSubject() {
return subject;
}
public void setSubject(Subject subject) {
this.subject = subject;
}
}
Run Code Online (Sandbox Code Playgroud)
subject.java(我想要搜索的有关主题的内容将存储在描述列中):
@Indexed …Run Code Online (Sandbox Code Playgroud) 我刚刚开始玩休眠搜索。我该如何解决以下问题。
我所有的 java 实体都是从超类继承的:
@SuppressWarnings("rawtypes")
@MappedSuperclass
@Indexed
public abstract class BaseEntity{
private Integer id;
private Integer jpaVersion;
private Date creaDate;
private Date modDate;
private String creaUser;
private String modUser;
@Id
@Column(name = "ID", unique = true, nullable = false, precision = 22, scale = 0)
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
.....
Run Code Online (Sandbox Code Playgroud)
例如我必须(还有更多)子类:
@Entity
@Indexed
@Table(name = "BASE_PERSON", schema = "MYSCHEMA")
public class Person extends extends BaseEntity{ …Run Code Online (Sandbox Code Playgroud) 我已经进行了休眠搜索设置,并且在提交时(重新)将我的注释实体编入索引就好了.
但是我有许多集成测试只有部分初始化的数据.由于这是在hibernate搜索尝试索引此对象时抛出异常,我宁愿完全关闭此测试的索引(或休眠搜索).
我已经尝试过不设置hibernate.search.default.indexBase和hibernate.search.default.directory_provider测试.但这没有帮助.
如何关闭hibernate搜索索引?
在从 4.5 版升级到 Hibernate Search 5 之前,我们的系统将所有文档 ID 索引为数字字段:
@Entity
public class Staff {
@Id
@NumericField
protected Long id;
// other fields
}
Run Code Online (Sandbox Code Playgroud)
这允许我们使用数字范围查询。在 Hibernate 5 中,所有文档 ID 都被索引为字符串,上面的注释会导致异常。如果没有注释,所有数字范围查询都无法正确搜索 ID 字段。
切换到 TermRangeQuery 而不是 NumericRangeQuery 会很乏味,我希望避免这种情况。
有没有办法继续将 ID 视为数值?
在我的 Spring Boot 1.5.10.Final 项目中,我使用 Hibernate Search ORM 5.6.4.Final。除了集成测试之外,它运行良好。有一个测试类有多种测试方法来测试搜索逻辑。如果我只运行这个测试类,一切都会正常。Spring Boot 正在启动并创建索引。如果我将这个测试类与所有其他集成测试一起运行,每个测试类都会抛出 LockObtainFailedException 并且 Hibernate Search 测试将失败。
org.apache.lucene.store.LockObtainFailedException: Lock held by this virtual machine: ...LieferantEntity\write.lock
at org.apache.lucene.store.NativeFSLockFactory.obtainFSLock(NativeFSLockFactory.java:127) ~[lucene-core-5.5.5.jar:5.5.5 b3441673c21c83762035dc21d3827ad16aa17b68 - sarowe - 2017-10-20 08:57:09]
at org.apache.lucene.store.FSLockFactory.obtainLock(FSLockFactory.java:41) ~[lucene-core-5.5.5.jar:5.5.5 b3441673c21c83762035dc21d3827ad16aa17b68 - sarowe - 2017-10-20 08:57:09]
at org.apache.lucene.store.BaseDirectory.obtainLock(BaseDirectory.java:45) ~[lucene-core-5.5.5.jar:5.5.5 b3441673c21c83762035dc21d3827ad16aa17b68 - sarowe - 2017-10-20 08:57:09]
at org.apache.lucene.index.IndexWriter.<init>(IndexWriter.java:776) ~[lucene-core-5.5.5.jar:5.5.5 b3441673c21c83762035dc21d3827ad16aa17b68 - sarowe - 2017-10-20 08:57:09]
at org.hibernate.search.backend.impl.lucene.IndexWriterHolder.createNewIndexWriter(IndexWriterHolder.java:126) ~[hibernate-search-engine-5.6.4.Final.jar:5.6.4.Final]
at org.hibernate.search.backend.impl.lucene.IndexWriterHolder.getIndexWriter(IndexWriterHolder.java:92) ~[hibernate-search-engine-5.6.4.Final.jar:5.6.4.Final]
at org.hibernate.search.backend.impl.lucene.AbstractWorkspaceImpl.getIndexWriter(AbstractWorkspaceImpl.java:117) ~[hibernate-search-engine-5.6.4.Final.jar:5.6.4.Final]
at org.hibernate.search.backend.impl.lucene.AbstractWorkspaceImpl.getIndexWriterDelegate(AbstractWorkspaceImpl.java:203) ~[hibernate-search-engine-5.6.4.Final.jar:5.6.4.Final]
at org.hibernate.search.backend.impl.lucene.LuceneBackendQueueTask.applyUpdates(LuceneBackendQueueTask.java:81) [hibernate-search-engine-5.6.4.Final.jar:5.6.4.Final]
at org.hibernate.search.backend.impl.lucene.LuceneBackendQueueTask.run(LuceneBackendQueueTask.java:46) [hibernate-search-engine-5.6.4.Final.jar:5.6.4.Final]
at org.hibernate.search.backend.impl.lucene.SyncWorkProcessor$Consumer.applyChangesets(SyncWorkProcessor.java:165) …Run Code Online (Sandbox Code Playgroud) 我使用 Java 8、Spring Boot 和 Hibernate Search,看起来应用程序启动在 Lucene 索引初始化时停止。尝试使用以前用 SB 2.2.7 构建的索引文件,但没有成功。还尝试删除 Lucene 制作的所有文件并重新开始,但没有成功。尽管我将所有内容都放在跟踪日志级别中,但日志中没有任何内容可以帮助了解发生了什么。
请参阅 pom.xml 和下面的最后几行日志。如果需要,我可以提供完整的跟踪日志。
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>4.0.0</modelVersion>
<groupId>com.thevegcat</groupId>
<artifactId>TheVegCat</artifactId>
<version>0.4.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>The Vegan Catalog</name>
<description>The Best World Vegan Catalog by H.Lo</description>
<developers>
<developer>
<id>HLo</id>
<name>Hrvoje Lon?ar</name>
<email>horvoje@gmail.com</email>
</developer>
</developers>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath />
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<hibernate.version>5.4.16.Final</hibernate.version>
<groovy-all.version>3.0.4</groovy-all.version>
<junit-jupiter.version>5.6.2</junit-jupiter.version>
<tika-core.version>1.24.1</tika-core.version>
<jsoup.version>1.13.1</jsoup.version>
<hibernate-search-orm.version>5.11.5.Final</hibernate-search-orm.version>
<opencsv.version>5.2</opencsv.version>
<org.eclipse.jdt.annotation.version>2.2.400</org.eclipse.jdt.annotation.version>
<jacoco.version>0.8.5</jacoco.version>
<junit-platform-surefire-provider.version>1.3.2</junit-platform-surefire-provider.version>
<commons-io.version>2.6</commons-io.version>
<urlrewritefilter.version>4.0.4</urlrewritefilter.version>
<json-simple.version>1.1.1</json-simple.version>
</properties>
<dependencies>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
</dependency> …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用最新版本从头开始设置 Spring Boot 3.0.1 项目。到目前为止,我设法使 mvc 和存储库正常工作,但是在添加 Hibernate Search 时,事情将不再起作用。
下面你可以看到我的pom.xml文件。
当我进入“依赖层次结构”(Eclipse STS)时,我可以找到hibernate-core 5.6.11.Final嵌入hibernate-search-mapper-pojo-base 6.1.7.Final 到hibernate-search-mapper-orm 6.1.7.Final. 这是否意味着我必须手动排除此版本并包含更高版本?也hibernate-core 5.6.11.Final有hibernate-commons-annotations 5.1.2.Final嵌入。为什么这么复杂?我有什么遗漏的吗?
在控制台日志中我得到这个:
***************************
APPLICATION FAILED TO START
***************************
Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
org.hibernate.cfg.annotations.BasicValueBinder.resolveJavaType(BasicValueBinder.java:1002)
The following method did not exist:
'java.lang.reflect.Type org.hibernate.annotations.common.reflection.ReflectionManager.toType(org.hibernate.annotations.common.reflection.XClass)'
The calling method's class, org.hibernate.cfg.annotations.BasicValueBinder, was loaded from the following location:
jar:file:/C:/Users/Hrvoje/.m2/repository/org/hibernate/orm/hibernate-core/6.1.6.Final/hibernate-core-6.1.6.Final.jar!/org/hibernate/cfg/annotations/BasicValueBinder.class …Run Code Online (Sandbox Code Playgroud) hibernate-search ×10
hibernate ×7
java ×7
lucene ×6
spring-boot ×3
spring ×2
java-ee-6 ×1
jpa ×1
search ×1