gpt4 book ai didi

java - 如何在 Hibernate Search 3.0.0.ga 中使用多个参数进行搜索

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:59:30 24 4
gpt4 key购买 nike

使用:

  • hibernate 3.2.7.ga
  • hibernate 搜索 3.0.0.ga
  • Hibernate-Anotations 3.3.0.ga
  • Hibernate-Commons-Cnnotations 3.0.0.ga
  • Lucene-Core 2.9.4
  • Lucene 分析器 2.9.4
  • Lucene-Queryparser 2.9.4

如何使用多个参数进行搜索,例如:

SELECT * 
FROM example
WHERE column1 = "text1"
AND (column2 = "text2" OR column2 ="text3")

Hibernate-Search documentation我只找到了搜索的例子:

Session session = universalHibernateDAO.getHibernateSession();
FullTextSession fullTextSession = Search.createFullTextSession(session);

Transaction tx = fullTextSession.beginTransaction();
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_29);
MultiFieldQueryParser parser = new MultiFieldQueryParser(Version.LUCENE_29,
new String[]{"summary", "body"}, analyzer);
org.apache.lucene.search.Query query = parser.parse( "Java rocks!" );
Query hibQuery = fullTextSession.createFullTextQuery( query, PscpExpedient.class );
List<PscpExpedient> result = (List<PscpExpedient>)hibQuery.list();

tx.commit();

如果存在某种方法来做类似的事情,那将是完美的:

org.apache.lucene.search.Query query = parser.parse("column1: text1", 
"column2: text2 | text3" );

此外,如果可能的话,很高兴知道如何搜索“onlyOneword”和“text with multiple spaces”

谢谢!

最佳答案

尝试像这样使用 BooleanQuery:

org.apache.lucene.search.Query query1 = ....;
org.apache.lucene.search.Query query2 = ....;
org.apache.lucene.search.Query query3 = ....;

BooleanQuery booleanQuery = new BooleanQuery();

luceneBooleanQuery.add(query1, BooleanClause.Occur.MUST);
luceneBooleanQuery.add(query2, BooleanClause.Occur.SHOULD);
luceneBooleanQuery.add(query3, BooleanClause.Occur.SHOULD);

FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(booleanQuery, DomainClass.class);

它应该可以工作。

关于java - 如何在 Hibernate Search 3.0.0.ga 中使用多个参数进行搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30366676/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com