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版本

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.6.1</version>
  5. <relativePath/> <!-- lookup parent from repository -->
  6. </parent>
  7. <dependency>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
  10. <version>2.6.1</version>
  11. </dependency>
  1. {
  2. "name" : "elacticsearch-ess-esn-3-1",
  3. "cluster_name" : "elacticsearch",
  4. "cluster_uuid" : "aaVcd-QVToKa7h5HP7NCNw",
  5. "version" : {
  6. "number" : "7.9.3",
  7. "build_flavor" : "oss",
  8. "build_type" : "tar",
  9. "build_hash" : "c4138e51121ef06a6404866cddc601906fe5c868",
  10. "build_date" : "2020-10-16T10:36:16.141335Z",
  11. "build_snapshot" : false,
  12. "lucene_version" : "8.6.2",
  13. "minimum_wire_compatibility_version" : "6.8.0",
  14. "minimum_index_compatibility_version" : "6.0.0-beta1"
  15. },
  16. "tagline" : "You Know, for Search"
  17. }

出现问题:无论如何配置都无法构建es连接

修改配置

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.3.2.RELEASE</version>
  5. <relativePath/> <!-- lookup parent from repository -->
  6. </parent>
  7. <dependency>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
  10. </dependency>

点进data-es可以看到

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter</artifactId>
  5. <version>2.3.2.RELEASE</version>
  6. <scope>compile</scope>
  7. </dependency>
  8. <dependency>
  9. <groupId>org.springframework.data</groupId>
  10. <artifactId>spring-data-elasticsearch</artifactId>
  11. <version>4.0.2.RELEASE</version>
  12. <scope>compile</scope>
  13. <exclusions>
  14. <exclusion>
  15. <artifactId>transport</artifactId>
  16. <groupId>org.elasticsearch.client</groupId>
  17. </exclusion>
  18. </exclusions>
  19. </dependency>
  20. </dependencies>

配置文件,此处ip和端口可修改为通过yml获取

  1. @Component
  2. public class EsClientConfig {//extends AbstractElasticsearchConfiguration
  3. /*@Override
  4. @Bean
  5. public RestHighLevelClient elasticsearchClient() {
  6. final ClientConfiguration clientConfiguration = ClientConfiguration.builder().connectedTo("139.198.17.162:9200").build();
  7. return RestClients.create(clientConfiguration).rest();
  8. }*/
  9. public static final RequestOptions COMMON_OPTIONS;
  10. static {
  11. RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
  12. COMMON_OPTIONS = builder.build();
  13. }
  14. @Bean
  15. public RestHighLevelClient esRestClient() {
  16. RestClientBuilder builder = null;
  17. // 可以指定多个es
  18. builder = RestClient.builder(new HttpHost("192.168.0.60", 9200, "http"));
  19. RestHighLevelClient client = new RestHighLevelClient(builder);
  20. return client;
  21. }
  22. }

实体类引入,到此完成

  1. @Qualifier("esRestClient")
  2. @Autowired
  3. private RestHighLevelClient highLevelClient;

2. should must联用导致的should失效问题

  1. 工作中使用es查询,本意是想匹配平台id,并且多项商品id匹配一项,原dsl语句

    1. GET app_openapi_youqian_goodsinfo_*/_search
    2. {
    3. "query": {
    4. "bool": {
    5. "must": [
    6. {
    7. "term": {
    8. "platform_id": {
    9. "value": "1001",
    10. "boost": 1
    11. }
    12. }
    13. }
    14. ],
    15. "should": [
    16. {
    17. "term": {
    18. "goods_id.keyword": {
    19. "value": "646484693204",
    20. "boost": 1
    21. }
    22. }
    23. }
    24. ],
    25. "adjust_pure_negative": true,
    26. "boost": 1
    27. }
    28. }
    29. }
  2. 使用嵌套语法

    1. {
    2. "query": {
    3. "bool": {
    4. "should": [
    5. {
    6. "bool": {
    7. "must": [
    8. {"term": {"sex": "男"}},
    9. {"term": {"score": "70"}}
    10. ]
    11. }
    12. },
    13. {
    14. "bool": {
    15. "must": [
    16. {"term": {"sex": "男"}},
    17. {"term": {"score": "80"}}
    18. ]
    19. }
    20. }
    21. ]
    22. }
    23. }
    24. }

    或者

    1. {
    2. "query": {
    3. "bool": {
    4. "should": [
    5. {
    6. "bool": {
    7. "must": [
    8. {"term": {"sex": "男"}},
    9. {"term": {"score": "70"}}
    10. ]
    11. }
    12. },
    13. {
    14. "bool": {
    15. "must": [
    16. {"term": {"sex": "男"}},
    17. {"term": {"score": "80"}}
    18. ]
    19. }
    20. }
    21. ]
    22. }
    23. }
    24. }
  3. 使用”minimum_should_match”: 1

    1. GET app_openapi_youqian_goodsinfo_*/_search
    2. {
    3. "query": {
    4. "bool": {
    5. "must": [
    6. {
    7. "term": {
    8. "platform_id": {
    9. "value": "1001",
    10. "boost": 1
    11. }
    12. }
    13. }
    14. ],
    15. "should": [
    16. {
    17. "term": {
    18. "goods_id.keyword": {
    19. "value": "646484693204",
    20. "boost": 1
    21. }
    22. }
    23. }
    24. ],
    25. "minimum_should_match": 1,
    26. "adjust_pure_negative": true,
    27. "boost": 1
    28. }
    29. }
    30. }