许家凯 před 3 roky
rodič
revize
e6baeafb77
32 změnil soubory, kde provedl 1515 přidání a 440 odebrání
  1. 33 0
      pom.xml
  2. 22 0
      src/main/java/com/winhc/phoenix/example/configuration/CORSConfiguration.java
  3. 105 0
      src/main/java/com/winhc/phoenix/example/configuration/DynamicElasticSearchClient.java
  4. 87 0
      src/main/java/com/winhc/phoenix/example/configuration/DynamicElasticSearchConfiguration.java
  5. 39 0
      src/main/java/com/winhc/phoenix/example/configuration/DynamicElasticSearchProperties.java
  6. 0 91
      src/main/java/com/winhc/phoenix/example/configuration/ElasticSearchConfiguration.java
  7. 44 0
      src/main/java/com/winhc/phoenix/example/configuration/ElasticSearchRestClientProperties.java
  8. 26 0
      src/main/java/com/winhc/phoenix/example/controller/CompanyDynamicController.java
  9. 18 0
      src/main/java/com/winhc/phoenix/example/controller/ElasticsearchProxyController.java
  10. 121 0
      src/main/java/com/winhc/phoenix/example/controller/LegalConsultingClazzController.java
  11. 36 11
      src/main/java/com/winhc/phoenix/example/controller/SearchController.java
  12. 6 1
      src/main/java/com/winhc/phoenix/example/dao/SearchDao.java
  13. 47 18
      src/main/java/com/winhc/phoenix/example/dao/impl/SearchDaoImpl.java
  14. 9 0
      src/main/java/com/winhc/phoenix/example/enums/CompanyQueryType.java
  15. 11 0
      src/main/java/com/winhc/phoenix/example/enums/CompanySearchSortType.java
  16. 9 0
      src/main/java/com/winhc/phoenix/example/enums/ElasticsearchCluster.java
  17. 6 7
      src/main/java/com/winhc/phoenix/example/job/BatchQueryEsTest.java
  18. 4 2
      src/main/java/com/winhc/phoenix/example/job/EsScanJob.java
  19. 80 0
      src/main/java/com/winhc/phoenix/example/job/UpdateEsForMongo.java
  20. 36 34
      src/main/java/com/winhc/phoenix/example/scheduled/ElasticsearchTask.java
  21. 15 0
      src/main/java/com/winhc/phoenix/example/service/CompanyDynamicService.java
  22. 11 0
      src/main/java/com/winhc/phoenix/example/service/ElasticsearchProxyService.java
  23. 8 1
      src/main/java/com/winhc/phoenix/example/service/SearchService.java
  24. 100 0
      src/main/java/com/winhc/phoenix/example/service/impl/CompanyDynamicServiceImpl.java
  25. 22 0
      src/main/java/com/winhc/phoenix/example/service/impl/ElasticsearchProxyServiceImpl.java
  26. 221 0
      src/main/java/com/winhc/phoenix/example/service/impl/FindRelationshipService.java
  27. 19 3
      src/main/java/com/winhc/phoenix/example/service/impl/HbaseQueryServiceImpl.java
  28. 126 124
      src/main/java/com/winhc/phoenix/example/service/impl/SearchV7ServiceImpl.java
  29. 124 25
      src/main/java/com/winhc/phoenix/example/service/impl/SearchV8FastServiceImpl.java
  30. 4 1
      src/main/java/com/winhc/phoenix/example/service/impl/SearchV8ServiceImpl.java
  31. 122 119
      src/main/java/com/winhc/phoenix/example/service/impl/SearchV8SimpServiceImpl.java
  32. 4 3
      src/main/java/com/winhc/phoenix/example/task/AsyncTask.java

+ 33 - 0
pom.xml

@@ -25,6 +25,14 @@
 
     <dependencies>
 
+
+   <!--     <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-configuration-processor</artifactId>
+            <optional>true</optional>
+        </dependency>-->
+
+
         <dependency>
             <groupId>org.elasticsearch</groupId>
             <artifactId>elasticsearch</artifactId>
@@ -196,6 +204,31 @@
             <version>5.5.8</version>
         </dependency>
 
+<!--        <dependency>-->
+<!--            <groupId>com.alibaba.otter</groupId>-->
+<!--            <artifactId>canal.protocol</artifactId>-->
+<!--            <version>1.1.5</version>-->
+<!--            <optional>true</optional>-->
+<!--            <exclusions>-->
+<!--                <exclusion>-->
+<!--                    <groupId>org.ow2.util.bundles</groupId>-->
+<!--                    <artifactId>slf4j-1.6.1</artifactId>-->
+<!--                </exclusion>-->
+<!--            </exclusions>-->
+<!--        </dependency>-->
+<!--        <dependency>-->
+<!--            <groupId>com.alibaba.otter</groupId>-->
+<!--            <artifactId>canal.client</artifactId>-->
+<!--            <version>1.1.5</version>-->
+<!--            <optional>true</optional>-->
+<!--            <exclusions>-->
+<!--                <exclusion>-->
+<!--                    <groupId>org.ow2.util.bundles</groupId>-->
+<!--                    <artifactId>slf4j-1.6.1</artifactId>-->
+<!--                </exclusion>-->
+<!--            </exclusions>-->
+<!--        </dependency>-->
+
     </dependencies>
 
     <build>

+ 22 - 0
src/main/java/com/winhc/phoenix/example/configuration/CORSConfiguration.java

@@ -0,0 +1,22 @@
+package com.winhc.phoenix.example.configuration;
+
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.servlet.config.annotation.CorsRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+
+/**
+ * @Author: XuJiakai
+ * @Date: 2020/8/7 09:48
+ * @Description:
+ */
+@Configuration
+public class CORSConfiguration implements WebMvcConfigurer {
+    @Override
+    public void addCorsMappings(CorsRegistry registry) {
+        registry.addMapping("/**")
+                .allowedOrigins("*")
+                .allowCredentials(true)
+                .allowedMethods("*")
+                .maxAge(3600);
+    }
+}

+ 105 - 0
src/main/java/com/winhc/phoenix/example/configuration/DynamicElasticSearchClient.java

@@ -0,0 +1,105 @@
+package com.winhc.phoenix.example.configuration;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.http.util.EntityUtils;
+import org.elasticsearch.client.Response;
+import org.elasticsearch.client.RestClient;
+import org.elasticsearch.client.RestHighLevelClient;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * @author: XuJiakai
+ * 2021/8/5 14:40
+ */
+@Slf4j
+public class DynamicElasticSearchClient {
+    private final Map<String, RestClient> restClientMap;
+    private final Map<String, RestHighLevelClient> restHighLevelClientMap;
+    private final String primary;
+
+    private final Map<String, List<String>> indexMap;
+
+
+    public DynamicElasticSearchClient(Map<String, RestClient> restClientMap, Map<String, RestHighLevelClient> restHighLevelClientMap, String primary) {
+        this.restClientMap = restClientMap;
+        this.restHighLevelClientMap = restHighLevelClientMap;
+        this.primary = primary;
+        Map<String, Set<String>> tmpIndexMap = restClientMap.entrySet().stream().collect(Collectors.toMap(e -> e.getKey().replace("_client", ""), e -> {
+            RestClient restClient = e.getValue();
+            try {
+                Response get = restClient.performRequest("get", "_cat/indices?h=index");
+                String s = EntityUtils.toString(get.getEntity());
+                Set<String> collect = Arrays.stream(s.split("\n")).filter(index -> !index.startsWith(".")).collect(Collectors.toSet());
+
+                Response get2 = restClient.performRequest("get", "_cat/aliases?h=alias");
+                String s2 = EntityUtils.toString(get2.getEntity());
+                Set<String> collect1 = Arrays.stream(s2.split("\n")).filter(index -> !index.startsWith(".")).collect(Collectors.toSet());
+
+                collect1.addAll(collect);
+                return collect1;
+            } catch (Exception ex) {
+                log.error(ex.getMessage(), e.getKey());
+                log.error(ex.getMessage(), ex);
+                throw new RuntimeException(ex);
+            }
+        }, (t1, t2) -> t1));
+        indexMap = tmpIndexMap.entrySet().stream().flatMap(e -> {
+            String client = e.getKey();
+            return e.getValue().stream().map(v -> v + "\001" + client);
+        }).collect(Collectors.groupingBy(e -> e.split("\001")[0], Collectors.mapping(e -> e.split("\001")[1], Collectors.toList())));
+    }
+
+    public RestClient getRestClient() {
+        return getRestClient(primary);
+    }
+
+    public RestHighLevelClient getRestHighLevelClient() {
+        return getRestHighLevelClient(primary);
+    }
+
+
+    public RestClient getRestClientByIndex(String index) {
+        List<String> strings = indexMap.get(index);
+        if (strings.size() == 1) {
+            return getRestClient(strings.get(0));
+        }
+        if (strings.contains(primary)) {
+            return getRestClient(primary);
+        } else {
+            throw new RuntimeException("要查询的index在多个集群中都有 !");
+        }
+    }
+
+    public RestHighLevelClient getRestHighLevelClientByIndex(String index) {
+        List<String> strings = indexMap.get(index);
+        if (strings.size() == 1) {
+            return getRestHighLevelClient(strings.get(0));
+        }
+        if (strings.contains(primary)) {
+            return getRestHighLevelClient(primary);
+        } else {
+            throw new RuntimeException("要查询的index在多个集群中都有 !");
+        }
+    }
+
+
+    public RestClient getRestClient(String ds) {
+        if (!restClientMap.containsKey(ds + "_client")) {
+            throw new RuntimeException(ds + " not fount !");
+        }
+        return restClientMap.get(ds + "_client");
+
+    }
+
+    public RestHighLevelClient getRestHighLevelClient(String ds) {
+        if (!restHighLevelClientMap.containsKey(ds + "_high_client")) {
+            throw new RuntimeException(ds + " not fount !");
+        }
+        return restHighLevelClientMap.get(ds + "_high_client");
+    }
+}

+ 87 - 0
src/main/java/com/winhc/phoenix/example/configuration/DynamicElasticSearchConfiguration.java

@@ -0,0 +1,87 @@
+package com.winhc.phoenix.example.configuration;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.http.HttpHost;
+import org.apache.http.auth.AuthScope;
+import org.apache.http.auth.Credentials;
+import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.client.CredentialsProvider;
+import org.apache.http.impl.client.BasicCredentialsProvider;
+import org.elasticsearch.client.RestClient;
+import org.elasticsearch.client.RestClientBuilder;
+import org.elasticsearch.client.RestHighLevelClient;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
+import org.springframework.boot.context.properties.PropertyMapper;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationContextAware;
+import org.springframework.context.annotation.Configuration;
+
+import java.time.Duration;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author: XuJiakai
+ * 2021/8/4 11:02
+ */
+@Slf4j
+@Configuration
+public class DynamicElasticSearchConfiguration implements ApplicationContextAware {
+    @Override
+    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
+        DynamicElasticSearchProperties dynamicElasticSearchProperties = applicationContext.getBean(DynamicElasticSearchProperties.class);
+
+        ConfigurableListableBeanFactory beanFactory = (ConfigurableListableBeanFactory) applicationContext
+                .getAutowireCapableBeanFactory();
+
+        Map<String, RestClient> map1 = new HashMap<>(12);
+        Map<String, RestHighLevelClient> map2 = new HashMap<>(12);
+
+        for (Map.Entry<String, ElasticSearchRestClientProperties> entry : dynamicElasticSearchProperties.getRest().entrySet()) {
+            String beanName = entry.getKey();
+            ElasticSearchRestClientProperties properties = entry.getValue();
+
+            HttpHost[] hosts = properties.getUris().stream().map(HttpHost::create).toArray(HttpHost[]::new);
+            RestClientBuilder builder = RestClient.builder(hosts);
+            PropertyMapper map = PropertyMapper.get();
+            map.from(properties::getUsername).whenHasText().to((username) -> {
+                CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
+                Credentials credentials = new UsernamePasswordCredentials(properties.getUsername(),
+                        properties.getPassword());
+                credentialsProvider.setCredentials(AuthScope.ANY, credentials);
+                builder.setHttpClientConfigCallback(
+                        (httpClientBuilder) -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
+            });
+            builder.setRequestConfigCallback((requestConfigBuilder) -> {
+                map.from(properties::getConnectionTimeout).whenNonNull().asInt(Duration::toMillis)
+                        .to(requestConfigBuilder::setConnectTimeout);
+                map.from(properties::getReadTimeout).whenNonNull().asInt(Duration::toMillis)
+                        .to(requestConfigBuilder::setSocketTimeout);
+                return requestConfigBuilder;
+            });
+            RestClient restClient = elasticsearchRestClient(builder);
+            RestHighLevelClient restHighLevelClient = elasticsearchRestHighLevelClient(builder, restClient);
+
+            map1.put(beanName + "_client", restClient);
+            map2.put(beanName + "_high_client", restHighLevelClient);
+            beanFactory.registerSingleton(beanName + "_client", restClient);
+            beanFactory.registerSingleton(beanName + "_high_client", restHighLevelClient);
+            log.info("register {} successful !", beanName);
+        }
+        beanFactory.registerSingleton("dynamicElasticSearchClient", new DynamicElasticSearchClient(map1, map2, dynamicElasticSearchProperties.getPrimary()));
+    }
+
+
+    private RestClient elasticsearchRestClient(RestClientBuilder builder) {
+        return builder.build();
+    }
+
+    private RestHighLevelClient elasticsearchRestHighLevelClient(RestClientBuilder restClientBuilder, RestClient restClient) {
+        if (restClient != null) {
+            return new RestHighLevelClient(restClient);
+        }
+        return new RestHighLevelClient(restClientBuilder.build());
+    }
+
+}

