Use the demo of springboot+elasticserach7 to compare the similarities and differences between sider and Baidu ai

Compare aigc engine: sider chatgpt3.5 and Baidu ai

Prompt words:

springboot2.5, connect to the demo of elasticsearch7, there must be basic operations, use the template method

The following is a sample code for connecting to Elasticsearch 7 using Spring Boot 2.5, including basic operation methods:

First, you need to add Elasticsearch dependencies in the pom.xml file:

 
 
xml<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.15.0</version>
</dependency>

Then, create a Spring Boot application and add the following configuration:

 
 
java@Configuration
public class ElasticsearchConfig {

@Value("${elasticsearch.host}")
private String host;

@Value("${elasticsearch.port}")
private int port;

@Value("${elasticsearch.username}")
private String username;

@Value("${elasticsearch.password}")
private String password;

@Bean
public RestHighLevelClient client() {
return new RestHighLevelClient(
RestClient.builder(new HttpHost(host, port, "http"), new HttpHost(host, port, "http"))
.setHttpClientConfigCallback(httpClientBuilder -> {
httpClientBuilder.setDefaultCredentialsProvider(CredentialsProviderBuilder.create()
.setUsername(username)
.setPassword(password)
.build());
return httpClientBuilder;
}));
}
}

Next, create an ElasticsearchRepository interface that defines basic Elasticsearch operation methods:

 
 
javapublic interface ElasticsearchRepository {

@Query("{\"match\": {\"${idField}\": \"?0\"}}")
Optional<Object> findById(String id);

default void index(Object document) {
IndexRequest request = new IndexRequest(document);
client().index(request, RequestOptions.DEFAULT);
}

default void deleteById(String id) {
DeleteRequest request = new DeleteRequest(id);
client().delete(request, RequestOptions.DEFAULT);
}
}

Among them, ${idField}is a placeholder for matching the document ID, which needs to be replaced according to the specific situation. In the example, we use Optionalto process the query results so that a null value is returned if the document cannot be found. index()The and deleteById()methods are used to index a document into Elasticsearch and delete a document with a specified ID, respectively. These methods are default methods, so they can be called directly in the class.

Finally, we need to create a concrete implementation class that implements the ElasticsearchRepository interface, for example:

 
 
java@Service
public class ElasticsearchService implements ElasticsearchRepository {

@Override
public Optional<Object> findById(String id) {
return client().get(new GetRequest(id), new AbstractResponseEntityWrapper<Object>() {
@Override
protected Object bodyToEntity(Response response) throws IOException {
return response.getEntity();
}
}).optional();
}
}

Compare to sider:

The following is a sample program that uses Spring Boot 2.5 to connect to Elasticsearch 7, including basic functions, using the template method:

First, make sure you have the following dependencies added to your project:

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

Then, create an Elasticsearch configuration class to configure Elasticsearch connection information:

@Configuration public class ElasticsearchConfig { @Value("${elasticsearch.host}") private String host; @Value("${elasticsearch.port}") private int port; @Bean public RestHighLevelClient elasticsearchClient() { RestClientBuilder builder = RestClient .builder(new HttpHost(host, port, "http")); RestHighLevelClient client = new RestHighLevelClient(builder); return client; } @Bean public ElasticsearchOperations elasticsearchTemplate() { return new ElasticsearchRestTemplate(elasticsearchClient()); } }
Next , create a data entity class for mapping documents in Elasticsearch:

@Document(indexName = "my_index", type = "my_type") public class MyEntity { @Id private String id; private String name; // getters and setters } Then,
create a data access interface inherited from ElasticsearchRepository for operation Documentation in Elasticsearch:

public interface MyEntityRepository extends ElasticsearchRepository<MyEntity, String> { List<MyEntity> findByName(String name); }
Finally, create a Spring Boot application class and write some simple sample code:

@SpringBootApplication public class DemoApplication implements CommandLineRunner { @Autowired private MyEntityRepository myEntityRepository; public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Override public void run(String... args) throws Exception { // Create a document MyEntity entity = new MyEntity(); entity.setId("1"); entity.setName("John"); myEntityRepository.save(entity); // Query documents by name List<MyEntity> entities = myEntityRepository.findByName("John"); for (MyEntity e : entities) { System.out.println(e.getId() + ": " + e.getName()); } } }
in application.properties file Configure Elasticsearch connection information in:

elasticsearch.host=localhost elasticsearch.port=9200
In this way, a simple sample program using Spring Boot to connect to Elasticsearch is completed. You can further extend and customize it according to your needs.

 ----------------------------

Analysis: The codes generated by the two are not much different in essence. There will be new results after each refresh, and sometimes the results will be similar. The codes written in a fixed format are actually similar.

Guess you like

Origin blog.csdn.net/jiao_zg/article/details/132642180