elasticsearch 如何使用hibernateSearch 6在ElasticSeacrch中搜索索引实体的所有字段

c0vxltue  于 12个月前  发布在  ElasticSearch
关注(0)|答案(1)|浏览(186)

我有下面的方法,它使用Hibernate Search API在ElasticSearch中搜索。我传递queryString值“addresses.address_key:123”,它在addresses.address_key中搜索,就像字段(addresses.address_key)中提到的那样。
我如何让这个方法接受queryString中的任何输入,例如:first_name:john,并让它在所有FullTextFields中搜索。
ElasticSearch版本:8.10 HibernateSearch版本:6.2.2

public Page<Employee> searchEmployee(String queryString) {
       try {
            SearchSession searchSession = Search.session(entityManager);

            SearchResult<Employee> searchResult = searchSession.search(Employee.class)
                    .extension(ElasticsearchExtension.get())
                    .where(f -> f.simpleQueryString().fields("addresses.address_key").matching(queryString))
                    .fetch(Math.toIntExact(page.getOffset()), page.getPageSize());

            Page<Employee> pageResult = new PageImpl<>(searchResult.hits(), page, searchResult.total().hitCount());
            return pageResult;
        } catch (SearchException pe) {
           throw pe;
        }
}

字符串
被索引的实体类:

@Indexed(index = "employee")
public class Employee {

  @FullTextField(name = "employee_key")
  private String employeeKey;

  @FullTextField(name = "first_name")
  private String firstName;
    
  @IndexedEmbedded(includeEmbeddedObjectId = true, includeDepth = 2)
  private Address addresses;
}

public class Address {
    
  @FullTextField(name = "address_key")
  private String addressKey;

  @FullTextField(name = "street_name")
  private String streetName;

}

fnx2tebb

fnx2tebb1#

在定义 predicate 时,可以显式地将多个字段作为目标:

SearchResult<Employee> searchResult = searchSession.search(Employee.class)
        .where(f -> f.simpleQueryString()
                .fields("employee_key", "first_name", "addresses.address_key")
                .matching(queryString))
        .fetch(Math.toIntExact(page.getOffset()), page.getPageSize());

字符串
目前没有只针对“所有文本字段”的选项;请参阅https://hibernate.atlassian.net/browse/HSEARCH-3926
另外,为了清楚起见,simple-query-string语法不支持显式引用查询字符串中的字段。
在这个例子中:

f.simpleQueryString().fields("addresses.address_key")
        .matching("addresses.address_key:123")


.在搜索字符串(“addresses.address_key:123”)中的“addresses.address_key:“前缀没有特定的含义,只会被解释为在文档中找到的术语。
你也可以这样写:

f.simpleQueryString().fields("addresses.address_key")
        .matching("addresses.address_key 123")


但实际上,我认为你想要的是:

f.simpleQueryString().fields("addresses.address_key")
        .matching("123")

相关问题