1. 人民网项目搭建
由于elasticsearch版本多,又有多个Java客户端,首次搭建时,没有明确使用哪个客户端,由此带来许多问题。
客户端发展史:Node client(2.3弃用) —> Transport client(7.0弃用) —>Jest client —> Rest client(low/hign)。前两种使用es原生传输 协议和集群交互,JestClient兼容性优于前两者,不用考虑Es集群使用es版本问题,low REST客户端,通过http与集群交互,用户需自己编组请求JSON串,及解析响应JSON串。兼容所有ES版本。hign REST基于low REST增加了编组请求JSON串、解析响应JSON串等相关api。使用的版本需要保持和ES服务端的版本一致,否则会有版本问题。设置了默认调优参数,若版本匹配,其性能会更加优秀。
最初配置
项目初始化为2.3.12.RELEASE,后来可能误操作修改了spring boot版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<version>2.6.1</version>
</dependency>
{
"name" : "elacticsearch-ess-esn-3-1",
"cluster_name" : "elacticsearch",
"cluster_uuid" : "aaVcd-QVToKa7h5HP7NCNw",
"version" : {
"number" : "7.9.3",
"build_flavor" : "oss",
"build_type" : "tar",
"build_hash" : "c4138e51121ef06a6404866cddc601906fe5c868",
"build_date" : "2020-10-16T10:36:16.141335Z",
"build_snapshot" : false,
"lucene_version" : "8.6.2",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
修改配置
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
点进data-es可以看到
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.3.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>4.0.2.RELEASE</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>transport</artifactId>
<groupId>org.elasticsearch.client</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
配置文件,此处ip和端口可修改为通过yml获取
@Component
public class EsClientConfig {//extends AbstractElasticsearchConfiguration
/*@Override
@Bean
public RestHighLevelClient elasticsearchClient() {
final ClientConfiguration clientConfiguration = ClientConfiguration.builder().connectedTo("139.198.17.162:9200").build();
return RestClients.create(clientConfiguration).rest();
}*/
public static final RequestOptions COMMON_OPTIONS;
static {
RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
COMMON_OPTIONS = builder.build();
}
@Bean
public RestHighLevelClient esRestClient() {
RestClientBuilder builder = null;
// 可以指定多个es
builder = RestClient.builder(new HttpHost("192.168.0.60", 9200, "http"));
RestHighLevelClient client = new RestHighLevelClient(builder);
return client;
}
}
实体类引入,到此完成
@Qualifier("esRestClient")
@Autowired
private RestHighLevelClient highLevelClient;
2. should must联用导致的should失效问题
工作中使用es查询,本意是想匹配平台id,并且多项商品id匹配一项,原dsl语句
GET app_openapi_youqian_goodsinfo_*/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"platform_id": {
"value": "1001",
"boost": 1
}
}
}
],
"should": [
{
"term": {
"goods_id.keyword": {
"value": "646484693204",
"boost": 1
}
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
}
}
使用嵌套语法
{
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{"term": {"sex": "男"}},
{"term": {"score": "70"}}
]
}
},
{
"bool": {
"must": [
{"term": {"sex": "男"}},
{"term": {"score": "80"}}
]
}
}
]
}
}
}
或者
{
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{"term": {"sex": "男"}},
{"term": {"score": "70"}}
]
}
},
{
"bool": {
"must": [
{"term": {"sex": "男"}},
{"term": {"score": "80"}}
]
}
}
]
}
}
}
使用”minimum_should_match”: 1
GET app_openapi_youqian_goodsinfo_*/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"platform_id": {
"value": "1001",
"boost": 1
}
}
}
],
"should": [
{
"term": {
"goods_id.keyword": {
"value": "646484693204",
"boost": 1
}
}
}
],
"minimum_should_match": 1,
"adjust_pure_negative": true,
"boost": 1
}
}
}