Elasticsearch is a search tool that can also analyze data as well, And it is free to use by everyone. Developers appreciated it as it impressively handles many searches at once, therefore assisting in increasing the speed of their applications and making them work more efficiently.
Now, when you mix Elasticsearch with Spring Boot, which is another tool that helps build big, serious apps easily, things get even better. Your designed app can quickly scroll through a tremendous amount of data, accomplish a better understanding of data, and flourish without any nerve-wracking moments. This guide is for all of those who would like to use the capabilities of Elasticsearch to make their Spring Boot app more intelligent. We will do it step by step from the very beginning to the very end to show you how you can assemble it to get it going.
You can either enhance your app or come up with a completely new one in which case we will provide you with all the resources you need to ensure the best results and get your app to the top of Elasticsearch thus making sure that your app excels.
What is Elasticsearch?
Elasticsearch is a real-time distributed and open-source full-text search and analytics engine. It is used in Single Page Application (SPA) projects. Elasticsearch is an open-source developed in Java and used by many big organizations around the world.
Recommendation: Explore Elasticsearch and Why It’s Worth Using
Benefits of Using Elasticsearch with Spring Boot
#1. Imagine a library where you have every book instantly, as they can be sorted and found in any type of configuration. The application function becomes a lot better when you use the Elasticsearch tool. The search result of lots of information, even if you have tons of data, is fast with it.
#2. As your app gains more users as it gets more popular, the need to expand its storage capacity becomes more apparent because large data will cause your app to slow down. Elasticsearch together with Spring Boot in case combination is a good selection. They help you to increase the size of your app smoothly with no difficulties handling all the data.
#3. Elasticsearch doesn’t just seek to locate information; it magnificently handles the tasks of the analysis. Understanding your data then becomes easier. You can spot trends, for example, and find out exactly what your users prefer most. To make the right choice, that is to make a smart decision for your app, this kind of data is crucial.
#4. Application web development in Spring Boot is ideal since it means that developers set up and bootstrap things quickly and effortlessly. Employing it in conjunction with Elasticsearch enables you to start building the saucy sections of your app, without hogging your time with the nitty-gritty of setting it up.
#5. Searches look up quickly and provide matching results that stick your app to the user’s mind. People make decisions instantly when you provide what they’re exactly looking for. This momentary action will make them stick to your app more.
#6. Elasticsearch gets fresh and relevant data, and updates search results as things change. This is the perfect fit for apps that consume the most recent or most up-to-date information, especially for apps like news apps or social media.
Read | Spring Boot with Microsoft Azure Integration
Key Components
Documents: These are the basic units of information stored in Elasticsearch. Documents are indexed in JSON format and can contain various types of data like numbers, strings, and dates. Each document has a unique ID and a specific data type.
Indices: Collections of documents with similar characteristics are grouped into indices. Think of indices as databases in relational databases where related documents are stored together. Each index has a unique name and can be queried against.
Inverted Index: An index in Elasticsearch is also referred to as an inverted index. This data structure is fundamental to search engines and maps content like words or numbers to their locations in documents.
Shards: Shards are individual Lucene indexes that make up Elasticsearch’s scalability. They allow for distributing data across nodes to enhance performance and prevent issues like index size limitations.
Nodes: Nodes are essential components of an Elasticsearch cluster. Different types of nodes exist, such as data nodes for storing data, master nodes for cluster management, client nodes for handling requests, and more. Nodes are assigned specific roles within the cluster.
Cluster: An Elasticsearch cluster comprises one or more nodes and has a unique identifier. Clusters are formed by nodes joining together and are crucial for the distributed nature of Elasticsearch.
Primary Use Cases
Application search: For applications that access, retrieve, and report data mainly via a search platform.
Web Search: Elasticsearch serves as a powerful tool for accurate and effective searches on websites that have massive content repositories. It comes as no surprise that Elasticsearch is becoming more and more popular in the site search industry.
Business exploration: Enterprise-wide search is made possible by Elasticsearch and can contain every type of search you can imagine, including document, blog, people, and E-commerce product searches. It has gradually supplanted the search functions on most of the well-known websites that we visit regularly. From an enterprise-specific standpoint, corporate intranets make extensive use of Elasticsearch.
Logging and log analytics: Elasticsearch is a prominent tool for scalable, near-real-time ingesting and analyzing log data. To motivate activities, it additionally provides substantial operational information on log metrics.
Monitoring of containers and infrastructure metrics: A lot of companies utilize the ELK stack for analyzing various metrics. This could entail collecting information on several performance metrics that change depending on the use case.
Security analytics: Elasticsearch is also an effective analytics application for security analysis. The ELK stack may be utilized to analyze access logs and other logs relevant to system security, offering you a broader understanding of what’s going on in real time across all your systems.
Setting Up Elasticsearch with Spring Boot
Step 1: Include Required Dependencies
To start, you need to include the necessary dependencies in your Spring Boot project. Add the following dependencies to your pom.xml file:
<dependency>
      <groupId>co.elastic.clients</groupId>
     <artifactId>elasticsearch-java</artifactId>
</dependency>
<dependency>
     <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>
