java - 在数据库中搜索文本(使用 Hibernate Search)

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

我想实现一个在数据库中搜索文本的搜索查询。 但我总是得到一个空结果,尽管文本出现在数据集中。

  public List<Object> getSearchVorgangResult( final int mandantId, final String searchValue, final List<Integer> projektIds,
                                              final Field field,
                                              final boolean isGrossKlein )
  {
    final EntityManager em = getEntityManager();

    final FullTextEntityManager fullTextEntityManager =
        org.hibernate.search.jpa.Search.getFullTextEntityManager( em );

    // create native Lucene query unsing the query DSL
    // alternatively you can write the Lucene query using the Lucene query parser
    // or the Lucene programmatic API. The Hibernate Search DSL is recommended though
    final QueryBuilder qb = fullTextEntityManager.getSearchFactory()
        .buildQueryBuilder().forEntity( Vorgang.class ).get();

    final List<String> fieldNames = new ArrayList<>();
    System.out.println( field );
    if ( field == null )
    {
      for ( final Field classField : Vorgang.class.getDeclaredFields() )
      {
        fieldNames.add( classField.getName() );
      }
    }
    else
    {
      fieldNames.add( field.getName() );
    }
    String[] fields = new String[fieldNames.size()];
    fields = fieldNames.toArray( fields );

    final org.apache.lucene.search.Query luceneQuery = qb
        .keyword()
        .onFields( fields ).ignoreAnalyzer()
        .matching( searchValue )
        .createQuery();

    // wrap Lucene query in a javax.persistence.Query
    final javax.persistence.Query jpaQuery =
        fullTextEntityManager.createFullTextQuery( luceneQuery, Vorgang.class );

    // execute search
    return jpaQuery.getResultList();
  }

我不知道为什么结果总是空的。

最佳答案

我回答您有关整数字段或一般数字字段的问题。

您必须使用 hibernate 搜索注释,该注释允许您索引整数或任何数字字段:

@FieldBridge(impl=IntegerNumericFieldBridge.class) 
@Field(index=Index.YES, analyze=Analyze.NO, store=Store.NO) @NumericField
private Integer integerValue;

我不知道@NumericField注释是否是可选的。 分析。否,因为您不需要分析数字字段。

要构造查询,您需要使用 lucene NumericRangeQuery,如下所示:

NumericRangeQuery<Integer> integerQuery = NumericRangeQuery.newIntRange(yourIntegerFieldName, minValue, maxValue,  minInclusive, maxInclusive);

如果你需要组合多个查询,你必须使用另一个对象,即lucene BooleanQuery:

BooleanQuery luceneBooleanQuery = new BooleanQuery(); // creates a boolean query

luceneBooleanQuery.add(integerQuery, BooleanClause.Occur.MUST); // adds integerQuery to luceneBooleanQuery. You can add many queries

最后你可以在 jpa 查询中包装 lucene 查询

javax.persistence.Query jpaQuery =
        fullTextEntityManager.createFullTextQuery(luceneBooleanQuery, Vorgang.class );

希望对你有帮助。

关于java - 在数据库中搜索文本(使用 Hibernate Search),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31136187/

相关文章:

java - 另一个谷歌地图 v2 无法充气,请

java - Lucene 索引 - 很多文档/短语

java - Jpa 合并抛出 StaleObjectStateException?

java - Hibernate - 如何设置空值以便级联工作[有趣!]?

php - Sphinx 搜索 Api SPH_MATCH_EXTENDED 不起作用

javascript - 在排序数组中查找/跟踪元素

java - 包装类的 GSON 反/序列化

java - Maven 在构建我们自己的 custom.jar 时发布和使用第三方运行时 jar

hibernate - 在 Hibernate 顺序标准中将字符串转换为 int

PHP 搜索/过滤功能