File tree Expand file tree Collapse file tree 2 files changed +8
-6
lines changed
spring-elastic/src/main/java/com/example/spring_elastic Expand file tree Collapse file tree 2 files changed +8
-6
lines changed Original file line number Diff line number Diff line change 1010import co .elastic .clients .transport .ElasticsearchTransport ;
1111import co .elastic .clients .transport .rest_client .RestClientTransport ;
1212import org .springframework .beans .factory .annotation .Value ;
13+ import org .springframework .stereotype .Component ;
1314
15+ @ Component
1416public class BookElasticsearchClient {
1517 private ElasticsearchClient client ;
1618
1719 @ Value ("${spring.elasticsearch.port}" )
18- private Integer port ;
20+ private String port ;
1921
20- @ Value ("${spring.elasticsearch.port }" )
22+ @ Value ("${spring.elasticsearch.url }" )
2123 private String url ;
2224
2325 public ElasticsearchClient getClient ()
@@ -31,7 +33,7 @@ public ElasticsearchClient getClient()
3133
3234 private void initClient ()
3335 {
34- RestClient restClient = RestClient .builder (new HttpHost ("localhost" , 9200 )).build ();
36+ RestClient restClient = RestClient .builder (new HttpHost (url , Integer . parseInt ( port ) )).build ();
3537
3638 ElasticsearchTransport transport = new RestClientTransport (restClient , new JacksonJsonpMapper ());
3739
Original file line number Diff line number Diff line change 66
77import com .example .spring_elastic .client .BookElasticsearchClient ;
88import com .example .spring_elastic .models .Book ;
9+ import org .springframework .beans .factory .annotation .Autowired ;
910import org .springframework .context .annotation .Configuration ;
1011
1112
1213import co .elastic .clients .elasticsearch ._types .ElasticsearchException ;
13- import co .elastic .clients .elasticsearch .core .BulkRequest ;
1414import co .elastic .clients .elasticsearch .core .SearchResponse ;
1515import co .elastic .clients .elasticsearch .core .search .Hit ;
1616
1717@ Configuration
1818public class BookElasticsearchService {
19- BookElasticsearchClient client = new BookElasticsearchClient ();
20-
19+ @ Autowired
20+ private BookElasticsearchClient client ;
2121 private static final String BOOK_INDEX = "books" ;
2222
2323 public Book createBook (Book book ) {
You can’t perform that action at this time.
0 commit comments