# Autocomplete with Hibernate Search 6

**URL:** <https://discourse.hibernate.org/t/autocomplete-with-hibernate-search-6/5006>\
**Category:** Hibernate Search\
**Created:** [February 2, 2021, 8:38am UTC](https://discourse.hibernate.org/t/autocomplete-with-hibernate-search-6/5006 "2021-02-02T08:38:27Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![yrodiere](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/yrodiere/32/1760_2.png) [@yrodiere](https://discourse.hibernate.org/u/yrodiere)\
**Post date:** [February 2, 2021, 11:33am UTC](https://discourse.hibernate.org/t/autocomplete-with-hibernate-search-6/5006/4 "2021-02-02T11:33:49Z")

</div>

Okay then… this may work for small indices, but it will perform rather badly on large ones.

In general, I’d recommend a different approach involving a query. The main difference is that it doesn’t suggest terms, but entities. So if you type “jo” you won’t get “John” as a suggestion, but the entity representing the person “John Smith”. Then you can decide to display that entity as you see fit.

If you’re interested by this approach, read on.

If you really want to use `Terms` to collect indexed terms with Hibernate Search 6, you will have to wait until [HSEARCH-4065](https://hibernate.atlassian.net/browse/HSEARCH-4065) gets fixed (or come and discuss [on Zulip](https://hibernate.zulipchat.com/#narrow/stream/132092-hibernate-search-dev) how to contribute a patch to fix it 😉 ).

Alternatively, you can also switch to Elasticsearch, where you can use suggesters, which I covered briefly [here](https://stackoverflow.com/a/65183114/6692043).

Now, to implement autocomplete with queries:

1. Declare an appropriate analyzer in your [analysis configurer](https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#backend-lucene-analysis-analyzers):

```auto
public class MyAnalysisConfigurer implements LuceneAnalysisConfigurer {
	@Override
	public void configure(LuceneAnalysisConfigurationContext context) {
		context.analyzer( "autocomplete-indexing" ).custom()
				.tokenizer( StandardTokenizerFactory.class )
				.tokenFilter( LowerCaseFilterFactory.class )
				.tokenFilter( SnowballPorterFilterFactory.class )
						.param( "language", "English" )
				.tokenFilter( ASCIIFoldingFilterFactory.class )
				.tokenFilter( EdgeNGramFilterFactory.class )
						.param( "minGramSize", "3" )
						.param( "maxGramSize", "7" );

		// Same as "autocomplete-indexing", but without the edge-ngram filter
		context.analyzer( "autocomplete-query" ).custom()
				.tokenizer( StandardTokenizerFactory.class )
				.tokenFilter( LowerCaseFilterFactory.class )
				.tokenFilter( SnowballPorterFilterFactory.class )
						.param( "language", "English" )
				.tokenFilter( ASCIIFoldingFilterFactory.class );
	}
}

```

1. Declare full-text fields with the appropriate analyzers:

```auto
@Indexed
public class MyEntity {

    @FullTextField(name = "firstEntityField_autocomplete,
            analyzer = "autocomplete-indexing", searchAnalyzer = "autocomplete-query")
    private String firstEntityField;

    @FullTextField(name = "secondEntityField_autocomplete,
            analyzer = "autocomplete-indexing", searchAnalyzer = "autocomplete-query")
    private String secondEntityField;

    @FullTextField(name = "thirdEntityField_autocomplete,
            analyzer = "autocomplete-indexing", searchAnalyzer = "autocomplete-query")
    private String thirdEntityField;

    // ... getters, setters ...
}

```

1. [Reindex](https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#mapper-orm-indexing-massindexer) your data
2. Query like this to retrieve only entities that match the given terms:

```java
String terms = ...; // User input
List<MyEntity> hits = Search.session( entityManager )
		.search( MyEntity.class )
		.where( f -> f.simpleQueryString()
				.fields( "firstEntityField_autocomplete",
						"secondEntityField_autocomplete",
						"thirdEntityField_autocomplete" )
				.matching( terms )
				.defaultOperator( BooleanOperator.AND ) )
		.fetchHits( 20 );

```

---

_[View the full topic](https://discourse.hibernate.org/t/autocomplete-with-hibernate-search-6/5006)._
