【问题标题】:Apache Lucene createFullTextQuery returns null object as matchedApache Lucene createFullTextQuery 返回匹配的空对象
【发布时间】:2017-09-20 09:39:48
【问题描述】:

我的以下 Lucene 查询

org.apache.lucene.search.Query luceneQuery = qb.bool() 
            .should(qb.keyword().onField("name").matching(searchString.toLowerCase()).createQuery()).createQuery();
        result= getFullTextEntityManager().createFullTextQuery(luceneQuery, Verb.class).setProjection(ProjectionConstants.THIS, ProjectionConstants.SCORE).setMaxResults(1).getResultList();

返回:[null, 6.170484] for searchString 'mix'

这是我第一次观察到 null 作为匹配项而不是匹配的实例返回。

以下是我的 Verb.class 上的注释

@Field(analyze=Analyze.YES, store=Store.YES, termVector=TermVector.YES)
private String name;

有人知道吗?

TIA

编辑

@Entity
@Table(name="mverb")
@Indexed
public class Verb extends Serializable {

  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  private Integer id;

  @Field(analyze=Analyze.YES, store=Store.YES, termVector=TermVector.YES)
  private String name;

}

在应用程序加载时

indexAllVerbs();
searchVerbs("mix");

功能:

protected FullTextEntityManager getFullTextEntityManager() {
        return Search.getFullTextEntityManager(em);
}

public void indexAllVerbs() {
    for (Verb b : Foo.getVerbs()) {
        getFullTextEntityManager().index(b);
    }
}

public List<Object[]> searchVerbs(String searchString) {
    QueryBuilder qb = getFullTextEntityManager().getSearchFactory().buildQueryBuilder().forEntity(Verb.class).get();
    org.apache.lucene.search.Query luceneQuery = qb.bool() 
            .should(qb.keyword().onField("name").matching(searchString.toLowerCase()).createQuery()).createQuery();
    return getFullTextEntityManager().createFullTextQuery(luceneQuery, Verb.class).setProjection(ProjectionConstants.THIS, ProjectionConstants.SCORE).setMaxResults(1).getResultList();
}

【问题讨论】:

    标签: java lucene full-text-search hibernate-search


    【解决方案1】:

    这很可能意味着从匹配文档中提取的标识符与数据库中的任何实体都不匹配。

    两个可能的原因:

    1. 你在你的 ID 上使用了一个奇异的字段桥,它不能很好地工作。检查您的实施。
    2. 您的索引由于某种原因不同步。 Reindex your database

    【讨论】:

    • 感谢 Yoann,我在搜索操作之前调用了 getFullTextEntityManager.index()。我使用不同的查询字符串执行了几个搜索操作。但它始终因“混合”而失败,而它在其他查询中给出正确的结果。索引关键字长度太小可能是一个问题吗?不一致的行为让我质疑为什么这个特定的实体“混合”返回为空,而其他实体工作正常。
    • @user2745862 请提供完整代码,包括对 getFullTextEntityManager.index() 的调用。一般不需要调用 .index() ,所以你一定是在做一些奇怪的事情。
    • Yoann,如果我在做一些奇怪的事情,我不会感到惊讶。请找到上面的代码。感谢您的帮助
    • @user2745862 你的Foo.getVerbs() 呢?您是否 100% 肯定它返回已经存在的实体?已刷新到数据库的实体?你能看到数据库中的那些实体吗?我问是因为,你知道,从静态方法返回实体几乎不是常见的做法......
    • Yoann,我只是为了便于理解而编写了 Foo.getVerbs(),它基本上是我的存储库服务,它查询数据库并获取持久的动词列表。动词实体无论如何都是一个库,我不更新该表中的条目,因此不存在未刷新数据的问题。必须是别的东西。谢谢
    猜你喜欢
    • 1970-01-01
    • 2018-10-17
    • 1970-01-01
    • 1970-01-01
    • 2011-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多