Skip to content

Commit

Permalink
HSEARCH-3601 Use better names for analyzers in the getting started guide
Browse files Browse the repository at this point in the history
  • Loading branch information
yrodiere authored and fax4ever committed Jun 17, 2019
1 parent e371f09 commit b6b1914
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 13 deletions.
16 changes: 11 additions & 5 deletions documentation/src/main/asciidoc/getting-started.asciidoc
Expand Up @@ -394,11 +394,14 @@ include::{sourcedir}/org/hibernate/search/documentation/gettingstarted/withhsear
----
include::{resourcesdir}/META-INF/persistence.xml[tags=gettingstarted-configuration-orm_lucene-analysis]
----
<1> Define a custom analyzer named "myAnalyzer".
<1> Define a custom analyzer named "english", to analyze English text such as book titles.
<2> Set the tokenizer to a standard tokenizer. You need to pass factory classes to refer to components.
<3> Set the token filters. Token filters are applied in the order they are given.
<4> Set the value of a parameter for the last added char filter/tokenizer/token filter.
<5> Assign the configurer to the backend "myBackend" in the Hibernate Search configuration (here in `persistence.xml`).
<5> Define another custom analyzer, named "name", to analyze author names.
On contrary to the first one, do not use enable stemming,
as it is unlikely to lead to useful results on proper nouns.
<6> Assign the configurer to the backend "myBackend" in the Hibernate Search configuration (here in `persistence.xml`).
====

.Analysis configurer implementation and configuration in `persistence.xml` for a "Hibernate ORM + Elasticsearch" setup
Expand All @@ -411,13 +414,16 @@ include::{sourcedir}/org/hibernate/search/documentation/gettingstarted/withhsear
----
include::{resourcesdir}/META-INF/persistence.xml[tags=gettingstarted-configuration-orm_elasticsearch-analysis]
----
<1> Define a custom analyzer named "myAnalyzer".
<1> Define a custom analyzer named "english", to analyze English text such as book titles.
<2> Set the tokenizer to a standard tokenizer.
<3> Set the token filters. Token filters are applied in the order they are given.
<4> Note that, for Elasticsearch, any parameterized char filter, tokenizer or token filter
must be defined separately and given a name.
must be defined separately and assigned a name.
<5> Set the value of a parameter for the char filter/tokenizer/token filter being defined.
<6> Assign the configurer to the backend "myBackend" in the Hibernate Search configuration (here in `persistence.xml`).
<6> Define another custom analyzer, named "name", to analyze author names.
On contrary to the first one, do not use enable stemming,
as it is unlikely to lead to useful results on proper nouns.
<7> Assign the configurer to the backend "myBackend" in the Hibernate Search configuration (here in `persistence.xml`).
====

Once analysis is configured, the mapping must be adapted to assign the relevant analyzer to each field:
Expand Down
Expand Up @@ -23,7 +23,7 @@ public class Author {
@GeneratedValue
private Integer id;

@FullTextField(analyzer = "myAnalyzer") // <1>
@FullTextField(analyzer = "name") // <1>
private String name;

@ManyToMany(mappedBy = "authors")
Expand Down
Expand Up @@ -26,7 +26,7 @@ public class Book {
@GeneratedValue
private Integer id;

@FullTextField(analyzer = "myAnalyzer") // <1>
@FullTextField(analyzer = "english") // <1>
private String title;

@ManyToMany
Expand Down
Expand Up @@ -13,13 +13,17 @@
public class MyElasticsearchAnalysisConfigurer implements ElasticsearchAnalysisConfigurer {
@Override
public void configure(ElasticsearchAnalysisDefinitionContainerContext context) {
context.analyzer( "myAnalyzer" ).custom() // <1>
context.analyzer( "english" ).custom() // <1>
.withTokenizer( "standard" ) // <2>
.withTokenFilters( "asciifolding", "lowercase", "mySnowballFilter" ); // <3>
.withTokenFilters( "asciifolding", "lowercase", "snowball_english" ); // <3>

context.tokenFilter( "mySnowballFilter" ) // <4>
context.tokenFilter( "snowball_english" ) // <4>
.type( "snowball" )
.param( "language", "English" ); // <5>

context.analyzer( "name" ).custom() // <6>
.withTokenizer( "standard" )
.withTokenFilters( "asciifolding", "lowercase" );
}
}
// end::include[]
Expand Up @@ -18,12 +18,17 @@
public class MyLuceneAnalysisConfigurer implements LuceneAnalysisConfigurer {
@Override
public void configure(LuceneAnalysisDefinitionContainerContext context) {
context.analyzer( "myAnalyzer" ).custom() // <1>
context.analyzer( "english" ).custom() // <1>
.tokenizer( StandardTokenizerFactory.class ) // <2>
.tokenFilter( ASCIIFoldingFilterFactory.class ) // <3>
.tokenFilter( LowerCaseFilterFactory.class ) // <3>
.tokenFilter( SnowballPorterFilterFactory.class ) // <3>
.param( "language", "English" ); // <4>

context.analyzer( "name" ).custom() // <5>
.tokenizer( StandardTokenizerFactory.class )
.tokenFilter( ASCIIFoldingFilterFactory.class )
.tokenFilter( LowerCaseFilterFactory.class );
}
}
// end::include[]
4 changes: 2 additions & 2 deletions documentation/src/test/resources/META-INF/persistence.xml
Expand Up @@ -106,7 +106,7 @@
value="local_directory"/>
<!-- tag::gettingstarted-configuration-orm_lucene-analysis[] -->
<property name="hibernate.search.backends.myBackend.analysis_configurer"
value="org.hibernate.search.documentation.gettingstarted.withhsearch.withanalysis.MyLuceneAnalysisConfigurer"/> <!--5-->
value="org.hibernate.search.documentation.gettingstarted.withhsearch.withanalysis.MyLuceneAnalysisConfigurer"/> <!--6-->
<!-- end::gettingstarted-configuration-orm_lucene-analysis[] -->
<property name="hibernate.search.default_backend"
value="myBackend"/>
Expand All @@ -131,7 +131,7 @@
value="elasticsearch" />
<!-- tag::gettingstarted-configuration-orm_elasticsearch-analysis[] -->
<property name="hibernate.search.backends.myBackend.analysis_configurer"
value="org.hibernate.search.documentation.gettingstarted.withhsearch.withanalysis.MyElasticsearchAnalysisConfigurer"/> <!--6-->
value="org.hibernate.search.documentation.gettingstarted.withhsearch.withanalysis.MyElasticsearchAnalysisConfigurer"/> <!--7-->
<!-- end::gettingstarted-configuration-orm_elasticsearch-analysis[] -->
<property name="hibernate.search.default_backend"
value="myBackend"/>
Expand Down

0 comments on commit b6b1914

Please sign in to comment.