I want to use the suggest feature of elastic-search for autocomplete.
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters.html
it is perfectly fit for my requirements, but I cannot find a way to create an index for this field. There is FieldType
enum type for search-index types, but it does not contain suggest
type, and I don't see any way to extend it. nothing is in the documentation about this. Is it even possible to index some elastic specific field by hibernate-search?
Asked
Active
Viewed 269 times
0

yaroslavTir
- 711
- 2
- 10
- 22
-
suggest is not a type but a fonctionnality, you can use it on any es index-type you want. You can index any json with hibernate-search client yes. Just index some document (using curl maybe) if you want to test suggest, there is plenty of tutorial online (https://qbox.io/blog/how-to-build-did-you-mean-feature-with-elasticsearch-phrase-suggester) – LeBigCat Feb 25 '19 at 17:52
-
There are [several options](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters.html) for suggesters (e.g. term, phrase, completion, etc.), so your field mappings will depend on which type of suggestion you want to use. If you can specify which type of suggestion you want to use, that would help narrow down where to point you for the answer. – dmbaughman Feb 26 '19 at 00:43
-
No, this is not possible in Hibernate Search 5. – yrodiere Feb 26 '19 at 09:24
1 Answers
1
To elaborate on my comment: no, this is not possible in Hibernate Search 5, but...
You can implement autocomplete yourself.
Define an analyzer with an edge-ngram token filer for indexing, and another for querying, as explained here.
Then use these analyzers on the field you want to add autocomplete to:
@Field(name = "myField_autocomplete", analyzer = @Analyzer(definition = "edgeNGram"))
@Field(name = "dummy", analyzer = @Analyzer(definition = "edgeNGram_query")
String myField;
Note the "dummy" field is required because of some limitations in Hibernate Search 5, in order to make sure the "edgeNGram_query" analyzer is available in your index. See here for details about this hack.
Then reindex your data.
Then you will query this way:
String userInput = ...;
QueryBuilder builder = fullTextEntityManager.getSearchFactory().buildQueryBuilder()
.forEntity(Contact.class)
.overridesForField("myField", "edgeNGram_query")
.get();
Query luceneQuery = builder.keyword().onField("myField").matching(userInput).createQuery();
FullTextQuery query = fullTextEntityManager.createQuery( luceneQuery, MyEntity.class );
List<MyEntity> results = query.getResultList();
Note that these limitations (not being able to use the suggest
type, having to hack around to define a query-only analyzer) will be gone in Hibernate Search 6, which is still in development.

yrodiere
- 9,280
- 1
- 13
- 35
-
Thank yrodiere for such a detailed answer. I saw such approach, but as I can see the query did not eliminate duplications(and I have a lot of them), and as I understand the current version of hibernating search does not support aggregation function, except facet, but I cannot use facets against text analyzed fields(actually I don't really understand this restriction) – yaroslavTir Feb 26 '19 at 09:51