Step 2: Configure Elasticsearch Connection with Java Configuration Class
Establishes a connection to the Elasticsearch cluster using the RestClient builder.
Configures security (HTTPS) with hostname, port, and credentials (username and password).
Creates RestClient, ElasticsearchTransport, and finally the ElasticsearchClient bean for interaction.
@Configuration
public class ElasticsearchConfig {
  private static String HTTP_CA_FINGERPRINT = “<<HTTP_CA_FINGERPRINT>>â€;
  @Bean
  public RestClient restClient() {
    SSLContext sslContext = TransportUtils.sslContextFromCaFingerprint(HTTP_CA_FINGERPRINT);
    BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(“<<USERNAME>>â€, “<<PASSWORD>>â€));
    return RestClient.builder(new HttpHost(“<<HOSTNAME>>â€, 9200, “httpsâ€))
        .setHttpClientConfigCallback(
            hc -> hc
                .setSSLContext(sslContext)
                .setDefaultCredentialsProvider(credentialsProvider)
        ).build();
  }
  @Bean
  public ElasticsearchTransport elasticsearchTransport() {
    return new RestClientTransport(restClient(), new JacksonJsonpMapper());
  }
  @Bean
  public ElasticsearchClient elasticsearchClient() {
    return new ElasticsearchClient(elasticsearchTransport());
  }
}
Note: You will get http_ca_fingerprint, username, and password when you start the Elasticsearch service for the first time.
The default username is elastic.
If somehow you forgot the password, then you can reset the password by using the following command:
bin/elasticsearch-reset-password -u elastic –url https://localhost:9200
You can get http_ca_fingerprint using the following command:
openssl x509 -in config/certs/http_ca.crt -noout -fingerprint | sed ‘s/://g’
Step 3: Create One Service Class to Create Index, Ingest and Search Document Using Elasticsearch Client
Simple Product class:
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class Product {
  private String name;
  private String description;
  private Double price;
  private List<String> category;
}
Service class: inject ElasticsearchClient dependency
Provides methods for various operations:
createIndex: Creates an Elasticsearch index with the provided name.
ingestDocument: Inserts a document into a specific index with a unique ID.
getProductById: Retrieves a document from an index by its ID.
searchDocuments: Searches for documents in an index based on a field and search term.
getAllDocuments: Retrieves all documents from a specific index.
@Service
@RequiredArgsConstructor
public class ElasticsearchService {
 private final ElasticsearchClient elasticsearchClient;
  public String createIndex(String indexName) throws IOException {
    return elasticsearchClient.indices()
        .create(c -> c.index(indexName)).index();
  }
  public Result ingestDocument(String indexName, String id, Product product) throws IOException {
    IndexRequest<Object> indexRequest = IndexRequest.of(
        request -> request
            .index(indexName)
            .id(id)
            .document(product)
    );
    return elasticsearchClient.index(indexRequest).result();
  }
  public Object getProductById(String indexName, String id) throws IOException {
    GetResponse<Product> response = elasticsearchClient.get(
        g -> g.index(indexName).id(id),
        Product.class
    );
    if (response.found()) {
      return response.source();
    } else {
      return “Product Not Foundâ€;
    }
  }
  public List<Product> searchDocuments(String indexName, String fieldName, String text) throws IOException {
    List<Product> responseList = new ArrayList<>();
    SearchResponse<Product> searchResponse = elasticsearchClient.search(s -> s
            .index(indexName)
            .query(q -> q
                .term(t -> t
                    .field(fieldName)
                    .value(v -> v.stringValue(text))
                )),
        Product.class);
    for (Hit<Product> hit : searchResponse.hits().hits()) {
      responseList.add(hit.source());
    }
    return responseList;
  }
  public List<Product> getAllDocuments(String indexName) throws IOException {
    List<Product> responseList = new ArrayList<>();
    SearchResponse<Product> searchResponse = elasticsearchClient.search(s -> s
        .index(indexName).query(q -> q.matchAll(MatchAllQuery.of(m -> m.queryName(“match_allâ€)))), Product.class);
    for (Hit<Product> hit : searchResponse.hits().hits()) {
      responseList.add(hit.source());
    }
    return responseList;
  }
}
Step 4: Create One Simple Rest Controller and Inject ElasticsearchService Dependency
@RestController
@RequestMapping(“/appâ€)
@RequiredArgsConstructor
public class SampleController {
  private final ElasticsearchService elasticsearchService;
  @PostMapping(“/index/create/{indexName}â€)
  public String createIndex(@PathVariable String indexName) throws IOException {
    return elasticsearchService.createIndex(indexName);
  }
  @PostMapping(“/document/ingest/{indexName}/{id}â€)
  public Result ingestDocument(@PathVariable String indexName, @PathVariable String id, @RequestBody Product product) throws IOException {
    return elasticsearchService.ingestDocument(indexName, id, product);
  }
  @GetMapping(“/document/get-by-id/{indexName}/{id}â€)
  public Object getProductById(@PathVariable String indexName, @PathVariable String id) throws IOException {
    return elasticsearchService.getProductById(indexName, id);
  }
  @GetMapping(“/documents/find/{indexName}/{fieldName}/{text}â€)
  public List<Product> searchByName(@PathVariable String indexName, @PathVariable String fieldName, @PathVariable String text) throws IOException {
    return elasticsearchService.searchDocuments(indexName, fieldName, text);
  }
  @GetMapping(“/documents/{indexName}â€)
  public List<Product> getAllDocuments(@PathVariable String indexName) throws IOException {
    return elasticsearchService.getAllDocuments(indexName);
  }
}
Start elasticsearch service before running the project:
Run the following command:
./bin/elasticsearch
GitHub Reference URL:
https://github.com/jay-rudani/elasticsearch-spring-boot
The post Elasticsearch Integration with Spring Boot: A Developer’s Guide appeared first on Inexture.
Source: Read MoreÂ