gpt4 book ai didi

java - 如何对复杂类型的 Hibernate 搜索结果进行排序

转载 作者:行者123 更新时间:2023-12-02 11:26:55 26 4
gpt4 key购买 nike

我想根据车牌和fleetCustomer.customerId之后的订单模糊搜索所有车辆。

到目前为止,我可以进行模糊搜索,但排序部分似乎很难在我的时间里自己完成。

hibernate-search库的版本是5.5.5.final

我有 2 个实体:

车辆:

@Entity
@Table(name = "VEHICLE")
@Indexed
public class VehicleDao {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "vehicle_id")
private Long vehicleId;

@Column(name = "license_plate", length = 50)
@Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
private String licensePlate;

@IndexedEmbedded
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "fleetCustomer")
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
private CustomerDao fleetCustomer;

...
}

和客户:

@Embeddable
@Entity
@Table(name="CUSTOMER")
public class CustomerDao {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "CUSTOMER_ID", nullable = false)
private Long customerId;

...

}

到目前为止,我尝试的是将排序属性作为字段添加到搜索中,但这不起作用,没有返回结果。

这是我到目前为止所尝试过的,模糊搜索有效。从代码中您可以看出,fleetCustomer.customerId = 1025 的车辆必须是第一辆。

        org.apache.lucene.search.Query luceneQuery = b.bool()
.must( b.keyword().fuzzy().onField("licensePlate").boostedTo(3)
.matching("mhp 070").createQuery() )
// .must( b.keyword().onField("fleetCustomer.customerId").ignoreFieldBridge()
// .matching("1025").createQuery() )
.createQuery();

final org.hibernate.search.jpa.FullTextQuery fullTextQuery =
fullTextEntityManager.createFullTextQuery( luceneQuery , VehicleDao.class);

fullTextQuery.setFirstResult(1);
fullTextQuery.setMaxResults(5);

List<T> resultList = fullTextQuery.getResultList();

非常感谢任何帮助。

最佳答案

如果您想对结果进行排序,则不应更改查询。您应该添加排序。

编辑:我没有看到您使用的是搜索 5.5。这是搜索 5.5 的解决方案。这个版本已经很旧了,如果可以的话,你应该考虑升级。

添加一个字段以根据客户 ID 进行排序:

@Embeddable
@Entity
@Table(name="CUSTOMER")
public class CustomerDao {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "CUSTOMER_ID", nullable = false)
@Field(name = "customerId_sort") // <== Add this
@SortableField(forField = "customerId_sort") // <== And this
private Long customerId;

...

}

然后在查询时添加排序:

        org.apache.lucene.search.Query luceneQuery = b.bool()
.must( b.keyword().fuzzy().onField("licensePlate").boostedTo(3)
.matching("mhp 070").createQuery() )
.createQuery();

final org.hibernate.search.jpa.FullTextQuery fullTextQuery =
fullTextEntityManager.createFullTextQuery( luceneQuery , VehicleDao.class);


// Add this
org.apache.lucene.search.Sort sort = new org.apache.lucene.search.Sort( new org.apache.lucene.search.SortField( "fleetCustomer.customerId_sort", org.apache.lucene.search.SortField.Type.LONG ) )
fullTextQuery.setSort( sort );

fullTextQuery.setFirstResult(1);
fullTextQuery.setMaxResults(5);

List<T> resultList = fullTextQuery.getResultList();

如果您是 Hibernate Search 的新手,尤其是全文世界的新手,我建议您查看 the section about sorts in the Hibernate Search documentation .

<小时/>

原始解决方案(适用于搜索 5.6 及更高版本):

添加一个字段以根据客户 ID 进行排序:

@Embeddable
@Entity
@Table(name="CUSTOMER")
public class CustomerDao {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "CUSTOMER_ID", nullable = false)
@Field(name = "customerId_sort") // <== Add this
@SortableField(forField = "customerId_sort") // <== And this
private Long customerId;

...

}

然后在查询时添加排序:

        org.apache.lucene.search.Query luceneQuery = b.bool()
.must( b.keyword().fuzzy().onField("licensePlate").boostedTo(3)
.matching("mhp 070").createQuery() )
.createQuery();

final org.hibernate.search.jpa.FullTextQuery fullTextQuery =
fullTextEntityManager.createFullTextQuery( luceneQuery , VehicleDao.class);


// Add this
org.apache.lucene.search.Sort sort = b.sort().byField( "fleetCustomer.customerId_sort" ).createSort();
fullTextQuery.setSort(sort);

fullTextQuery.setFirstResult(1);
fullTextQuery.setMaxResults(5);

List<T> resultList = fullTextQuery.getResultList();

如果您是 Hibernate Search 的新手,尤其是全文世界的新手,我建议您查看 the section about sorts in the Hibernate Search documentation .

关于java - 如何对复杂类型的 Hibernate 搜索结果进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49553352/

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