English 中文(简体)
Elastic 搜索/ 休眠搜索 : 如何用 java 中的查询 DSL API 编码 xor 操作员?
原标题:Elastic Search / Hibernate Search : how to code xor operator with query DSL API in java?
I am using hibernate search java API to generate an elastic search query. I have problem to code a xor operator. Typically I wrote : private void applyXorOperator(BooleanPredicateClausesStep boolClausesAppender, BooleanPredicateClausesStep firstBool, BooleanPredicateClausesStep secondBool) { boolClausesAppender.should( boolClausesAppender.must(firstBool).mustNot(secondBool)); boolClausesAppender.should( boolClausesAppender.mustNot(firstBool).must(secondBool)); boolClausesAppender.minimumShouldMatchNumber(1); } How do I say "not both", I was expecting : boolClausesAppender.maximumShouldMatchNumber(1); but this method doesn t exist in the API. Any solution ? Thx.
最佳回答
By definition: a XOR b <=> (a AND NOT b) OR (NOT a AND b) So just implement it that way: private void applyXorOperator(BooleanPredicateClausesStep boolClausesAppender, BooleanPredicateClausesStep firstBool, BooleanPredicateClausesStep secondBool) { SearchPredicate a = firstBool.toPredicate(); SearchPredicate b = secondBool.toPredicate(); boolClausesAppender.should(f -> f.bool() // a AND NOT b .should(f.bool().must(a).mustNot(b)) // OR (NOT a AND b) .should(f.bool().mustNot(a).must(b))); } EDIT: By the way, you don t need first/second to be boolean predicates. This would work too, and seems simpler: private void applyXorOperator(BooleanPredicateClausesStep boolClausesAppender, PredicateFinalStep first, PredicateFinalStep second) { SearchPredicate a = first.toPredicate(); SearchPredicate b = second.toPredicate(); boolClausesAppender.should(f -> f.bool() // a AND NOT b .should(f.bool().must(a).mustNot(b)) // OR (NOT a AND b) .should(f.bool().mustNot(a).must(b))); } EDIT: Also, starting with Hibernate Search 6.2 you can simply use .or(...)/.not(...), since in this case you don t need advanced bool features such as minShouldMatch or completely optional clauses that only affect the score. private void applyXorOperator(BooleanPredicateClausesStep boolClausesAppender, PredicateFinalStep a, PredicateFinalStep b) { boolClausesAppender.should(f -> f.or( f.and(a, f.not(b)), f.and(f.not(a), b))); }
问题回答

暂无回答




相关问题
How to do SQL IN like query in hibernate search

A simulating scenario is: Search for books whose content contains "success" AND author is in a list of passed names(could be thousands of). I looked into filter: http://docs.jboss.org/hibernate/...

Memory leak with paged JPA queries under JBoss AS 5.1

I m trying to integrate Hibernate Search into one of the projects I m currently working on. The first step in such an endeavour is fairly simply - index all the existing entities with Hibernate Search(...

Custom Lucene Sharding with Hibernate Search [closed]

Has anyone experience with custom Lucene sharding / paritioning using Hibernate Search? The documentation of Hibernate Search says the following about Lucene Sharding : In some cases, it is ...

How can I do this simple search?

I have a web app build with Hibernate. In one page, I have one submit button with 5 inputs field1 field2 field3 field4 field5 When all the inputs are null, I make a simple query Query query = ...

Hibernate-Search: How to search dates?

@Entity @Table(name = "USERS") public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; @Column(name = "USERNAME", nullable = false, length = 20) private String ...

Hibernate Search Paging + FullTextSearch + Criteria

I am trying to do a search with some criteria FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(finalQuery, KnowledgeBaseSolution.class).setCriteriaQuery(criteria); and then page it ...

Hibernate Search with index in a different database

I have a database which is readonly (I only have the access to view), but I have to index this database for search. The DAO layer to this table is now using a generic DAO approach with Hibernate+JPA. ...

热门标签