+ 39 - 0
src/main/java/com/winhc/phoenix/example/configuration/DynamicElasticSearchProperties.java

@@ -0,0 +1,39 @@
+package com.winhc.phoenix.example.configuration;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.Setter;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+/**
+ * @author: XuJiakai
+ * 2021/8/4 10:50
+ */
+@Slf4j
+@Getter
+@Setter
+@AllArgsConstructor
+@NoArgsConstructor
+@Component
+@ConfigurationProperties(prefix = DynamicElasticSearchProperties.PREFIX)
+public class DynamicElasticSearchProperties {
+    public static final String PREFIX = "winhc.dynamic.elasticsearch";
+
+    /**
+     * 必须设置默认的库,默认master
+     */
+    private String primary = "master";
+
+    /**
+     * 是否启用严格模式,默认不启动. 严格模式下未匹配到数据源直接报错, 非严格模式下则使用默认数据源primary所设置的数据源
+     */
+    private Boolean strict = false;
+
+    private Map<String, ElasticSearchRestClientProperties> rest = new LinkedHashMap<>();
+}

+ 0 - 91
src/main/java/com/winhc/phoenix/example/configuration/ElasticSearchConfiguration.java

@@ -1,91 +0,0 @@
-package com.winhc.phoenix.example.configuration;
-
-import org.apache.http.HttpHost;
-import org.apache.http.auth.AuthScope;
-import org.apache.http.auth.UsernamePasswordCredentials;
-import org.apache.http.client.CredentialsProvider;
-import org.apache.http.impl.client.BasicCredentialsProvider;
-import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
-import org.elasticsearch.client.RestClient;
-import org.elasticsearch.client.RestClientBuilder;
-import org.elasticsearch.client.RestHighLevelClient;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-
-import java.util.stream.Stream;
-
-/**
- * @Author: XuJiakai
- * @Date: 2020/9/15 20:01
- * @Description:
- */
-@Configuration
-public class ElasticSearchConfiguration {
-    @Value("${es.username}")
-    private String username;
-    @Value("${es.password}")
-    private String password;
-    @Value("${es.host}")
-    private String host;
-
-    @Value("${es.schema:http}")
-    String schema;
-    @Value(value = "${es.connect-timeout:100000}")
-    String connectTimeout;
-    @Value(value = "${es.socket-timeout:600000}")
-    String socketTimeout;
-    @Value(value = "${es.connection-request-timeout:50000}")
-    String connectionRequestTimeout;
-    @Value(value = "${es.max-conn-total:100}")
-    String maxConnTotal;
-    @Value(value = "${es.max-conn-per-route:100}")
-    String maxConnPerRoute;
-
-    @Bean
-    public RestClient bean() {
-        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
-        credentialsProvider.setCredentials(AuthScope.ANY,
-                new UsernamePasswordCredentials(username, password));
-        // 单击所创建的Elasticsearch实例ID,在基本信息页面获取公网地址,即为HOST。
-        return RestClient.builder(new HttpHost(host, 9200))
-                .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
-                    @Override
-                    public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
-                        return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
-                    }
-                }).build();
-    }
-
-
-    @Bean
-    public RestHighLevelClient getClient() {
-        HttpHost[] httpHosts = Stream.of(host.split(",")).map(host -> {
-            String[] split = host.split(":");
-            return new HttpHost(split[0], 9200, schema);
-        }).toArray(HttpHost[]::new);
-
-        // 阿里云Elasticsearch集群需要basic auth验证。
-        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
-        //访问用户名和密码为您创建阿里云Elasticsearch实例时设置的用户名和密码,也是Kibana控制台的登录用户名和密码。
-        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
-
-
-        return new RestHighLevelClient(RestClient
-                .builder(httpHosts)
-                .setMaxRetryTimeoutMillis(60000 * 3)
-                .setRequestConfigCallback(builder -> {
-                    builder.setConnectTimeout(!connectTimeout.isEmpty() ? Integer.valueOf(connectTimeout) : 1000);
-                    builder.setSocketTimeout(!socketTimeout.isEmpty() ? Integer.valueOf(socketTimeout) : 60000);
-                    builder.setConnectionRequestTimeout(!connectionRequestTimeout.isEmpty() ? Integer.valueOf(connectionRequestTimeout) : 500);
-                    return builder;
-                })
-                .setHttpClientConfigCallback(httpAsyncClientBuilder -> {
-                    httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
-                    httpAsyncClientBuilder.setMaxConnTotal(!maxConnTotal.isEmpty() ? Integer.valueOf(maxConnTotal) : 100);
-                    httpAsyncClientBuilder.setMaxConnPerRoute(!maxConnPerRoute.isEmpty() ? Integer.valueOf(maxConnPerRoute) : 100);
-                    return httpAsyncClientBuilder;
-                }).build()
-        );
-    }
-}

+ 44 - 0
src/main/java/com/winhc/phoenix/example/configuration/ElasticSearchRestClientProperties.java

@@ -0,0 +1,44 @@
+package com.winhc.phoenix.example.configuration;
+
+import lombok.Getter;
+import lombok.Setter;
+import lombok.extern.slf4j.Slf4j;
+
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * @author: XuJiakai
+ * 2021/8/4 10:52
+ */
+@Slf4j
+@Getter
+@Setter
+public class ElasticSearchRestClientProperties {
+    /**
+     * Comma-separated list of the Elasticsearch instances to use.
+     */
+    private List<String> uris = new ArrayList<>(Collections.singletonList("http://localhost:9200"));
+
+    /**
+     * Credentials username.
+     */
+    private String username;
+
+    /**
+     * Credentials password.
+     */
+    private String password;
+
+    /**
+     * Connection timeout.
+     */
+    private Duration connectionTimeout = Duration.ofSeconds(100);
+
+    /**
+     * Read timeout.
+     */
+    private Duration readTimeout = Duration.ofSeconds(300);
+}

+ 26 - 0
src/main/java/com/winhc/phoenix/example/controller/CompanyDynamicController.java

@@ -0,0 +1,26 @@
+package com.winhc.phoenix.example.controller;
+
+import com.winhc.phoenix.example.service.CompanyDynamicService;
+import io.swagger.annotations.Api;
+import lombok.AllArgsConstructor;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * @author: XuJiakai
+ * 2021/8/13 09:29
+ */
+@Api(tags = "企业动态", value = "CompanyDynamicController")
+@RestController
+@AllArgsConstructor
+@RequestMapping("dynamic")
+public class CompanyDynamicController {
+    private final CompanyDynamicService companyDynamicService;
+
+    @GetMapping("debug")
+    public Object debug(String code, @RequestParam(defaultValue = "0") Integer from, @RequestParam(defaultValue = "10") Integer size) {
+        return companyDynamicService.debug(code,from,size);
+    }
+}

+ 18 - 0
src/main/java/com/winhc/phoenix/example/controller/ElasticsearchProxyController.java

@@ -0,0 +1,18 @@
+package com.winhc.phoenix.example.controller;
+
+/**
+ * @author: XuJiakai
+ * 2021/9/13 17:14
+ */
+//@Slf4j
+//@RestController
+//@AllArgsConstructor
+//@RequestMapping("es-proxy")
+//public class ElasticsearchProxyController {
+//    private final ElasticsearchProxyService elasticsearchProxyService;
+//
+//    @PostMapping
+//    public Object proxy(@RequestParam ElasticsearchCluster cluster, @RequestParam String index, @RequestBody(required = false) String body) {
+//        return elasticsearchProxyService.query(cluster,index, body);
+//    }
+//}

+ 121 - 0
src/main/java/com/winhc/phoenix/example/controller/LegalConsultingClazzController.java

@@ -0,0 +1,121 @@
+package com.winhc.phoenix.example.controller;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.winhc.phoenix.example.configuration.DynamicElasticSearchClient;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import lombok.AllArgsConstructor;
+import lombok.SneakyThrows;
+import org.elasticsearch.action.search.SearchRequest;
+import org.elasticsearch.action.search.SearchResponse;
+import org.elasticsearch.client.RestHighLevelClient;
+import org.elasticsearch.index.query.BoolQueryBuilder;
+import org.elasticsearch.index.query.QueryBuilder;
+import org.elasticsearch.index.query.SpanNearQueryBuilder;
+import org.elasticsearch.search.builder.SearchSourceBuilder;
+import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.HashMap;
+
+import static org.elasticsearch.index.query.QueryBuilders.*;
+
+/**
+ * @author: XuJiakai
+ * 2021/9/27 17:46
+ */
+@Api(tags = "法律咨询问题分类", value = "legal-consulting-clazz")
+@AllArgsConstructor
+@RestController
+@RequestMapping("legal-consulting-clazz")
+public class LegalConsultingClazzController {
+    private final DynamicElasticSearchClient dynamicElasticSearchClient;
+
+    private static final String index = "winhc-legal-consulting-clazz_v2";
+    private static final String type = "_doc";
+    private ObjectMapper mapper;
+    private static final TypeReference<HashMap<String, Object>> typeRef
+            = new TypeReference<HashMap<String, Object>>() {
+    };
+
+   /* public List<String> analyze(@RequestBody String content){
+        RestClient restClient = dynamicElasticSearchClient.getRestClientByIndex(index);
+        restClient.performRequest("GET",index+"/_analyze",)
+
+    }*/
+
+
+    @SneakyThrows
+    @PostMapping("clazz")
+    @ApiOperation(value = "问题分类")
+    public Object query(@RequestBody String content) {
+        RestHighLevelClient restHighLevelClient = dynamicElasticSearchClient.getRestHighLevelClientByIndex(index);
+
+
+        BoolQueryBuilder boolQueryBuilder = boolQuery();
+
+        boolQueryBuilder.should(multiMatchQuery(content).boost(2)
+                .field("clazz_1", (float) 0.8)
+                .field("clazz_2", (float) 0.5)
+        );
+        boolQueryBuilder.should(
+                disMaxQuery()
+                        .add(multiMatchQuery(content).minimumShouldMatch("4")
+                                .boost((float) 0.5)
+                                .field("clazz_1.standard", (float) 0.8)
+                                .field("clazz_2.standard", (float) 0.2)
+                        )
+                        .add(multiMatchQuery(content).minimumShouldMatch("2")
+                                .boost((float) 0.5)
+                                .field("clazz_1.standard_dup", (float) 0.8)
+                                .field("clazz_2.standard_dup", (float) 0.2)
+                        )
+                        .tieBreaker((float) 0.1)
+        );
+
+        HighlightBuilder highlightBuilder = new HighlightBuilder()
+                .preTags("<font class='red'>")
+                .postTags("</font>")
+                .field("*")
+               ;
+        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
+                .query(boolQueryBuilder)
+                .highlighter(highlightBuilder);
+        SearchRequest searchRequest = new SearchRequest()
+                .indices(index)
+                .types(type)
+                .source(searchSourceBuilder)
+                ;
+
+        SearchResponse search = restHighLevelClient.search(searchRequest);
+
+        return mapper.readValue(search.toString(), typeRef);
+       /* SearchHits hits = search.getHits();
+        SearchHit[] hits1 = hits.getHits();
+        ArrayList<String> strings = new ArrayList<>();
+
+        for (SearchHit hit : hits1) {
+            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
+            String name = (String) sourceAsMap.get("name");
+            strings.add(name);
+        }
+        if (strings.isEmpty()) {
+            strings.add("综合");
+        }
+        return strings;*/
+    }
+
+    private static QueryBuilder getSpanNearQuery(String fields, String content) {
+        SpanNearQueryBuilder spanNearQueryBuilder = spanNearQuery(spanTermQuery(fields, content.charAt(0) + ""), 1);
+        for (int i = 1; i < content.length(); i++) {
+            spanNearQueryBuilder.addClause(spanTermQuery(fields, content.charAt(i) + ""));
+        }
+        return spanNearQueryBuilder;
+    }
+
+
+}

