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

斯皮里奥

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

  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();
  }

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

Bilal BBB

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

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

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

我不知道@NumericField批注是否为可选。Analyze.No,因为您不需要分析数字字段。

要构造查询,您需要像这样使用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

最后,您可以将lucene查询包装在jpa查询中

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

希望它能对您有所帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章