【问题标题】:Search in database for text (with Hibernate Search)在数据库中搜索文本(使用 Hibernate Search)
【发布时间】:2015-09-17 03:02:42
【问题描述】:

我想实现一个在数据库中搜索文本的搜索查询。 但是我总是得到一个空的结果,尽管文本出现在 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();
  }

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

【问题讨论】:

  • 在我看来,您创建了一个查询,但从不执行它。目录 dir = new RAMDirectory(); IndexReader idxReader = new IndexReader(dir); idxSearcher idxSearcher = new IndexSearcher(idxReader) 查询 q = new TermQuery(new Term(“field”, “value”)); idxSearcher.search(q);从这里:link
  • 您有什么理由使用ignoreAnalyzer()?您可以尝试删除 ignoreAnalyzer() 并尝试 .matching( searchValue.toUpperCase() ) 或 .matching( "%SOMEVALUETHATEXISTS%" )。您还可以检查索引是否正确
  • 如果我删除了 ignoreAnalyzer(),我会收到以下错误:org.hibernate.search.exception.EmptyQueryException: HSEARCH000146: 应用于字段 'anfrageEingangAm' 的查询字符串 ' ' 没有有意义的标记匹配。根据应用于此字段的分析器验证查询输入。 @SujitChaitanya
  • 您所说的“在数据库中搜索文本”是什么意思?你把文本编入索引了吗?
  • 我还添加了:“fullTextEntityManager.createIndexer(Vorgang.class).startAndWait();”现在我得到了 900 多个结果。那太多了。 @BoutayaBilal

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


【解决方案1】:

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

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

@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 );

我希望它可以帮助你。

【讨论】:

  • 感谢您的帮助:) 我发现的问题是所有字段都设置为空(字符串除外)。我不知道为什么会这样。以下是字符串字段搜索的结果:“Resultobject:Vorgang DomainObject: id=763 bezeichnung=Ich habe ein Punkt.ebene=null tableRowNr=null”。数据库中的“ebene”和“tableRowNr”不为空。
  • 字段是什么意思?安装 LUKE 看看你的 index 包含什么,很有帮助
  • 卢克是个例外。 ___ 该位置的目录无效,请查看控制台以获取更多信息。最后一个异常:org.apache.lucene.index.IndexFormatTooNewException:不支持格式版本(资源:MMapIndexInput(path="C:\workspaceLea (mars)\LeaServiceImplementation\luceneIndexes\com.isp.lea.domain.Vorgang\segments. gen")): -3(需要在-2和-2之间)
  • 所以 eben 和 tableRowNrare 目前不在索引中。这怎么可能发生?但它缺少很多领域....
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多