+ 36 - 11
src/main/java/com/winhc/phoenix/example/controller/SearchController.java

@@ -1,8 +1,12 @@
 package com.winhc.phoenix.example.controller;
 
 import com.winhc.phoenix.example.aspect.Timer;
+import com.winhc.phoenix.example.enums.CompanyQueryType;
+import com.winhc.phoenix.example.enums.CompanySearchSortType;
 import com.winhc.phoenix.example.enums.EsVersion;
+import com.winhc.phoenix.example.service.CompanyDynamicService;
 import com.winhc.phoenix.example.service.SearchService;
+import com.winhc.phoenix.example.service.impl.FindRelationshipService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.AllArgsConstructor;
@@ -13,7 +17,10 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 
+import java.util.Collections;
+import java.util.HashSet;
 import java.util.Map;
+import java.util.Set;
 import java.util.regex.Pattern;
 
 /**
@@ -27,34 +34,52 @@ import java.util.regex.Pattern;
 @RequestMapping("es")
 public class SearchController {
     private final Map<String, SearchService> map;
+    private final CompanyDynamicService companyDynamicService;
+    private final FindRelationshipService findRelationshipService;
 
     @Timer
     @ApiOperation(value = "es搜索")
     @GetMapping("query")
-    public Object query(String content, @RequestParam(defaultValue = "v8版_Fast") EsVersion version, @RequestParam(defaultValue = "0") int from, @RequestParam(defaultValue = "10") int size) {
-        return map.get(version.getValue()).query(cleanup(content), from, size);
+    public Object query(String content
+            , @RequestParam(defaultValue = "v8版_Fast") EsVersion version
+            , @RequestParam(defaultValue = "0") int from
+            , @RequestParam(defaultValue = "10") int size
+            , @RequestParam(defaultValue = "默认排序") CompanySearchSortType sortType
+    ) {
+        return map.get(version.getValue()).query(cleanup(content), from, size, new HashSet<>(), sortType);
     }
 
 
     @Timer
-    @ApiOperation(value = "搜索对照组")
-    @GetMapping("control")
-    public Object controlGroup(String content, @RequestParam(defaultValue = "v8版_Fast") EsVersion version) {
-        return map.get(version.getValue()).controlGroup(cleanup(content));
+    @ApiOperation(value = "找关系搜索")
+    @GetMapping("find-relationship")
+    public Object findRelationship(String content, @RequestParam(required = false) Set<CompanyQueryType> set, @RequestParam(defaultValue = "0") int from, @RequestParam(defaultValue = "10") int size) {
+        if (set == null) {
+            set = Collections.emptySet();
+        }
+        return map.get(EsVersion.v8版_Fast.getValue()).query(cleanup(content), from, size, set, CompanySearchSortType.默认排序);
+//        return findRelationshipService.findRelationship(content, typeList, from, size);
+    }
+
+
+    @Timer
+    @ApiOperation(value = "企业动态")
+    @GetMapping("dynamic")
+    public Object dynamic(@RequestParam String companyId, @RequestParam(defaultValue = "0") int from, @RequestParam(defaultValue = "10") int size) {
+        return companyDynamicService.list(companyId, null, from, size);
     }
 
     @Timer
-    @ApiOperation(value = "搜索提示(测试中)")
-    @GetMapping("tips")
-    public Object searchTips(String content, @RequestParam(defaultValue = "v8版_Fast") EsVersion version) {
-        return map.get(version.getValue()).tips(cleanup(content));
+    @ApiOperation(value = "企业动态-展开折叠")
+    @GetMapping("dynamic-detail")
+    public Object dynamicDetail(@RequestParam String companyId, @RequestParam String collapseKey, @RequestParam(defaultValue = "0") int from, @RequestParam(defaultValue = "10") int size) {
+        return companyDynamicService.detail(companyId, collapseKey, from, size);
     }
 
 
     private static final Pattern pattern = Pattern.compile("[^\\u4e00-\\u9fa50-9a-zA-Z]");
 
     private static String cleanup(String val) {
-//        return val;
         return StringUtils.isNotBlank(val) ? pattern.matcher(val).replaceAll("") : "";
     }
 }

+ 6 - 1
src/main/java/com/winhc/phoenix/example/dao/SearchDao.java

@@ -1,6 +1,8 @@
 package com.winhc.phoenix.example.dao;
 
+import lombok.SneakyThrows;
 import org.elasticsearch.index.query.QueryBuilder;
+import org.elasticsearch.search.builder.SearchSourceBuilder;
 import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
 import org.elasticsearch.search.rescore.RescoreBuilder;
 import org.elasticsearch.search.sort.SortBuilder;
@@ -17,7 +19,10 @@ public interface SearchDao {
 
     Object search(String index, String type, QueryBuilder query, int from, int size);
 
-    Object search(String index, String type, QueryBuilder query, RescoreBuilder rescoreBuilder, SortBuilder sortBuilder, FetchSourceContext fetchSourceContext, int from, int size);
+    Object search(String index, String type, QueryBuilder query, List<RescoreBuilder> rescoreBuilder, SortBuilder sortBuilder, FetchSourceContext fetchSourceContext, int from, int size);
+
+    @SneakyThrows
+    Object search(String index, String type, SearchSourceBuilder searchSourceBuilder);
 
     boolean deleteByIds(String index, String type, List<String> ids);
 

+ 47 - 18
src/main/java/com/winhc/phoenix/example/dao/impl/SearchDaoImpl.java

@@ -2,6 +2,7 @@ package com.winhc.phoenix.example.dao.impl;
 
 import com.fasterxml.jackson.core.type.TypeReference;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.winhc.phoenix.example.configuration.DynamicElasticSearchClient;
 import com.winhc.phoenix.example.dao.SearchDao;
 import lombok.AllArgsConstructor;
 import lombok.SneakyThrows;
@@ -33,7 +34,23 @@ import java.util.List;
 @Repository
 @AllArgsConstructor
 public class SearchDaoImpl implements SearchDao {
-    private RestHighLevelClient restHighLevelClient;
+    private final DynamicElasticSearchClient dynamicElasticSearchClient;
+
+    private RestClient getRestClient(String index) {
+        if ("winhc-company-dynamic-v1".equals(index)) {
+            return dynamicElasticSearchClient.getRestClient("old");
+        } else {
+            return dynamicElasticSearchClient.getRestClient("new");
+        }
+    }
+
+    private RestHighLevelClient getRestHighLevelClient(String index) {
+        if ("winhc-company-dynamic-v1".equals(index)) {
+            return dynamicElasticSearchClient.getRestHighLevelClient("old");
+        } else {
+            return dynamicElasticSearchClient.getRestHighLevelClient("new");
+        }
+    }
 
 
     private ObjectMapper mapper;
@@ -54,18 +71,18 @@ public class SearchDaoImpl implements SearchDao {
 
     @SneakyThrows
     @Override
-    public Object search(String index, String type, QueryBuilder query, RescoreBuilder rescoreBuilder, SortBuilder sortBuilder, FetchSourceContext fetchSourceContext, int from, int size) {
-      /*  HighlightBuilder.Field query1 = new HighlightBuilder.Field("history_name.value").highlightQuery(QueryBuilders.matchQuery("history_name.value", "华为"));
-        HighlightBuilder.Field query3 = new HighlightBuilder.Field("history_name.value.keyword").highlightQuery(QueryBuilders.matchQuery("history_name.value", "华为").boost(10000));
-        HighlightBuilder.Field query2 = new HighlightBuilder
-                .Field("cname.value")
-                .highlightQuery(QueryBuilders.matchQuery("cname.value", "华为").boost(1000));*/
+    public Object search(String index, String type, QueryBuilder query, List<RescoreBuilder> rescoreBuilder, SortBuilder sortBuilder, FetchSourceContext fetchSourceContext, int from, int size) {
         HighlightBuilder highlightBuilder = new HighlightBuilder()
-//                .field(query1)
-//                .field(query2)
-//                .field(query3)
-                .preTags("<font color='red'>")
+                .preTags("<font class='my-bold'>")
                 .postTags("</font>")
+                .field("cname.value")
+                .field("history_name.value", 1)
+                .field("legal_entity_name")
+                .field("holder.name", 1)
+                .field("staff.name", 1)
+                .field("icp", 1)
+                .field("app_info", 1)
+                .field("company_tm", 1)
                 .order("score");
         SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
                 .query(query)
@@ -73,10 +90,10 @@ public class SearchDaoImpl implements SearchDao {
                 .size(size)
                 .highlighter(highlightBuilder);
 
-        if (rescoreBuilder != null) {
-            searchSourceBuilder.addRescorer(rescoreBuilder)
-//                    .addRescorer(new QueryRescorerBuilder(query))
-            ;
+        if (rescoreBuilder != null && !rescoreBuilder.isEmpty()) {
+            for (RescoreBuilder builder : rescoreBuilder) {
+                searchSourceBuilder.addRescorer(builder);
+            }
         }
         if (fetchSourceContext != null) {
             searchSourceBuilder.fetchSource(fetchSourceContext);
@@ -91,6 +108,7 @@ public class SearchDaoImpl implements SearchDao {
                 .indices(index)
                 .types(type)
                 .source(searchSourceBuilder);
+        RestHighLevelClient restHighLevelClient = getRestHighLevelClient(index);
 
         SearchResponse search = restHighLevelClient.search(searchRequest);
         return mapper.readValue(search.toString(), typeRef);
@@ -98,25 +116,36 @@ public class SearchDaoImpl implements SearchDao {
 
     @SneakyThrows
     @Override
+    public Object search(String index, String type, SearchSourceBuilder searchSourceBuilder) {
+        SearchRequest searchRequest = new SearchRequest()
+                .indices(index)
+                .types(type)
+                .source(searchSourceBuilder);
+        RestHighLevelClient restHighLevelClient = dynamicElasticSearchClient.getRestHighLevelClientByIndex(index);
+        SearchResponse search = restHighLevelClient.search(searchRequest);
+        return mapper.readValue(search.toString(), typeRef);
+    }
+
+    @SneakyThrows
+    @Override
     public boolean deleteByIds(String index, String type, List<String> ids) {
         BulkRequest bulkRequest = new BulkRequest();
         for (String id : ids) {
             DeleteRequest deleteRequest = new DeleteRequest(index, type, id);
             bulkRequest.add(deleteRequest);
         }
+        RestHighLevelClient restHighLevelClient = getRestHighLevelClient(index);
         restHighLevelClient.bulk(bulkRequest);
         return true;
     }
 
 
-    private final RestClient restClient;
-
     @SneakyThrows
     public Object test() {
+        RestClient restClient = getRestClient("");
         Response get = restClient.performRequest("get", "_cat/indices/judicial_case_v*?h=index");
         String s = EntityUtils.toString(get.getEntity());
         System.out.println(s);
-
         return null;
     }
 }

+ 9 - 0
src/main/java/com/winhc/phoenix/example/enums/CompanyQueryType.java

@@ -0,0 +1,9 @@
+package com.winhc.phoenix.example.enums;
+
+/**
+ * @author: XuJiakai
+ * 2021/8/3 09:05
+ */
+public enum CompanyQueryType {
+    NAME, LEGAL_REPRESENTATIVE, HOLDER_OR_STAFF, APP
+}

+ 11 - 0
src/main/java/com/winhc/phoenix/example/enums/CompanySearchSortType.java

@@ -0,0 +1,11 @@
+package com.winhc.phoenix.example.enums;
+
+/**
+ * @author: XuJiakai
+ * 2021/11/2 13:49
+ */
+public enum CompanySearchSortType {
+    默认排序,
+    注册资本_从高到底, 注册资本_从低到高,
+    成立日期_从早到晚, 成立日期_从晚到早
+}

+ 9 - 0
src/main/java/com/winhc/phoenix/example/enums/ElasticsearchCluster.java

@@ -0,0 +1,9 @@
+package com.winhc.phoenix.example.enums;
+
+/**
+ * @author: XuJiakai
+ * 2021/9/13 17:22
+ */
+public enum ElasticsearchCluster {
+    NEW,OLD
+}

+ 6 - 7
src/main/java/com/winhc/phoenix/example/job/BatchQueryEsTest.java

@@ -1,3 +1,4 @@
+/*
 package com.winhc.phoenix.example.job;
 
 import com.fasterxml.jackson.core.type.TypeReference;
@@ -15,16 +16,19 @@ import org.elasticsearch.index.query.QueryBuilders;
 import org.elasticsearch.search.aggregations.AggregationBuilders;
 import org.elasticsearch.search.aggregations.metrics.sum.SumAggregationBuilder;
 import org.elasticsearch.search.builder.SearchSourceBuilder;
+import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.data.mongodb.core.MongoTemplate;
 import org.springframework.stereotype.Component;
 
 import java.util.*;
 
 import static com.winhc.phoenix.example.util.MaxBatchQueryUtils.addTerms;
+*/
 /**
  * @author: XuJiakai
  * 2021/3/1 09:43
- */
+ *//*
+
 @Slf4j
 @Component
 @AllArgsConstructor
@@ -68,12 +72,6 @@ public class BatchQueryEsTest {
 
         List<SumAggregationBuilder> list = Arrays.asList(
                 AggregationBuilders.sum("open_announcement_1").field("summary.company_court_open_announcement_deleted_0_defendant")
-//                , AggregationBuilders.sum("open_announcement_2").field("summary.company_court_open_announcement_deleted_0_plaintiff")
-//                ,
-//                AggregationBuilders.sum("3").field("summary.company_app_info_del_1")
-//                , AggregationBuilders.sum("4").field("summary.company_staff_del_1")
-//                , AggregationBuilders.sum("5").field("summary.company_icp_del_0")
-//                , AggregationBuilders.sum("6").field("summary.company_tm_del_1")
         );
 
 
@@ -122,3 +120,4 @@ public class BatchQueryEsTest {
     }
 
 }
+*/

+ 4 - 2
src/main/java/com/winhc/phoenix/example/job/EsScanJob.java

@@ -1,6 +1,7 @@
 package com.winhc.phoenix.example.job;
 
 import com.mongodb.client.MongoCollection;
+import com.winhc.phoenix.example.configuration.DynamicElasticSearchClient;
 import com.winhc.phoenix.example.framework.es.EsFastScan;
 import com.winhc.phoenix.example.util.DateUtils;
 import lombok.AllArgsConstructor;
@@ -24,7 +25,8 @@ import java.util.stream.Collectors;
 @Component
 @AllArgsConstructor
 public class EsScanJob {
-    private RestHighLevelClient restHighLevelClient;
+
+    private final DynamicElasticSearchClient dynamicElasticSearchClient;
     private final MongoTemplate mongoTemplate;
 
     public void start() {
@@ -47,7 +49,7 @@ public class EsScanJob {
             }).collect(Collectors.toList());
             person.insertMany(li);
         };
-
+        RestHighLevelClient restHighLevelClient = dynamicElasticSearchClient.getRestHighLevelClient();
         new EsFastScan(restHighLevelClient, func, "winhc_index_" + tn + "_v1", "_doc", dsl).scan();
     }
 

+ 80 - 0
src/main/java/com/winhc/phoenix/example/job/UpdateEsForMongo.java

@@ -0,0 +1,80 @@
+package com.winhc.phoenix.example.job;
+
+import com.mongodb.client.MongoCollection;
+import com.mongodb.client.MongoDatabase;
+import com.winhc.phoenix.example.configuration.DynamicElasticSearchClient;
+import com.winhc.phoenix.example.framework.mongo.MongoDbFastScan;
+import lombok.AllArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.bson.Document;
+import org.elasticsearch.action.bulk.BulkRequest;
+import org.elasticsearch.action.bulk.BulkResponse;
+import org.elasticsearch.action.update.UpdateRequest;
+import org.elasticsearch.client.RestHighLevelClient;
+import org.elasticsearch.script.Script;
+import org.elasticsearch.script.ScriptType;
+import org.springframework.data.mongodb.core.MongoTemplate;
+import org.springframework.stereotype.Component;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.function.Consumer;
+
+/**
+ * @author: XuJiakai
+ * 2021/8/13 18:30
+ */
+@Slf4j
+@Component
+@AllArgsConstructor
+public class UpdateEsForMongo {
+
+    private final DynamicElasticSearchClient dynamicElasticSearchClient;
+    private final MongoTemplate mongoTemplate;
+
+    private static final Script default_script = new Script(ScriptType.INLINE, "painless", "ctx._source.collapse_key=ctx._source.dynamic_code+'_'+ctx._source.dynamic_time", new HashMap<>());
+    private static final Script deleted_script = new Script(ScriptType.INLINE, "painless", "ctx._source.deleted='5'", new HashMap<>());
+    private static final Script fr_script = new Script(ScriptType.INLINE, "painless", "ctx._source.collapse_key='101003_'+ctx._source.dynamic_time", new HashMap<>());
+
+    public void start() {
+        String scanTab = "a_xjk_fix_dynamic_company_status_20210818";
+        String errorTab = "a_xjk_fix_dynamic_company_status_20210818_error";
+
+        RestHighLevelClient old = dynamicElasticSearchClient.getRestHighLevelClient("old");
+        MongoDatabase db = mongoTemplate.getDb();
+        MongoCollection<Document> error = db.getCollection(errorTab);
+        Consumer<List<Document>> func = list -> {
+            try {
+                BulkRequest bulkRequest = new BulkRequest();
+
+                list.stream().filter(r -> StringUtils.isNotEmpty(r.getString("id")))
+                        .map(r -> new UpdateRequest("winhc-company-dynamic_v1", "dynamic", r.getString("id")).script(UpdateEsForMongo.deleted_script))
+                        /* .map(r -> {
+                             if (r.getString("dynamic_code").equals("110101")) {
+                                 return new UpdateRequest("winhc-company-dynamic_v1", "dynamic", r.getString("id")).script(UpdateEsForMongo.fr_script);
+                             } else {
+                                 return new UpdateRequest("winhc-company-dynamic_v1", "dynamic", r.getString("id")).script(UpdateEsForMongo.default_script);
+                             }
+                         })*/
+                        .forEach(bulkRequest::add);
+                BulkResponse bulk = old.bulk(bulkRequest);
+                if (bulk.hasFailures()) {
+                    String s = bulk.buildFailureMessage();
+                    log.error("xjk");
+                    log.error(s);
+                    error.insertMany(list);
+                } else {
+                    log.info("success ...");
+                }
+            } catch (Exception e) {
+                error.insertMany(list);
+                log.error("xjk 111");
+                log.error(e.getMessage(), e);
+            }
+        };
+        MongoDbFastScan mongoDbFastScan = new MongoDbFastScan(scanTab, func, db)
+                .batchSize(500).threadNum(1);
+        mongoDbFastScan.scan();
+    }
+}

+ 36 - 34
src/main/java/com/winhc/phoenix/example/scheduled/ElasticsearchTask.java

@@ -1,34 +1,36 @@
-package com.winhc.phoenix.example.scheduled;
-
-import com.winhc.phoenix.example.service.SearchService;
-import com.winhc.phoenix.example.service.impl.SearchV8SimpServiceImpl;
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.beans.factory.annotation.Qualifier;
-
-/**
- * @author: XuJiakai
- * 2020/12/2 10:11
- */
-//@Component
-@Slf4j
-public class ElasticsearchTask {
-    public ElasticsearchTask(@Qualifier(SearchV8SimpServiceImpl.index) SearchService searchService) {
-        this.searchService = searchService;
-    }
-
-    private final SearchService searchService;
-    private static final String[] keywords = new String[]{"所", "厂", "集团", "店", "公司", "县", "市", "省", "区", "场", "会"};
-    private static int i = 0;
-
-//    @Scheduled(cron = "0 0/1 * * * ? ")
-    public void preheat() {
-        i = ++i % keywords.length;
-        int size = (int) (Math.random() * 9 + 1);
-//        String k = keywords[(int) (Math.random() * keywords.length)];
-        String k = keywords[i];
-        log.info("preheat index {} {}...", k, size);
-        Object query = searchService.query(k, size, size);
-//        log.info("{}", query);
-        log.info("preheat successfully !");
-    }
-}
+//package com.winhc.phoenix.example.scheduled;
+//
+//import com.winhc.phoenix.example.service.SearchService;
+//import com.winhc.phoenix.example.service.impl.SearchV8SimpServiceImpl;
+//import lombok.extern.slf4j.Slf4j;
+//import org.springframework.beans.factory.annotation.Qualifier;
+//
+//import java.util.HashSet;
+//
+///**
+// * @author: XuJiakai
+// * 2020/12/2 10:11
+// */
+////@Component
+//@Slf4j
+//public class ElasticsearchTask {
+//    public ElasticsearchTask(@Qualifier(SearchV8SimpServiceImpl.index) SearchService searchService) {
+//        this.searchService = searchService;
+//    }
+//
+//    private final SearchService searchService;
+//    private static final String[] keywords = new String[]{"所", "厂", "集团", "店", "公司", "县", "市", "省", "区", "场", "会"};
+//    private static int i = 0;
+//
+////    @Scheduled(cron = "0 0/1 * * * ? ")
+//    public void preheat() {
+//        i = ++i % keywords.length;
+//        int size = (int) (Math.random() * 9 + 1);
+////        String k = keywords[(int) (Math.random() * keywords.length)];
+//        String k = keywords[i];
+//        log.info("preheat index {} {}...", k, size);
+//        Object query = searchService.query(k, size, size,new HashSet<>());
+////        log.info("{}", query);
+//        log.info("preheat successfully !");
+//    }
+//}

+ 15 - 0
src/main/java/com/winhc/phoenix/example/service/CompanyDynamicService.java

@@ -0,0 +1,15 @@
+package com.winhc.phoenix.example.service;
+
+import java.util.List;
+
+/**
+ * @author: XuJiakai
+ * 2021/7/1 14:03
+ */
+public interface CompanyDynamicService {
+    Object list(String companyId, List<String> tn,int from,int size);
+
+    Object detail(String companyId, String collapseKey,int from,int size);
+
+    Object debug(String code,  Integer from,  Integer size);
+}

+ 11 - 0
src/main/java/com/winhc/phoenix/example/service/ElasticsearchProxyService.java

@@ -0,0 +1,11 @@
+package com.winhc.phoenix.example.service;
+
+import com.winhc.phoenix.example.enums.ElasticsearchCluster;
+
+/**
+ * @author: XuJiakai
+ * 2021/9/13 17:16
+ */
+public interface ElasticsearchProxyService {
+    String query(ElasticsearchCluster cluster,String index, String content);
+}

+ 8 - 1
src/main/java/com/winhc/phoenix/example/service/SearchService.java

@@ -1,5 +1,10 @@
 package com.winhc.phoenix.example.service;
 
+import com.winhc.phoenix.example.enums.CompanyQueryType;
+import com.winhc.phoenix.example.enums.CompanySearchSortType;
+
+import java.util.Set;
+
 /**
  * @author: XuJiakai
  * 2020/11/19 14:52
@@ -9,5 +14,7 @@ public interface SearchService {
 
     Object controlGroup(String s);
 
-    Object query(String s,int from,int size);
+//    Object query(String s, int from, int size, Set<CompanyQueryType> set);
+
+    Object query(String s, int from, int size, Set<CompanyQueryType> set, CompanySearchSortType sortType);
 }

+ 100 - 0
src/main/java/com/winhc/phoenix/example/service/impl/CompanyDynamicServiceImpl.java

@@ -0,0 +1,100 @@
+package com.winhc.phoenix.example.service.impl;
+
+import com.winhc.phoenix.example.dao.SearchDao;
+import com.winhc.phoenix.example.service.CompanyDynamicService;
+import lombok.AllArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.elasticsearch.index.query.BoolQueryBuilder;
+import org.elasticsearch.index.query.InnerHitBuilder;
+import org.elasticsearch.search.builder.SearchSourceBuilder;
+import org.elasticsearch.search.collapse.CollapseBuilder;
+import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
+import org.elasticsearch.search.sort.SortBuilders;
+import org.elasticsearch.search.sort.SortOrder;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+
+import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
+import static org.elasticsearch.index.query.QueryBuilders.termQuery;
+
+/**
+ * @author: XuJiakai
+ * 2021/7/1 14:04
+ */
+@Slf4j
+@Service
+@AllArgsConstructor
+public class CompanyDynamicServiceImpl implements CompanyDynamicService {
+    private final SearchDao searchDao;
+    private static final String[] includes = new String[]{
+            "tn"
+            , "dynamic_time"
+            , "rowkey"
+            , "association_entity_info.*"
+            , "dynamic_*"
+    };
+
+    @Override
+    public Object list(String companyId, List<String> tn, int from, int size) {
+        BoolQueryBuilder queryBuilder = boolQuery()
+                .must(termQuery("association_entity_info.keyno", companyId));
+
+        FetchSourceContext fetchSourceContext = new FetchSourceContext(true, includes, null);
+
+
+        CollapseBuilder collapseBuilder = new CollapseBuilder("collapse_key");
+        InnerHitBuilder innerHitBuilder = new InnerHitBuilder()
+                .setName("collapse_record")
+                .setFrom(1)
+                .setSize(2)
+                .setTrackScores(true)
+                .setIgnoreUnmapped(true)
+                .setFetchSourceContext(fetchSourceContext)
+                .addSort(SortBuilders.fieldSort("rowkey").order(SortOrder.DESC));
+        collapseBuilder.setInnerHits(innerHitBuilder);
+
+        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
+                .query(queryBuilder)
+                .from(from)
+                .size(size)
+                .fetchSource(includes, null)
+                .sort("dynamic_time", SortOrder.DESC)
+                .collapse(collapseBuilder);
+
+        return searchDao.search("winhc-company-dynamic-v1", "dynamic", searchSourceBuilder);
+    }
+
+    @Override
+    public Object detail(String companyId, String collapseKey, int from, int size) {
+        BoolQueryBuilder queryBuilder = boolQuery()
+                .must(termQuery("association_entity_info.keyno", companyId))
+                .must(termQuery("collapse_key", collapseKey));
+
+
+        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
+                .query(queryBuilder)
+                .from(from)
+                .size(size)
+                .fetchSource(includes, null)
+                .sort("rowkey", SortOrder.DESC);
+
+        return searchDao.search("winhc-company-dynamic-v1", "dynamic", searchSourceBuilder);
+
+    }
+
+    @Override
+    public Object debug(String code, Integer from, Integer size) {
+        BoolQueryBuilder queryBuilder = boolQuery()
+                .must(termQuery("dynamic_code", code));
+
+
+        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
+                .query(queryBuilder)
+                .from(from)
+                .size(size);
+
+        return searchDao.search("winhc-company-dynamic_v1", "dynamic", searchSourceBuilder);
+
+    }
+}

+ 22 - 0
src/main/java/com/winhc/phoenix/example/service/impl/ElasticsearchProxyServiceImpl.java

@@ -0,0 +1,22 @@
+package com.winhc.phoenix.example.service.impl;
+
+import com.winhc.phoenix.example.configuration.DynamicElasticSearchClient;
+import com.winhc.phoenix.example.enums.ElasticsearchCluster;
+import com.winhc.phoenix.example.service.ElasticsearchProxyService;
+import lombok.AllArgsConstructor;
+import org.springframework.stereotype.Service;
+
+/**
+ * @author: XuJiakai
+ * 2021/9/13 17:20
+ */
+@Service
+@AllArgsConstructor
+public class ElasticsearchProxyServiceImpl implements ElasticsearchProxyService {
+    private final DynamicElasticSearchClient dynamicElasticSearchClient;
+
+    @Override
+    public String query(ElasticsearchCluster cluster, String index, String content) {
+        return null;
+    }
+}

+ 221 - 0
src/main/java/com/winhc/phoenix/example/service/impl/FindRelationshipService.java

@@ -0,0 +1,221 @@
+package com.winhc.phoenix.example.service.impl;
+
+import com.winhc.phoenix.example.dao.SearchDao;
+import lombok.AllArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.elasticsearch.common.lucene.search.function.CombineFunction;
+import org.elasticsearch.index.query.BoolQueryBuilder;
+import org.elasticsearch.index.query.MultiMatchQueryBuilder;
+import org.elasticsearch.index.query.Operator;
+import org.elasticsearch.index.query.QueryBuilders;
+import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
+import org.elasticsearch.index.query.functionscore.ScriptScoreFunctionBuilder;
+import org.elasticsearch.script.Script;
+import org.elasticsearch.script.ScriptType;
+import org.elasticsearch.search.rescore.QueryRescoreMode;
+import org.elasticsearch.search.rescore.QueryRescorerBuilder;
+import org.elasticsearch.search.sort.ScriptSortBuilder;
+import org.elasticsearch.search.sort.SortOrder;
+import org.springframework.stereotype.Service;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.elasticsearch.index.query.QueryBuilders.*;
+
+/**
+ * @author: XuJiakai
+ * 2021/7/28 14:38
+ */
+@Slf4j
+@Service
+@AllArgsConstructor
+public class FindRelationshipService {
+    public static enum FindType {
+        NAME, LEGAL_REPRESENTATIVE, HOLDER_OR_STAFF, APP
+    }
+
+    private SearchDao searchDao;
+
+    public static final String index = "winhc-company-v8_3";
+    public static final String type = "company";
+
+    public Object findRelationship(String content, List<FindType> typeList, Integer from, Integer size) {
+        BoolQueryBuilder boolQuery = getBoolQuery(content, typeList);
+
+        Map<String, Object> map = new HashMap<String, Object>() {{
+            put("query_content", content);
+        }};
+        ScriptSortBuilder scriptSortBuilder = new ScriptSortBuilder(new Script(ScriptType.STORED, null, "company-search-script", map), ScriptSortBuilder.ScriptSortType.NUMBER).order(SortOrder.DESC);
+
+//        QueryRescorerBuilder rescorerBuilder = new QueryRescorerBuilder(functionScoreQuery(new ScriptScoreFunctionBuilder(scriptSortBuilder.script())))
+//                .windowSize(50)
+//                .setScoreMode(QueryRescoreMode.Multiply);
+
+
+        QueryRescorerBuilder rescorerBuilder2 = new QueryRescorerBuilder(getRescorerBool(content, typeList))
+                .windowSize(50)
+                .setScoreMode(QueryRescoreMode.Total);
+
+        Object search = searchDao.search(index, type, boolQuery, Arrays.asList(rescorerBuilder2), null, null, from, size);
+
+        return search;
+    }
+
+    private static FunctionScoreQueryBuilder getRescorerBool(String content, List<FindType> typeList) {
+        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
+
+        if (typeList.contains(FindType.NAME)) {
+            boolQuery.should(termQuery("cname.value.keyword", content).boost(1000));
+            boolQuery.should(
+                    disMaxQuery()
+                            .add(multiMatchQuery(content)
+                                    .type(MultiMatchQueryBuilder.Type.CROSS_FIELDS)
+                                    .minimumShouldMatch("5<90%")
+                                    .tieBreaker(0.3F)
+
+                                    .field("cname.value", 16)
+                                    .field("history_name.value", 12))
+                            .add(multiMatchQuery(content)
+                                    .operator(Operator.AND)
+                                    .type(MultiMatchQueryBuilder.Type.CROSS_FIELDS)
+                                    .tieBreaker(0.3F)
+                                    .field("cname.value.standard", 16)
+                                    .field("history_name.value.standard", 12))
+                            .tieBreaker(0.4F)
+            );
+
+            boolQuery.should(disMaxQuery()
+                    .add(disMaxQuery()
+                            .add(termQuery("holder.name.keyword", content).boost(5.5F))
+                            .tieBreaker(0.3F)
+                    ).add(disMaxQuery()
+                            .add(matchPhraseQuery("holder.name", content).boost(10).slop(3))
+                            .tieBreaker(0.3F)
+                    ).tieBreaker(0.3F)
+            );
+            boolQuery.should(disMaxQuery()
+                    .add(disMaxQuery()
+                            .add(termQuery("app_info.keyword", content).boost(40))
+                            .add(termQuery("company_tm.keyword", content).boost(20))
+                            .tieBreaker(0.4F))
+                    .add(disMaxQuery()
+                            .add(matchPhraseQuery("app_info", content).boost(19).slop(3))
+                            .add(matchPhraseQuery("company_tm", content).boost(7).slop(3))
+                            .tieBreaker(0.3F)
+                    ).tieBreaker(0.4F)
+            );
+        }
+
+        if (typeList.contains(FindType.HOLDER_OR_STAFF)) {
+            boolQuery.should(disMaxQuery()
+                    .add(disMaxQuery()
+                            .add(termQuery("holder.name.keyword", content).boost(5.5F))
+                            .add(termQuery("staff.name.keyword", content).boost(5.5F))
+                            .tieBreaker(0.3F)
+                    ).add(disMaxQuery()
+                            .add(matchPhraseQuery("holder.name", content).boost(10).slop(3))
+                            .add(matchPhraseQuery("staff.name", content).boost(6).slop(3))
+                            .tieBreaker(0.3F)
+                    ).tieBreaker(0.3F)
+            );
+        }
+
+        if (typeList.contains(FindType.APP)) {
+            boolQuery.should(disMaxQuery()
+                    .add(termQuery("app_info.keyword", content).boost(40))
+                    .add(matchPhraseQuery("app_info", content).boost(19).slop(3))
+                    .tieBreaker(0.4F)
+            );
+        }
+
+
+        if (typeList.contains(FindType.LEGAL_REPRESENTATIVE)) {
+            BoolQueryBuilder legalRepresentativeQuery = QueryBuilders.boolQuery();
+            legalRepresentativeQuery.should(termQuery("legal_entity_name.keyword", content).boost(10));
+
+            legalRepresentativeQuery.should(matchQuery("legal_entity_name", content).boost(6).minimumShouldMatch("5<95%"));
+            boolQuery.should(legalRepresentativeQuery);
+        }
+
+
+//        return boolQuery;
+
+        Map<String, Object> map = new HashMap<String, Object>() {{
+            put("query_content", content);
+        }};
+        ScriptSortBuilder scriptSortBuilder = new ScriptSortBuilder(new Script(ScriptType.STORED, null, "company-search-script", map), ScriptSortBuilder.ScriptSortType.NUMBER).order(SortOrder.DESC);
+
+        FunctionScoreQueryBuilder function = functionScoreQuery(boolQuery, new ScriptScoreFunctionBuilder(scriptSortBuilder.script()))
+                .maxBoost(100)
+                .boostMode(CombineFunction.MULTIPLY);
+
+        return function;
+    }
+
+
+    private BoolQueryBuilder getBoolQuery(String content, List<FindType> typeList) {
+        BoolQueryBuilder boolQuery = boolQuery();
+        if (typeList.contains(FindType.NAME)) {
+            BoolQueryBuilder nameQuery = QueryBuilders.boolQuery();
+            nameQuery.should(termQuery("cname.value.keyword", content).boost(1000));
+            nameQuery.should(
+                    disMaxQuery()
+                            .add(multiMatchQuery(content)
+                                    .type(MultiMatchQueryBuilder.Type.CROSS_FIELDS)
+                                    .minimumShouldMatch("5<90%")
+                                    .tieBreaker(0.3F)
+
+                                    .field("cname.value", 16)
+                                    .field("history_name.value", 12))
+                            .add(multiMatchQuery(content)
+                                    .operator(Operator.AND)
+                                    .type(MultiMatchQueryBuilder.Type.CROSS_FIELDS)
+                                    .tieBreaker(0.3F)
+                                    .field("cname.value.standard", 16)
+                                    .field("history_name.value.standard", 12))
+                            .tieBreaker(0.4F)
+            );
+            boolQuery.should(nameQuery);
+        }
+
+        if (typeList.contains(FindType.LEGAL_REPRESENTATIVE)) {
+            BoolQueryBuilder legalRepresentativeQuery = QueryBuilders.boolQuery();
+            legalRepresentativeQuery.should(termQuery("legal_entity_name.keyword", content).boost(10));
+
+            legalRepresentativeQuery.should(
+                    matchQuery("legal_entity_name", content).boost(6).minimumShouldMatch("5<95%")
+            );
+            boolQuery.should(legalRepresentativeQuery);
+        }
+
+        if (typeList.contains(FindType.HOLDER_OR_STAFF)) {
+            boolQuery.should(disMaxQuery()
+                    .add(disMaxQuery()
+                            .add(termQuery("holder.name.keyword", content).boost(5.5F))
+                            .add(termQuery("staff.name.keyword", content).boost(5.5F))
+                            .tieBreaker(0.3F)
+                    ).add(disMaxQuery()
+                            .add(matchPhraseQuery("holder.name", content).boost(10).slop(3))
+                            .add(matchPhraseQuery("staff.name", content).boost(6).slop(3))
+                            .tieBreaker(0.3F)
+                    ).tieBreaker(0.3F)
+            );
+        }
+
+        if (typeList.contains(FindType.APP)) {
+            boolQuery.should(disMaxQuery()
+                    .add(termQuery("app_info.keyword", content).boost(40))
+                    .add(matchPhraseQuery("app_info", content).boost(19).slop(3))
+                    .tieBreaker(0.4F)
+            );
+        }
+
+        return boolQuery()
+                .filter(termQuery("deleted", "0"))
+                .filter(rangeQuery("company_score_weight").gt(0.3F))
+                .must(boolQuery);
+    }
+}

+ 19 - 3
src/main/java/com/winhc/phoenix/example/service/impl/HbaseQueryServiceImpl.java

@@ -8,6 +8,7 @@ import com.winhc.phoenix.example.vo.HbaseQueryParamVo;
 import lombok.AllArgsConstructor;
 import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.hadoop.hbase.HConstants;
 import org.apache.hadoop.hbase.TableName;
 import org.apache.hadoop.hbase.client.*;
@@ -120,7 +121,12 @@ public class HbaseQueryServiceImpl implements HbaseQueryService {
     @SneakyThrows
     @Override
     public Object fastGetHbase(String tableName, HbaseQueryParamVo hbaseQueryParamVo) {
-        List<String> keys = Arrays.stream(hbaseQueryParamVo.getQueryKey()).map(String::toUpperCase)
+        if (hbaseQueryParamVo.getQueryKey() == null) {
+            hbaseQueryParamVo.setQueryKey(new String[]{});
+        }
+        List<String> keys = Arrays.stream(hbaseQueryParamVo.getQueryKey())
+                .filter(StringUtils::isNotEmpty)
+                .map(String::toUpperCase)
                 .collect(Collectors.toList());
         List<Get> gets = Arrays.stream(hbaseQueryParamVo.getRowkey()).map(String::getBytes).map(Get::new).peek(get -> {
             for (String key : keys) {
@@ -129,10 +135,20 @@ public class HbaseQueryServiceImpl implements HbaseQueryService {
         }).collect(Collectors.toList());
         try (Table table = connection.getTable(TableName.valueOf(tableName.toUpperCase().getBytes()))) {
             Result[] results = table.get(gets);
+
+
             Map<String, Map<String, String>> collect = Arrays.stream(results)
                     .filter(r -> !r.isEmpty())
-                    .collect(Collectors.toMap(r -> Bytes.toString(r.getRow()), r -> keys.stream().collect(Collectors.toMap(k -> k, k -> Bytes.toString(r.getValue(f, k.getBytes())), (k1, k2) -> k1))
-                            , (r1, r2) -> r1));
+                    .collect(Collectors
+                            .toMap(r -> Bytes.toString(r.getRow())
+                                    , r -> {
+                                        if (keys.isEmpty()) {
+                                            return HbaseResultUtils.parseResult(r);
+                                        } else {
+                                            return keys.stream().collect(Collectors.toMap(k -> k, k -> Bytes.toString(r.getValue(f, k.getBytes())), (k1, k2) -> k1));
+                                        }
+                                    }
+                                    , (r1, r2) -> r1));
             return collect;
         }
     }

+ 126 - 124
src/main/java/com/winhc/phoenix/example/service/impl/SearchV7ServiceImpl.java

@@ -1,124 +1,126 @@
-package com.winhc.phoenix.example.service.impl;
-
-import com.winhc.phoenix.example.dao.impl.SearchDaoImpl;
-import com.winhc.phoenix.example.service.SearchService;
-import lombok.AllArgsConstructor;
-import lombok.extern.slf4j.Slf4j;
-import org.elasticsearch.common.lucene.search.function.CombineFunction;
-import org.elasticsearch.index.query.BoolQueryBuilder;
-import org.elasticsearch.index.query.MultiMatchQueryBuilder;
-import org.elasticsearch.index.query.Operator;
-import org.elasticsearch.index.query.QueryBuilders;
-import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
-import org.elasticsearch.index.query.functionscore.ScriptScoreFunctionBuilder;
-import org.elasticsearch.script.Script;
-import org.elasticsearch.script.ScriptType;
-import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
-import org.elasticsearch.search.sort.ScriptSortBuilder;
-import org.elasticsearch.search.sort.SortOrder;
-import org.springframework.stereotype.Service;
-
-import java.util.HashMap;
-import java.util.Map;
-
-import static org.elasticsearch.index.query.QueryBuilders.*;
-
-/**
- * @author: XuJiakai
- * 2020/12/7 10:58
- */
-@Slf4j
-@Service(SearchV7ServiceImpl.index)
-@AllArgsConstructor
-public class SearchV7ServiceImpl implements SearchService {
-
-
-    private SearchDaoImpl searchDao;
-    private static final ScriptSortBuilder scriptSortBuilder = fastDefaultSort();
-
-    public static final String index = "winhc-company-v7";
-    public static final String type = "company";
-
-    private static final String[] includes = new String[]{"cname", "estiblish_time", "reg_status", "company_type", "province_code", "reg_capital", "logo"};
-    private static final FetchSourceContext fetchSourceContext = new FetchSourceContext(true, includes, null);
-
-
-    private static final String[] includes_tips = new String[]{"cname.show"};
-    private static final FetchSourceContext fetchSourceContext_tips = new FetchSourceContext(true, includes_tips, null);
-
-    private BoolQueryBuilder getBoolQuery(String content) {
-        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
-        MultiMatchQueryBuilder multiMatchQueryBuilder = multiMatchQuery(content)
-                .operator(Operator.AND)
-                .type(MultiMatchQueryBuilder.Type.CROSS_FIELDS)
-                .minimumShouldMatch("5<80%")
-                .tieBreaker(0.3F);
-
-        boolQuery.should(termQuery("cname.value.keyword", content).boost(100));
-        boolQuery.should(termQuery("history_name.value.keyword", content).boost(100));
-
-        multiMatchQueryBuilder
-                .field("cname.value", 16)
-                .field("history_name.value", 12)
-        ;
-
-
-        boolQuery.should(multiMatchQueryBuilder);
-        BoolQueryBuilder boolQuery2 = QueryBuilders.boolQuery();
-        boolQuery2.must(boolQuery);
-        boolQuery2.mustNot(QueryBuilders.existsQuery("current_id"));
-
-        return boolQuery2;
-
-    }
-
-
-    private static ScriptSortBuilder fastDefaultSort() {
-        Map<String, Object> params = new HashMap<>();
-        String script_inline =
-                "if(doc['cname.value.keyword'].value==null||doc['cname.value.keyword'].value.length()==3){" +
-                        "return 0.01;" +
-                        "}" +
-                        "if(doc['cname.value.keyword'].value.length()<=3){" +
-                        "return 0.3;" +
-                        "}" +
-                        "if(doc['reg_status'].value==null || doc['reg_status'].value.contains('销')){" +
-                        "return 1;" +
-                        "}" +
-                        "double a = doc['reg_capital_amount']==null?0.0:doc['reg_capital_amount'].value>1000000000000.0?1000000000000.0:doc['reg_capital_amount'].value;" +
-                        "double w = Math.log(a/10000000+1)+1;" +
-                        "if(doc['company_type'].value=='1'){" +
-                        "w=w+3;" +
-                        "}" +
-                        "return w;";
-
-        Script script = new Script(ScriptType.INLINE, "painless", script_inline, params);
-        return new ScriptSortBuilder(script, ScriptSortBuilder.ScriptSortType.NUMBER).order(SortOrder.DESC);
-
-    }
-
-    @Override
-    public Object tips(String s) {
-        BoolQueryBuilder boolQuery = getBoolQuery(s);
-        FunctionScoreQueryBuilder function = functionScoreQuery(boolQuery, new ScriptScoreFunctionBuilder(scriptSortBuilder.script())
-        )
-                .boostMode(CombineFunction.MULTIPLY);
-
-        return searchDao.search(index, type, function, null, fetchSourceContext_tips, 0, 5);
-    }
-
-    @Override
-    public Object controlGroup(String s) {
-        return searchDao.search(index, type, getBoolQuery(s), null, fetchSourceContext, 0, 10);
-    }
-
-    @Override
-    public Object query(String s, int from, int size) {
-        BoolQueryBuilder boolQuery = getBoolQuery(s);
-        FunctionScoreQueryBuilder function = functionScoreQuery(boolQuery, new ScriptScoreFunctionBuilder(scriptSortBuilder.script())
-        )
-                .boostMode(CombineFunction.MULTIPLY);
-
-        return searchDao.search(index, type, function, null, fetchSourceContext, from, size);
-    }
-}
+//package com.winhc.phoenix.example.service.impl;
+//
+//import com.winhc.phoenix.example.dao.impl.SearchDaoImpl;
+//import com.winhc.phoenix.example.enums.CompanyQueryType;
+//import com.winhc.phoenix.example.service.SearchService;
+//import lombok.AllArgsConstructor;
+//import lombok.extern.slf4j.Slf4j;
+//import org.elasticsearch.common.lucene.search.function.CombineFunction;
+//import org.elasticsearch.index.query.BoolQueryBuilder;
+//import org.elasticsearch.index.query.MultiMatchQueryBuilder;
+//import org.elasticsearch.index.query.Operator;
+//import org.elasticsearch.index.query.QueryBuilders;
+//import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
+//import org.elasticsearch.index.query.functionscore.ScriptScoreFunctionBuilder;
+//import org.elasticsearch.script.Script;
+//import org.elasticsearch.script.ScriptType;
+//import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
+//import org.elasticsearch.search.sort.ScriptSortBuilder;
+//import org.elasticsearch.search.sort.SortOrder;
+//import org.springframework.stereotype.Service;
+//
+//import java.util.HashMap;
+//import java.util.Map;
+//import java.util.Set;
+//
+//import static org.elasticsearch.index.query.QueryBuilders.*;
+//
+///**
+// * @author: XuJiakai
+// * 2020/12/7 10:58
+// */
+//@Slf4j
+//@Service(SearchV7ServiceImpl.index)
+//@AllArgsConstructor
+//public class SearchV7ServiceImpl implements SearchService {
+//
+//
+//    private SearchDaoImpl searchDao;
+//    private static final ScriptSortBuilder scriptSortBuilder = fastDefaultSort();
+//
+//    public static final String index = "winhc-company-v7";
+//    public static final String type = "company";
+//
+//    private static final String[] includes = new String[]{"cname", "estiblish_time", "reg_status", "company_type", "province_code", "reg_capital", "logo"};
+//    private static final FetchSourceContext fetchSourceContext = new FetchSourceContext(true, includes, null);
+//
+//
+//    private static final String[] includes_tips = new String[]{"cname.show"};
+//    private static final FetchSourceContext fetchSourceContext_tips = new FetchSourceContext(true, includes_tips, null);
+//
+//    private BoolQueryBuilder getBoolQuery(String content) {
+//        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
+//        MultiMatchQueryBuilder multiMatchQueryBuilder = multiMatchQuery(content)
+//                .operator(Operator.AND)
+//                .type(MultiMatchQueryBuilder.Type.CROSS_FIELDS)
+//                .minimumShouldMatch("5<80%")
+//                .tieBreaker(0.3F);
+//
+//        boolQuery.should(termQuery("cname.value.keyword", content).boost(100));
+//        boolQuery.should(termQuery("history_name.value.keyword", content).boost(100));
+//
+//        multiMatchQueryBuilder
+//                .field("cname.value", 16)
+//                .field("history_name.value", 12)
+//        ;
+//
+//
+//        boolQuery.should(multiMatchQueryBuilder);
+//        BoolQueryBuilder boolQuery2 = QueryBuilders.boolQuery();
+//        boolQuery2.must(boolQuery);
+//        boolQuery2.mustNot(QueryBuilders.existsQuery("current_id"));
+//
+//        return boolQuery2;
+//
+//    }
+//
+//
+//    private static ScriptSortBuilder fastDefaultSort() {
+//        Map<String, Object> params = new HashMap<>();
+//        String script_inline =
+//                "if(doc['cname.value.keyword'].value==null||doc['cname.value.keyword'].value.length()==3){" +
+//                        "return 0.01;" +
+//                        "}" +
+//                        "if(doc['cname.value.keyword'].value.length()<=3){" +
+//                        "return 0.3;" +
+//                        "}" +
+//                        "if(doc['reg_status'].value==null || doc['reg_status'].value.contains('销')){" +
+//                        "return 1;" +
+//                        "}" +
+//                        "double a = doc['reg_capital_amount']==null?0.0:doc['reg_capital_amount'].value>1000000000000.0?1000000000000.0:doc['reg_capital_amount'].value;" +
+//                        "double w = Math.log(a/10000000+1)+1;" +
+//                        "if(doc['company_type'].value=='1'){" +
+//                        "w=w+3;" +
+//                        "}" +
+//                        "return w;";
+//
+//        Script script = new Script(ScriptType.INLINE, "painless", script_inline, params);
+//        return new ScriptSortBuilder(script, ScriptSortBuilder.ScriptSortType.NUMBER).order(SortOrder.DESC);
+//
+//    }
+//
+//    @Override
+//    public Object tips(String s) {
+//        BoolQueryBuilder boolQuery = getBoolQuery(s);
+//        FunctionScoreQueryBuilder function = functionScoreQuery(boolQuery, new ScriptScoreFunctionBuilder(scriptSortBuilder.script())
+//        )
+//                .boostMode(CombineFunction.MULTIPLY);
+//
+//        return searchDao.search(index, type, function, null, fetchSourceContext_tips, 0, 5);
+//    }
+//
+//    @Override
+//    public Object controlGroup(String s) {
+//        return searchDao.search(index, type, getBoolQuery(s), null, fetchSourceContext, 0, 10);
+//    }
+//
+//    @Override
+//    public Object query(String s, int from, int size, Set<CompanyQueryType> set) {
+//        BoolQueryBuilder boolQuery = getBoolQuery(s);
+//        FunctionScoreQueryBuilder function = functionScoreQuery(boolQuery, new ScriptScoreFunctionBuilder(scriptSortBuilder.script())
+//        )
+//                .boostMode(CombineFunction.MULTIPLY);
+//
+//        return searchDao.search(index, type, function, null, fetchSourceContext, from, size);
+//    }
+//}

+ 124 - 25
src/main/java/com/winhc/phoenix/example/service/impl/SearchV8FastServiceImpl.java

@@ -1,6 +1,8 @@
 package com.winhc.phoenix.example.service.impl;
 
 import com.winhc.phoenix.example.dao.SearchDao;
+import com.winhc.phoenix.example.enums.CompanyQueryType;
+import com.winhc.phoenix.example.enums.CompanySearchSortType;
 import com.winhc.phoenix.example.service.SearchService;
 import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
@@ -11,15 +13,14 @@ import org.elasticsearch.script.ScriptType;
 import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
 import org.elasticsearch.search.rescore.QueryRescoreMode;
 import org.elasticsearch.search.rescore.QueryRescorerBuilder;
+import org.elasticsearch.search.rescore.RescoreBuilder;
 import org.elasticsearch.search.sort.FieldSortBuilder;
-import org.elasticsearch.search.sort.ScriptSortBuilder;
 import org.elasticsearch.search.sort.SortBuilders;
 import org.elasticsearch.search.sort.SortOrder;
 import org.springframework.context.annotation.Primary;
 import org.springframework.stereotype.Service;
 
-import java.util.HashMap;
-import java.util.Map;
+import java.util.*;
 import java.util.regex.Pattern;
 
 import static org.elasticsearch.index.query.QueryBuilders.*;
@@ -35,8 +36,9 @@ import static org.elasticsearch.index.query.QueryBuilders.*;
 public class SearchV8FastServiceImpl implements SearchService {
     private SearchDao searchDao;
 
-//        public static final String index = "winhc-company-v8";
-    public static final String index = "winhc-company-v8_3";
+    //        public static final String index = "winhc-company-v8";
+//    public static final String index = "winhc-company-v8_3";
+    public static final String index = "winhc-company-v8_4";
     public static final String type = "company";
     private static final String[] includes = new String[]{"cname", "legal_entity*", "estiblish_time", "reg_status_std", "company_type", "province_code", "reg_capital", "logo", "new_cid"};
     private static final FetchSourceContext fetchSourceContext = new FetchSourceContext(true, includes, null);
@@ -60,51 +62,105 @@ public class SearchV8FastServiceImpl implements SearchService {
 
     @Override
     public Object controlGroup(String s) {
-        QueryBuilder boolQuery = getBoolQuery(s);
+        QueryBuilder boolQuery = getBoolQuery(s, new HashSet<>());
         return searchDao.search(index, type, boolQuery, null, fetchSourceContext, 0, 10);
     }
 
-    @Override
-    public Object query(String content, int from, int size) {
-        BoolQueryBuilder boolQuery = getBoolQuery(content);
-        Map<String, Object> map = new HashMap<String, Object>() {{
+
+    private List<RescoreBuilder> getReScoreBuilder(String content) {
+        Map<String, Object> map = new HashMap<String, Object>(2) {{
             put("query_content", content);
+            put("der", 0.85);
         }};
-        ScriptSortBuilder scriptSortBuilder = new ScriptSortBuilder(new Script(ScriptType.STORED, null, "company-search-script", map), ScriptSortBuilder.ScriptSortType.NUMBER).order(SortOrder.DESC);
 
+        QueryRescorerBuilder rescorerBuilder_0 = new QueryRescorerBuilder(functionScoreQuery(new ScriptScoreFunctionBuilder(new Script(ScriptType.STORED, null, "company-name-term-score", map))))
+                .windowSize(100)
+                .setScoreMode(QueryRescoreMode.Total);
 
-        QueryRescorerBuilder rescorerBuilder = new QueryRescorerBuilder(functionScoreQuery(new ScriptScoreFunctionBuilder(scriptSortBuilder.script())))
-                .windowSize(50)
+        QueryRescorerBuilder rescorerBuilder = new QueryRescorerBuilder(functionScoreQuery(new ScriptScoreFunctionBuilder(new Script(ScriptType.STORED, null, "company-search-script_v2", map))))
+                .windowSize(100)
                 .setScoreMode(QueryRescoreMode.Multiply);
 
-        Object search = searchDao.search(index, type, boolQuery, rescorerBuilder, null, fetchSourceContext_tips, from, size);
-        return search;
+
+        QueryRescorerBuilder rescorerBuilder2 = new QueryRescorerBuilder(getRescorerBool(content))
+                .windowSize(50)
+                .setScoreMode(QueryRescoreMode.Total);
+
+        return Arrays.asList(rescorerBuilder_0, rescorerBuilder, rescorerBuilder2);
+    }
+
+
+    @Override
+    public Object query(String content, int from, int size, Set<CompanyQueryType> set, CompanySearchSortType sortType) {
+        //召回
+        BoolQueryBuilder boolQuery = getBoolQuery(content, set);
+
+        if (sortType == CompanySearchSortType.注册资本_从低到高) {
+            FieldSortBuilder order = SortBuilders.fieldSort("reg_capital_amount").order(SortOrder.ASC);
+            Object search = searchDao.search(index, type, boolQuery, null, order, null, from, size);
+            return search;
+        } else if (sortType == CompanySearchSortType.注册资本_从高到底) {
+            FieldSortBuilder order = SortBuilders.fieldSort("reg_capital_amount").order(SortOrder.DESC);
+            Object search = searchDao.search(index, type, boolQuery, null, order, null, from, size);
+            return search;
+        } else if (sortType == CompanySearchSortType.成立日期_从早到晚) {
+            FieldSortBuilder order = SortBuilders.fieldSort("estiblish_time").order(SortOrder.ASC);
+            Object search = searchDao.search(index, type, boolQuery, null, order, null, from, size);
+            return search;
+        } else if (sortType == CompanySearchSortType.成立日期_从晚到早) {
+            FieldSortBuilder order = SortBuilders.fieldSort("estiblish_time").order(SortOrder.DESC);
+            Object search = searchDao.search(index, type, boolQuery, null, order, null, from, size);
+            return search;
+        } else {
+            List<RescoreBuilder> reScoreBuilder = getReScoreBuilder(content);
+            Object search = searchDao.search(index, type, boolQuery, reScoreBuilder, null, null, from, size);
+            return search;
+        }
+    }
+
+    private BoolQueryBuilder getRescorerBool(String content) {
+        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
+
+//        boolQuery.should(disMaxQuery()
+//                .add(matchQuery("holder.name", content).boost(10))
+//                .add(matchQuery("staff.name", content).boost(6))
+//                .tieBreaker(0.3F)
+//        );
+        boolQuery.should(disMaxQuery()
+                .add(matchQuery("icp", content).boost(8))
+                .add(matchQuery("app_info", content).boost(19))
+                .add(matchQuery("company_tm", content).boost(20))
+                .tieBreaker(0.5F)
+        );
+        return boolQuery;
     }
 
 
-    private BoolQueryBuilder getBoolQuery(String content) {
+    private BoolQueryBuilder getBoolQuery(String content, Set<CompanyQueryType> set) {
         BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
 
-        boolQuery.should(termQuery("cname.value.keyword", content).boost(1000));
-        boolQuery.should(termQuery("history_name.value.keyword", content).boost(1000));
+//        boolQuery.should(termQuery("cname.value.keyword", content).boost(1000));
+        if (content.length() > 3) {
+            boolQuery.should(termQuery("history_name.value.keyword", content).boost(1000));
+        }
 
         boolQuery.should(disMaxQuery()
                         .add(disMaxQuery()
                                 .add(termQuery("legal_entity_name.keyword", content).boost(10))
-                                .add(termQuery("holder.name.keyword", content).boost(5.5F))
+                                .add(termQuery("holder.name.keyword", content).boost(10F))
                                 .add(termQuery("staff.name.keyword", content).boost(5.5F))
                                 .tieBreaker(0.3F)
                         ).add(disMaxQuery()
 
-                                .add(matchQuery("legal_entity_name", content).boost(6).minimumShouldMatch("5<95%"))
+                                        .add(matchQuery("legal_entity_name", content).boost(6).minimumShouldMatch("5<95%"))
 //                        .add(matchQuery("holder", content).boost(10).minimumShouldMatch("5<80%"))
 //                        .add(matchQuery("staff", content).boost(6).minimumShouldMatch("5<80%"))
 
 //                        .add(matchPhraseQuery("legal_entity_name", content).boost(6).slop(3))
-                                .add(matchPhraseQuery("holder.name", content).boost(10).slop(3))
-                                .add(matchPhraseQuery("staff.name", content).boost(6).slop(3))
+                                        .add(matchPhraseQuery("holder.name", content).boost(10).slop(3))
+                                        .add(matchPhraseQuery("staff.name", content).boost(6).slop(3))
 
-                                .tieBreaker(0.3F)
+                                        .tieBreaker(0.3F)
                         ).tieBreaker(0.3F)
         );
         boolQuery.should(disMaxQuery()
@@ -145,16 +201,47 @@ public class SearchV8FastServiceImpl implements SearchService {
                                 .field("history_name.value.standard", 12))
                         .tieBreaker(0.4F)
         );
-
-
         BoolQueryBuilder boolQuery2 = QueryBuilders.boolQuery()
                 .filter(termQuery("deleted", "0"))
                 .filter(rangeQuery("company_score_weight").gt(0.3F))
                 .must(boolQuery);
+
+        if (!set.isEmpty()) {
+            BoolQueryBuilder filter = boolQuery();
+            if (set.contains(CompanyQueryType.NAME)) {
+                filter.should(getSpanNearQuery("cname.value.standard", content));
+                filter.should(getSpanNearQuery("history_name.value.standard", content));
+            }
+
+            if (set.contains(CompanyQueryType.APP)) {
+                filter.should(getSpanNearQuery("app_info.standard", content));
+            }
+
+            if (set.contains(CompanyQueryType.HOLDER_OR_STAFF)) {
+                filter.should(getSpanNearQuery("holder.name.standard", content));
+                filter.should(getSpanNearQuery("staff.name.standard", content));
+            }
+
+            if (set.contains(CompanyQueryType.LEGAL_REPRESENTATIVE)) {
+                filter.should(getSpanNearQuery("legal_entity_name.standard", content));
+            }
+
+            boolQuery2.filter(filter);
+        }
+
         return boolQuery2;
     }
 
 
+    private static QueryBuilder getSpanNearQuery(String fields, String content) {
+        SpanNearQueryBuilder spanNearQueryBuilder = spanNearQuery(spanTermQuery(fields, content.charAt(0) + ""), 1);
+        for (int i = 1; i < content.length(); i++) {
+            spanNearQueryBuilder.addClause(spanTermQuery(fields, content.charAt(i) + ""));
+        }
+        return spanNearQueryBuilder;
+    }
+
+
     private BoolQueryBuilder getPersonQuery(String content) {
         BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
 
@@ -230,4 +317,16 @@ public class SearchV8FastServiceImpl implements SearchService {
         return pattern.matcher(str).find();
     }
 
+
+    private void se() {
+        String content = "";
+
+        boolQuery().must(multiMatchQuery(content).field("name").minimumShouldMatch("100%"));
+        Script script = new Script(ScriptType.INLINE, "painless", "return doc['company_score_weight'].value;", new HashMap<>());
+        boolQuery().filter(scriptQuery(script));
+
+
+    }
+
+
 }

+ 4 - 1
src/main/java/com/winhc/phoenix/example/service/impl/SearchV8ServiceImpl.java

@@ -1,6 +1,8 @@
 package com.winhc.phoenix.example.service.impl;
 
 import com.winhc.phoenix.example.dao.SearchDao;
+import com.winhc.phoenix.example.enums.CompanyQueryType;
+import com.winhc.phoenix.example.enums.CompanySearchSortType;
 import com.winhc.phoenix.example.service.SearchService;
 import com.winhc.phoenix.example.util.SortUtil;
 import lombok.AllArgsConstructor;
@@ -15,6 +17,7 @@ import org.elasticsearch.search.sort.ScriptSortBuilder;
 import org.springframework.context.annotation.Primary;
 import org.springframework.stereotype.Service;
 
+import java.util.Set;
 import java.util.regex.Pattern;
 
 import static org.elasticsearch.index.query.QueryBuilders.*;
@@ -59,7 +62,7 @@ public class SearchV8ServiceImpl implements SearchService {
     }
 
     @Override
-    public Object query(String content, int from, int size) {
+    public Object query(String content, int from, int size, Set<CompanyQueryType> set, CompanySearchSortType sortType) {
         BoolQueryBuilder boolQuery = getBoolQuery(content);
         ScriptSortBuilder scriptSortBuilder = SortUtil.getInstance().fastSort;
         FunctionScoreQueryBuilder function = functionScoreQuery(boolQuery, new ScriptScoreFunctionBuilder(scriptSortBuilder.script())

+ 122 - 119
src/main/java/com/winhc/phoenix/example/service/impl/SearchV8SimpServiceImpl.java

@@ -1,119 +1,122 @@
-package com.winhc.phoenix.example.service.impl;
-
-import com.winhc.phoenix.example.dao.SearchDao;
-import com.winhc.phoenix.example.service.SearchService;
-import com.winhc.phoenix.example.util.SortUtil;
-import lombok.AllArgsConstructor;
-import lombok.extern.slf4j.Slf4j;
-import org.elasticsearch.common.lucene.search.function.CombineFunction;
-import org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery;
-import org.elasticsearch.index.query.BoolQueryBuilder;
-import org.elasticsearch.index.query.MultiMatchQueryBuilder;
-import org.elasticsearch.index.query.Operator;
-import org.elasticsearch.index.query.QueryBuilders;
-import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
-import org.elasticsearch.index.query.functionscore.ScriptScoreFunctionBuilder;
-import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
-import org.elasticsearch.search.sort.ScriptSortBuilder;
-import org.springframework.stereotype.Service;
-
-import static org.elasticsearch.index.query.QueryBuilders.*;
-
-/**
- * @author: XuJiakai
- * 2020/12/8 19:12
- */
-@Slf4j
-@Service(SearchV8SimpServiceImpl.index)
-@AllArgsConstructor
-public class SearchV8SimpServiceImpl implements SearchService {
-    private SearchDao searchDao;
-
-    public static final String index = "winhc-company-v8-simp";
-    public static final String type = "company";
-
-    private static final String[] includes_tips = new String[]{"cname.show", "new_cid"};
-    private static final FetchSourceContext fetchSourceContext_tips = new FetchSourceContext(true, includes_tips, null);
-
-
-    @Override
-    public Object tips(String s) {
-        BoolQueryBuilder boolQuery = getBoolQuery(s);
-        ScriptSortBuilder scriptSortBuilder = SortUtil.getInstance().fastSort;
-        FunctionScoreQueryBuilder function = functionScoreQuery(boolQuery, new ScriptScoreFunctionBuilder(scriptSortBuilder.script())
-        )
-                .scoreMode(FiltersFunctionScoreQuery.ScoreMode.SUM)
-                .boostMode(CombineFunction.MULTIPLY);
-
-        Object search = searchDao.search(index, type, function, null, fetchSourceContext_tips, 0, 5);
-        return search;
-    }
-
-    @Override
-    public Object controlGroup(String s) {
-        return searchDao.search(index, type, getBoolQuery(s), null, fetchSourceContext_tips, 0, 10);
-    }
-
-    @Override
-    public Object query(String s, int from, int size) {
-        BoolQueryBuilder boolQuery = getBoolQuery(s);
-        ScriptSortBuilder scriptSortBuilder = SortUtil.getInstance().fastSort;
-        FunctionScoreQueryBuilder function = functionScoreQuery(boolQuery, new ScriptScoreFunctionBuilder(scriptSortBuilder.script())
-        )
-                .boostMode(CombineFunction.MULTIPLY);
-
-        Object search = searchDao.search(index, type, function, null, fetchSourceContext_tips, from, size);
-        return search;
-    }
-
-
-    private BoolQueryBuilder getBoolQuery(String content) {
-        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
-
-        // 公司现用名*100
-        boolQuery.should(termQuery("cname.value.keyword", content).boost(100));
-        // 公司曾用名*100
-        boolQuery.should(termQuery("history_name.value.keyword", content).boost(100));
-
-
-        // sum(商标全匹配*20,产品全匹配*40)
-        boolQuery.should(
-                boolQuery()
-                        .should(termQuery("app_info.keyword", content).boost(40))
-                        .should(termQuery("company_tm.keyword", content).boost(20))
-
-        );
-
-        // sum(商标*5,产品信息*15,商标全匹配*20,产品全匹配*40)
-        boolQuery.should(
-                boolQuery()
-
-                        .should(matchQuery("app_info", content).boost(15))
-                        .should(matchQuery("company_tm", content).boost(5))
-
-                        .should(termQuery("app_info.keyword", content).boost(40))
-                        .should(termQuery("company_tm.keyword", content).boost(20))
-                        // 加入最小匹配度
-                        .minimumShouldMatch("5<80%")
-        );
-
-        // 0.7 * max(公司现用名*16,曾用名*12)+ 0.3 * sum(公司现用名*16,曾用名*12)
-        MultiMatchQueryBuilder multiMatchQueryBuilder = multiMatchQuery(content)
-                .operator(Operator.AND)
-                .type(MultiMatchQueryBuilder.Type.CROSS_FIELDS)
-                .minimumShouldMatch("5<80%")
-                .tieBreaker(0.3F)
-                .field("cname.value", 16)
-                .field("history_name.value", 12);
-
-        boolQuery.should(multiMatchQueryBuilder);
-
-        BoolQueryBuilder boolQuery2 = boolQuery()
-                // 过deleted非0数据
-                .filter(termQuery("deleted", "0"))
-                .must(boolQuery);
-        return boolQuery2;
-
-    }
-
-}
+//package com.winhc.phoenix.example.service.impl;
+//
+//import com.winhc.phoenix.example.dao.SearchDao;
+//import com.winhc.phoenix.example.enums.CompanyQueryType;
+//import com.winhc.phoenix.example.service.SearchService;
+//import com.winhc.phoenix.example.util.SortUtil;
+//import lombok.AllArgsConstructor;
+//import lombok.extern.slf4j.Slf4j;
+//import org.elasticsearch.common.lucene.search.function.CombineFunction;
+//import org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery;
+//import org.elasticsearch.index.query.BoolQueryBuilder;
+//import org.elasticsearch.index.query.MultiMatchQueryBuilder;
+//import org.elasticsearch.index.query.Operator;
+//import org.elasticsearch.index.query.QueryBuilders;
+//import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
+//import org.elasticsearch.index.query.functionscore.ScriptScoreFunctionBuilder;
+//import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
+//import org.elasticsearch.search.sort.ScriptSortBuilder;
+//import org.springframework.stereotype.Service;
+//
+//import java.util.Set;
+//
+//import static org.elasticsearch.index.query.QueryBuilders.*;
+//
+///**
+// * @author: XuJiakai
+// * 2020/12/8 19:12
+// */
+//@Slf4j
+//@Service(SearchV8SimpServiceImpl.index)
+//@AllArgsConstructor
+//public class SearchV8SimpServiceImpl implements SearchService {
+//    private SearchDao searchDao;
+//
+//    public static final String index = "winhc-company-v8-simp";
+//    public static final String type = "company";
+//
+//    private static final String[] includes_tips = new String[]{"cname.show", "new_cid"};
+//    private static final FetchSourceContext fetchSourceContext_tips = new FetchSourceContext(true, includes_tips, null);
+//
+//
+//    @Override
+//    public Object tips(String s) {
+//        BoolQueryBuilder boolQuery = getBoolQuery(s);
+//        ScriptSortBuilder scriptSortBuilder = SortUtil.getInstance().fastSort;
+//        FunctionScoreQueryBuilder function = functionScoreQuery(boolQuery, new ScriptScoreFunctionBuilder(scriptSortBuilder.script())
+//        )
+//                .scoreMode(FiltersFunctionScoreQuery.ScoreMode.SUM)
+//                .boostMode(CombineFunction.MULTIPLY);
+//
+//        Object search = searchDao.search(index, type, function, null, fetchSourceContext_tips, 0, 5);
+//        return search;
+//    }
+//
+//    @Override
+//    public Object controlGroup(String s) {
+//        return searchDao.search(index, type, getBoolQuery(s), null, fetchSourceContext_tips, 0, 10);
+//    }
+//
+//    @Override
+//    public Object query(String s, int from, int size, Set<CompanyQueryType> set) {
+//        BoolQueryBuilder boolQuery = getBoolQuery(s);
+//        ScriptSortBuilder scriptSortBuilder = SortUtil.getInstance().fastSort;
+//        FunctionScoreQueryBuilder function = functionScoreQuery(boolQuery, new ScriptScoreFunctionBuilder(scriptSortBuilder.script())
+//        )
+//                .boostMode(CombineFunction.MULTIPLY);
+//
+//        Object search = searchDao.search(index, type, function, null, fetchSourceContext_tips, from, size);
+//        return search;
+//    }
+//
+//
+//    private BoolQueryBuilder getBoolQuery(String content) {
+//        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
+//
+//        // 公司现用名*100
+//        boolQuery.should(termQuery("cname.value.keyword", content).boost(100));
+//        // 公司曾用名*100
+//        boolQuery.should(termQuery("history_name.value.keyword", content).boost(100));
+//
+//
+//        // sum(商标全匹配*20,产品全匹配*40)
+//        boolQuery.should(
+//                boolQuery()
+//                        .should(termQuery("app_info.keyword", content).boost(40))
+//                        .should(termQuery("company_tm.keyword", content).boost(20))
+//
+//        );
+//
+//        // sum(商标*5,产品信息*15,商标全匹配*20,产品全匹配*40)
+//        boolQuery.should(
+//                boolQuery()
+//
+//                        .should(matchQuery("app_info", content).boost(15))
+//                        .should(matchQuery("company_tm", content).boost(5))
+//
+//                        .should(termQuery("app_info.keyword", content).boost(40))
+//                        .should(termQuery("company_tm.keyword", content).boost(20))
+//                        // 加入最小匹配度
+//                        .minimumShouldMatch("5<80%")
+//        );
+//
+//        // 0.7 * max(公司现用名*16,曾用名*12)+ 0.3 * sum(公司现用名*16,曾用名*12)
+//        MultiMatchQueryBuilder multiMatchQueryBuilder = multiMatchQuery(content)
+//                .operator(Operator.AND)
+//                .type(MultiMatchQueryBuilder.Type.CROSS_FIELDS)
+//                .minimumShouldMatch("5<80%")
+//                .tieBreaker(0.3F)
+//                .field("cname.value", 16)
+//                .field("history_name.value", 12);
+//
+//        boolQuery.should(multiMatchQueryBuilder);
+//
+//        BoolQueryBuilder boolQuery2 = boolQuery()
+//                // 过deleted非0数据
+//                .filter(termQuery("deleted", "0"))
+//                .must(boolQuery);
+//        return boolQuery2;
+//
+//    }
+//
+//}

+ 4 - 3
src/main/java/com/winhc/phoenix/example/task/AsyncTask.java

@@ -1,10 +1,10 @@
 package com.winhc.phoenix.example.task;
 
 import com.mongodb.client.MongoCollection;
+import com.winhc.phoenix.example.configuration.DynamicElasticSearchClient;
 import com.winhc.phoenix.example.framework.es.EsFastScan;
 import lombok.AllArgsConstructor;
 import org.bson.Document;
-import org.elasticsearch.client.RestHighLevelClient;
 import org.elasticsearch.search.SearchHit;
 import org.springframework.data.mongodb.core.MongoTemplate;
 import org.springframework.scheduling.annotation.Async;
@@ -25,7 +25,8 @@ import java.util.stream.Collectors;
 @Component
 @AllArgsConstructor
 public class AsyncTask {
-    private final RestHighLevelClient restHighLevelClient;
+    private final DynamicElasticSearchClient dynamicElasticSearchClient;
+
     private final MongoTemplate mongoTemplate;
 
     @Async
@@ -103,7 +104,7 @@ public class AsyncTask {
                 person.insertMany(li);
             };
 
-            new EsFastScan(restHighLevelClient, func, "credit_punishment_case_info_v1", "_doc", dsl).scan();
+            new EsFastScan(dynamicElasticSearchClient.getRestHighLevelClient(), func, "credit_punishment_case_info_v1", "_doc", dsl).scan();
             return new AsyncResult<>(true);
         } catch (Exception e) {
             return new AsyncResult<>(false);