there I am Aditya. Currently making backend for ecommerce website as personal project. While making it I came to know about elasticsearch which seems to use some AI logic and helps to do fuzzy search. So i decided to use it for my products table. but the issue in its 8.+ version they have implemented https for security of elasticsearch api and the tutorials are out of date. But after some research I found how to connect springboot to elasticsearch using ssl contexts and Now all the basic functions are working for ElasticsearchRepository but the issue arise when I try to use custom queries.
here’s my product table:
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Document(indexName = "products")
public class Product {
@Id
private String id;
private int userId;
private String name;
private String description;
private double price;
private int stock;
private int categoryId;
private String image1;
private String image2;
private String image3;
private String image4;
private String image5;
private String image6;
private String image7;
private String image8;
private String image9;
}
My ElasticsearchRepository:
@Repository
public interface ProductESRepository extends ElasticsearchRepository<Product, String> {
List<Product> findByName(String name);
Product findByUserIdAndName(int userId, String name);
List<Product> findByUserId(int userId);
List<Product> search(Query query, Class<Product> class1);
}
My ElasticSearchClient:
@Configuration
public class ESClient {
@Bean
public ElasticsearchClient getElasticsearchClient() {
// Create a RestClientBuilder with your Elasticsearch host and port.
RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "https"));
// Set up custom HttpClientConfigCallback to load SSL certificate and
// truststore.
RestClientBuilder.HttpClientConfigCallback httpClientConfigCallback = new HttpClientConfigImpl();
builder.setHttpClientConfigCallback(httpClientConfigCallback);
// Create a ElasticsearchClient with the RestClient.
RestClient restClient = builder.build();
// Create a ElasticsearchClientTransport with the RestClient and
// JacksonJsonpMapper.
RestClientTransport restClientTransport = new RestClientTransport(restClient, new JacksonJsonpMapper());
// Create and return the ElasticsearchClient.
return new ElasticsearchClient(restClientTransport);
}
}
and finaly my custom HttpAsyncClientBuilder:
type here@Configuration
public class HttpClientConfigImpl implements HttpClientConfigCallback {
// to establish connection between elasticsearch instance
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) {
try {
// create credentials
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials("elastic",
"nothing");
credentialsProvider.setCredentials(AuthScope.ANY, usernamePasswordCredentials);
httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
// import truststore file
String trustLocationStore = "/home/all_father/elasticsearch-8.14.1/config/certs/truststore.p12";
File trustLocationFile = new File(trustLocationStore);
// load the truststore
SSLContextBuilder sslContextBuilder = SSLContexts.custom().loadTrustMaterial(trustLocationFile,
"nothing".toCharArray());
// provide the ssl context to the client builder
SSLContext sslContext = sslContextBuilder.build();
httpAsyncClientBuilder.setSSLContext(sslContext);
} catch (Exception e) {
System.out.println(e.getMessage());
}
return httpAsyncClientBuilder;
}
}
This is proper working code But the error**[search method not found for products]** arises when i try to do custom query. I have tried every single way(StringQuery, @Query, Query DSL, etc) of implementing custom query but none of the way is working.
Aditya Kumawat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.