how to use custom query in springboot (elasticsearch) [search method not found error]

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.

New contributor

Aditya Kumawat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật