Skip to content

Commit

Permalink
Use @SearchExtension in documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
yrodiere committed Jun 23, 2022
1 parent ea309fb commit 1b11a85
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions docs/src/main/asciidoc/hibernate-search-orm-elasticsearch.adoc
Expand Up @@ -440,8 +440,7 @@ import org.hibernate.search.backend.elasticsearch.analysis.ElasticsearchAnalysis
import javax.enterprise.context.Dependent;
import javax.inject.Named;
@Dependent
@Named("myAnalysisConfigurer") // <1>
@SearchExtension // <1>
public class AnalysisConfigurer implements ElasticsearchAnalysisConfigurer {
@Override
Expand All @@ -459,13 +458,31 @@ public class AnalysisConfigurer implements ElasticsearchAnalysisConfigurer {
}
}
----
<1> We will need to reference the configurer from the configuration properties, so we make it a named bean.
<1> Annotate the configurer implementation with the `@SearchExtension` qualifier
to tell Quarkus it should be used in the default persistence unit, for all Elasticsearch indexes (by default).
+
The annotation can also target a specific persistence unit (`@SearchExtension(persistenceUnit = "nameOfYourPU")`),
backend (`@SearchExtension(backend = "nameOfYourBackend")`), index (`@SearchExtension(index = "nameOfYourIndex")`),
or a combination of those
(`@SearchExtension(persistenceUnit = "nameOfYourPU", backend = "nameOfYourBackend", index = "nameOfYourIndex")`).
<2> This is a simple analyzer separating the words on spaces, removing any non-ASCII characters by its ASCII counterpart (and thus removing accents) and putting everything in lowercase.
It is used in our examples for the author's names.
<3> We are a bit more aggressive with this one and we include some stemming: we will be able to search for `mystery` and get a result even if the indexed input contains `mysteries`.
It is definitely too aggressive for person names, but it is perfect for the book titles.
<4> Here is the normalizer used for sorting. Very similar to our first analyzer, except we don't tokenize the words as we want one and only one token.

[TIP]
====
Alternatively, if for some reason you can't or don't want to annotate your analysis configurer with `@SearchExtension`,
you can simply annotate it with `@Dependent @Named("myAnalysisConfigurer")`
and then reference it from configuration properties:
[source,properties]
----
quarkus.hibernate-search-orm.elasticsearch.analysis.configurer=bean:myAnalysisConfigurer
----
====

== Adding full text capabilities to our REST service

In our existing `LibraryResource`, we just need to inject the `SearchSession`:
Expand Down Expand Up @@ -547,14 +564,13 @@ quarkus.datasource.db-kind=postgresql <2>
quarkus.hibernate-orm.sql-load-script=import.sql <3>
quarkus.hibernate-search-orm.elasticsearch.version=7 <4>
quarkus.hibernate-search-orm.elasticsearch.analysis.configurer=bean:myAnalysisConfigurer <5>
quarkus.hibernate-search-orm.automatic-indexing.synchronization.strategy=sync <6>
quarkus.hibernate-search-orm.automatic-indexing.synchronization.strategy=sync <5>
%prod.quarkus.datasource.jdbc.url=jdbc:postgresql://localhost/quarkus_test <7>
%prod.quarkus.datasource.jdbc.url=jdbc:postgresql://localhost/quarkus_test <6>
%prod.quarkus.datasource.username=quarkus_test
%prod.quarkus.datasource.password=quarkus_test
%prod.quarkus.hibernate-orm.database.generation=create
%prod.hibernate-search-orm.elasticsearch.hosts=localhost:9200 <7>
%prod.hibernate-search-orm.elasticsearch.hosts=localhost:9200 <6>
----
<1> We won't use SSL, so we disable it to have a more compact native executable.
<2> Let's create a PostgreSQL datasource.
Expand All @@ -563,11 +579,10 @@ quarkus.hibernate-search-orm.automatic-indexing.synchronization.strategy=sync <6
It is important because there are significant differences between Elasticsearch mapping syntax depending on the version.
Since the mapping is created at build time to reduce startup time, Hibernate Search cannot connect to the cluster to automatically detect the version.
Note that, for OpenSearch, you need to prefix the version with `opensearch:`; see <<opensearch>>.
<5> We point to the custom `AnalysisConfigurer` which defines the configuration of our analyzers and normalizers.
<6> This means that we wait for the entities to be searchable before considering a write complete.
<5> This means that we wait for the entities to be searchable before considering a write complete.
On a production setup, the `write-sync` default will provide better performance.
Using `sync` is especially important when testing as you need the entities to be searchable immediately.
<7> For development and tests, we rely on <<dev-services,Dev Services>>,
<6> For development and tests, we rely on <<dev-services,Dev Services>>,
which means Quarkus will start a PostgreSQL database and Elasticsearch cluster automatically.
In production mode, however,
you will want to start a PostgreSQL database and Elasticsearch cluster manually,
Expand Down

0 comments on commit 1b11a85

Please sign in to comment.