Building Semantic Search with Elasticsearch using Spring Boot

Abhishek Ranjan
5 min readApr 19, 2023

--

Introduction

Semantic search is an advanced search technique that focuses on understanding the intent and context behind a query, rather than just matching keywords. It aims to provide more accurate and relevant results by considering the relationships between words and phrases. Elasticsearch, a powerful, scalable, and real-time search and analytics engine, can be used to build an effective semantic search system.

In this article, we will walk you through the process of building a semantic search system using Elasticsearch, Java, and Spring Boot, complete with code samples and Mermaid diagrams to illustrate key concepts.

Prerequisites

  • Familiarity with Elasticsearch and its Query DSL (Domain Specific Language)
  • Basic knowledge of Java programming and Spring Boot
  • Elasticsearch and Kibana installed on your local machine

Overview

  1. Preprocessing and Indexing Documents
  2. Keyword-Based Search vs. Semantic Search
  3. Implementing Semantic Search
  4. Evaluating and Optimizing Search Relevance

1. Preprocessing and Indexing Documents

The first step in building a semantic search system is preprocessing and indexing documents. For this, we will create a simple Spring Boot application that interacts with Elasticsearch.

Setting up Spring Boot Application

Create a new Spring Boot project with the following dependencies:

  • Spring Web
  • Elasticsearch

Add the following dependency to your pom.xml file:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

Configuring Elasticsearch

Create a @Configuration class and configure Elasticsearch by providing the necessary settings:

@Configuration
public class ElasticsearchConfig extends AbstractElasticsearchConfiguration {
@Value("${elasticsearch.host}")
private String host;
@Value("${elasticsearch.port}")
private int port;
@Override
@Bean
public RestHighLevelClient elasticsearchClient() {
return new RestHighLevelClient(
RestClient.builder(new HttpHost(host, port, "http")));
}
}

Indexing Documents

Create a simple document class and annotate it with @Document:

@Document(indexName = "documents")
public class Document {
@Id
private String id;
private String title;
private String content;
}

Create a DocumentRepository interface that extends ElasticsearchRepository:

public interface DocumentRepository extends ElasticsearchRepository<Document, String> {
}

Implement a service to index documents:

@Service
public class DocumentService {
private final DocumentRepository documentRepository;
public DocumentService(DocumentRepository documentRepository) {
this.documentRepository = documentRepository;
}
public void indexDocuments(List<Document> documents) {
documentRepository.saveAll(documents);
}
}

Now, you can use the DocumentService to index documents into Elasticsearch.

2. Keyword-Based Search vs. Semantic Search

Keyword-based search matches documents based on the occurrence of specific words or phrases. It is simple to implement but may not provide the most relevant results. On the other hand, semantic search takes the meaning and context of words into account, providing more accurate and relevant results.

3. Implementing Semantic Search

To implement semantic search, we will use Elasticsearch’s built-in features, such as synonyms, text analysis, and more.

3.1 Synonyms

Create a synonym analyzer in your Elasticsearch index settings:

{
"settings": {
"analysis": {
"filter": {
"synonym_filter": {
"type": "synonym",
"synonyms_path": "analysis/synonym.txt"
}
},
"analyzer": {
"synonym_analyzer": {
"tokenizer": "standard",
"filter": [
"lowercase",
"synonym_filter"
]
}
}
}
}
}

The synonyms_path should point to a file containing the synonyms for your domain. Each line in the file represents a group of synonyms separated by commas.

3.2 Text Analysis

Next, update the mapping of your index to utilize the synonym analyzer:

{
"mappings": {
"properties": {
"title": {
"type": "text",
"analyzer": "synonym_analyzer"
},
"content": {
"type": "text",
"analyzer": "synonym_analyzer"
}
}
}
}

This will ensure that Elasticsearch uses the synonym analyzer while indexing and searching documents.

3.3 Query Expansion

To expand queries to cover synonyms, you can use the match query with the synonym_analyzer. Create a search method in your DocumentService class:

public List<Document> search(String query) {
MatchQueryBuilder matchQuery = QueryBuilders.matchQuery("content", query)
.analyzer("synonym_analyzer");

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(matchQuery);

SearchRequest searchRequest = new SearchRequest("documents");
searchRequest.source(searchSourceBuilder);

try {
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
return Arrays.stream(searchResponse.getHits().getHits())
.map(hit -> objectMapper.convertValue(hit.getSourceAsMap(), Document.class))
.collect(Collectors.toList());
} catch (IOException e) {
throw new RuntimeException("Failed to execute search", e);
}
}

4. Evaluating and Optimizing Search Relevance

To evaluate the search relevance of your semantic search, you can use Elasticsearch's built-in scoring mechanism, the _score field. You can also customize the scoring using function score queries or other advanced techniques. Continuously monitoring and evaluating search results will help you fine-tune the search experience.

Performance and Resource Implications of Semantic Search on Elasticsearch

Implementing semantic search using Elasticsearch can have some performance and resource implications. It is essential to be aware of these implications and carefully consider them when planning and optimizing your search system.

  1. Increased indexing time: Utilizing a synonym analyzer during indexing increases the time it takes to index documents. The complexity of the synonym list and the size of the documents being indexed can significantly impact the indexing performance. To mitigate this, consider using a smaller synonym list and optimizing your indexing process.
  2. Increased index size: Using synonyms can lead to a larger index size, as multiple terms representing the same concept will be stored in the index. This may lead to higher storage and memory usage. To optimize storage, consider using a more selective synonym list or exploring index compression options.
  3. Increased query time: Searching with a synonym analyzer can increase the query time, especially for complex synonym lists and large datasets. To improve query performance, you can use caching strategies or optimize your synonym list.
  4. Higher memory and CPU usage: Synonym processing requires additional memory and CPU resources during both indexing and querying. This can lead to increased overall resource consumption. To optimize resource usage, monitor your Elasticsearch cluster and adjust the hardware and configuration settings accordingly.
  5. Relevance tuning: Implementing semantic search can sometimes complicate the process of tuning search relevance. The use of synonyms and other text analysis techniques can affect the scoring of documents. Continuously evaluating and optimizing search relevance is crucial for maintaining a user-friendly search experience.

Hence, while semantic search can provide more accurate and relevant search results, it can also impact Elasticsearch’s performance and resource usage. Careful planning, monitoring, and optimization can help you balance the benefits of semantic search with the performance and resource requirements of your Elasticsearch cluster.

Conclusion

In this article, we've covered the process of building a semantic search system using Elasticsearch, Java, and Spring Boot. We have discussed preprocessing and indexing documents, the difference between keyword-based and semantic search, and implementing semantic search using synonyms, text analysis, and query expansion. By evaluating and optimizing search relevance, you can create a powerful and user-friendly search experience for your users.

For more advanced techniques, consider exploring Elasticsearch features like the phrase and span queries, as well as using machine learning models to enhance search results.

--

--