I have following method which searches in ElasticSearch using Hibernate Search API. I pass queryString value as "addresses.address_key:123" which searches in addresses.address_key as mentioned in fields(addresses.address_key).
How can I make this method take any input in queryString, for example: first_name:john and make it search in all FullTextFields.
ElasticSearch version: 8.10 HibernateSearch version: 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;
}
}
Entity classes which are indexed:
@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;
}
You can target multiple fields explicitly when you define your predicate:
There is no option to just target "all text fields" at the moment; see https://hibernate.atlassian.net/browse/HSEARCH-3926 .
Also, just to be clear, the simple-query-string syntax does not support explicitly referring to fields in the query string.
In this example:
... the "addresses.address_key:" prefix in the searched string ("addresses.address_key:123") has not particular meaning and will just be interpreted as terms to be found in the document.
You might as well have written it like this:
But really, I think what you want is this: