spring-boot - 无法从 ElasticSearch 获取数据
问题描述
拥有一个运行在 6.8.3 版本的简单 ES 本地集群。问题是我能够通过我的 SpringBoot 服务器插入数据但无法获取它。它的投掷错误
Suppressed: org.elasticsearch.client.ResponseException: method [POST], host [http://localhost:9200], URI [/customer/_search?typed_keys=true&max_concurrent_shard_requests=5&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true&ignore_throttled=true&search_type=dfs_query_then_fetch&batched_reduce_size=512&ccs_minimize_roundtrips=true], status line [HTTP/1.1 500 Internal Server Error]
{"error":{"root_cause":[{"type":"json_parse_exception","reason":"Current token (VALUE_NUMBER_INT) not of boolean type\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@15e6a969; line: 1, column: 100]"}],"type":"json_parse_exception","reason":"Current token (VALUE_NUMBER_INT) not of boolean type\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@15e6a969; line: 1, column: 100]"},"status":500}
at org.elasticsearch.client.RestClient.convertResponse(RestClient.java:302) ~[elasticsearch-rest-client-7.9.3.jar:7.9.3]
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:272) ~[elasticsearch-rest-client-7.9.3.jar:7.9.3]
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:246) ~[elasticsearch-rest-client-7.9.3.jar:7.9.3]
at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1613) ~[elasticsearch-rest-high-level-client-7.9.3.jar:7.9.3]
... 92 common frames omitted
我有一个非常简单的服务器,
客户对象
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName = "customer")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Customer {
@Id
private String id;
private String firstName;
private String lastName;
private String age;
}
一个简单的控制器
@RestController
public class CustomerController {
@Autowired
private CustomerDao customerDao;
@PostMapping("/saveCustomer")
public Customer saveCustomer(@RequestBody Customer customer){
return customerDao.save(customer);
}
@GetMapping("/findAll")
public Iterable<Customer> findAllCustomer(){
return customerDao.findAll();
}
@GetMapping("/findByName/{firstName}")
public List<Customer> findByFirstName(@PathVariable String firstName){
return customerDao.findByFirstName(firstName);
}
}
这是我的 POM 依赖项
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.learn</groupId>
<artifactId>elastic</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>elastic</name>
<description>Learn ElasticSearch</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-elasticsearch -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<--PLus some other -->
无法理解为什么保存有效但搜索无效。
解决方案
我想问题出在你的道级别上,我提供了两种方法。
- 弹性搜索存储库。
使用您的模型(索引)名称的参数创建一个弹性搜索存储库,然后添加然后自动怪异您的存储库并使用它预先编写的查询,如 findAll 并根据需要使用它们如何在String的帮助下创建自己的查询或条件,本机或Criteriabase查询。
@Repository
public interface YouRepositoryName extends ElasticsearchRepository<YOURMODELNAME, String> {}
@Autowired
YourRepository repositoryEntity
- 基于 Elastic 存储库中的 Dsl
借助支持的查询方法创建您自己的查询。考虑以下示例,我们在 Binder 类中使用了一个方法,该方法使用字符串查询语言生成我们的查询脚本,然后我们将其传递到我们的存储库并执行查询。
@Component
public class Binder {
public String SearchCore(SequenceSearchDTO entity) {
String query="";
Double z=0.00;
Long x=z.longValue();
if(entity.getParentKnowledgeGroupId() != null && !entity.getParentKnowledgeGroupId().isEmpty() && !entity.getParentKnowledgeGroupId().equals("")){
query= "[{\"query_string\": { \"query\" : \""+entity.getParentKnowledgeGroupId()+"\", \"fields\" : [ \"parentKnowledgeGroupId\" ] }}," ;
}
if(entity.getKnowledgeGroupId() != null && !entity.getKnowledgeGroupId().isEmpty() && !entity.getKnowledgeGroupId().equals("")){
query=query+ "{\"query_string\" : { \"query\" : \""+entity.getKnowledgeGroupId()+"\", \"fields\" : [ \"knowledgeGroupId\" ]}},";
}
if(entity.getTerm() != null && !entity.getTerm().isEmpty() && !entity.getTerm().equals("")){
query=query+ "{\"query_string\" : { \"query\" : \""+entity.getTerm()+"\", \"fields\" : [ \"content\" ]}},";
}
if(entity.getPrice() != null && ! entity.getPrice().equals("")){
query=query+"{\"range\" : {\"price\" : {\"from\" :"+x+" , \"to\" : "+entity.getPrice()+", \"include_lower\" : true, \"include_upper\" : true } } },";
}
if(entity.getCourseType() != null && ! entity.getCourseType().equals("")){
query=query+ "{\"query_string\" : { \"query\" : \""+entity.getCourseType()+"\", \"fields\" : [ \"courseType\" ]}},";
}
if(entity.getScore() != null && ! entity.getScore().equals("")){
query=query+"{\"range\" : {\"scoree\" : {\"from\" : "+0+", \"to\" : "+entity.getScore()+", \"include_lower\" : true, \"include_upper\" : false } } },";
}
if(entity.getBeginDate() != null && ! entity.getBeginDate().equals("")){
if(entity.getEndDate() != null && ! entity.getEndDate().equals("")) {
query = query + "{\"range\" : {\"LogicalDate\" : {\"from\" : " + entity.getBeginDate().getTime() / 1000 + ", \"to\" : " + entity.getEndDate().getTime() / 1000 + ", \"include_lower\" : true, \"include_upper\" : true } } },";
}
else{
query=query+ "{\"range\" : {\"LogicalDate\" : {\"from\" : "+entity.getBeginDate().getTime()/1000+", \"to\" : "+new Date().getTime()/1000+", \"include_lower\" : true, \"include_upper\" : true } } },";
}
}
if(entity.getDuration() != null && ! entity.getDuration().equals("")){
query=query+"{\"range\" : {\"studyTime\" : {\"from\" :"+x+" , \"to\" : "+entity.getDuration()+", \"include_lower\" : true, \"include_upper\" : true } } },";
}
if(entity.getFree() != null){
query=query+ "{\"query_string\" : { \"query\" : \""+entity.getFree()+"\", \"fields\" : [ \"free\" ]}}]";
}
return query;
}
}
创建一个支持的查询方法,如下所示
@Repository
public interface SequenceRepository extends ElasticsearchRepository<ElasticModelSequence, String> {
@Query("{\"bool\":"+
"{\"must\" :"+
"?0"+
"}"+
"}")
List<ElasticModelSequence> stringQuery(@Param("flag") String flag);
}
终于在你的道级中使用它
List<ElasticModelSequence> flag=sequenceRepository.stringQuery(query);
推荐阅读
- jpeg - JPEG 编码。如何使用 Huffman 算法对 RLE(AC) 进行编码
- azure-devops - 扩大管道数量 AzureDevOps
- ruby-on-rails - 如何使用现有记录删除祖先
- grafana - 如何将 JavaScript(例如 Google Analytics)包含到 Grafana 中?
- websphere - IBM ACE - 覆盖基本 URL (RestRequestNod)
- android - Android Volley:为什么这两个请求中的第二个请求超时失败?
- c# - 诊断服务器端 Blazor 内存问题
- redis - Thumbor 不会处理整个图像。底部有一个灰色框
- tableau-api - Tableau Server 对 Snowflake 的自动请求
- linux - 如何使用 shell 脚本在大小为 8gb 的 Linux Ec2 服务器中进行磁盘分区?