1、创建删除别名:https://www.cnblogs.com/tjp40922/p/12907335.html
    2、springboot集成es客户端: https://www.jianshu.com/p/a584848da515
    3、es使用别名:https://blog.csdn.net/weixin_39631689/article/details/111636714
    4、使用kib进行查询es数据:https://blog.csdn.net/qq_26676207/article/details/81019677
    5、使用索引进行操作:https://www.cnblogs.com/jklixin/archive/2020/05/27/12974270.html
    6、别名操作1:https://www.cnblogs.com/libin2015/p/10649189.html
    7、别名操作2:https://z.itpub.net/article/detail/791751845D2ABAAA1C0A2C4A43F7E2BC
    8、es助手:https://blog.csdn.net/qinqinde123/article/details/106786724/
    9、禁止自动创建索引:https://www.letianbiji.com/elasticsearch/es5-forbid-auto-create-index.html
    10、RestHighLevelClient创建使用,基础增删改查:https://blog.csdn.net/boss_way/article/details/108333768
    11、注意:alias_es_user是别名,_search是索引关键字
    image.png
    image.png
    12、es详细操作:https://blog.csdn.net/lianisgood/article/details/84723248
    13、将mysql转成es的json :http://www.ischoolbar.com/EsParser/
    14、批量插入

    1. //面向对象来操作
    2. @Autowired
    3. private RestHighLevelClient restHighLevelClient;
    4. /**
    5. * 使用elasticSearch批量插入数据
    6. */
    7. @Test
    8. void testBulkRequest() throws IOException {
    9. //1.创建批量导入数据
    10. BulkRequest bulkRequest = new BulkRequest ();
    11. //设置多长时间导入一次
    12. bulkRequest.timeout ("10s");
    13. //2.定义一个集合
    14. ArrayList<User> userList = new ArrayList<> ();
    15. userList.add (new User ("耀",21));
    16. userList.add (new User ("关羽",22));
    17. userList.add (new User ("张飞",20));
    18. userList.add (new User ("刘备",23));
    19. //3.将数据批量添加
    20. for (int i = 0; i < userList.size (); i++) {
    21. //如果需要做批量删除或者批量更新,修改这里请求即可
    22. bulkRequest.add (
    23. new IndexRequest ("liyuanzhi_index")
    24. //不填id时将会生成随机id
    25. .id (""+i+1+"")
    26. .source (JSON.toJSONString (userList.get (i)),XContentType.JSON)
    27. );
    28. }
    29. //4.执行请求
    30. BulkResponse bulkResponse = restHighLevelClient.bulk (bulkRequest, RequestOptions.DEFAULT);
    31. //5.响应 判断是否执行成功
    32. RestStatus status = bulkResponse.status ();
    33. System.out.println (status.getStatus ());
    34. }

    15、批量查询

    1. /**
    2. * 使用elasticSearch批量数据的查询
    3. */
    4. @Test
    5. void testSearch() throws IOException {
    6. //1.创建批量查询数据的请求
    7. SearchRequest searchRequest = new SearchRequest (ESconst.ES_INDEX);
    8. //2.构建搜索的条件
    9. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder ();
    10. //3.查询条件,我们可以使用QueryBuilders 工具类来实现
    11. // termQuery:精确匹配
    12. TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery ("age", "22");
    13. //匹配全部的查询
    14. // MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery ();
    15. //4.放入构建器中
    16. searchSourceBuilder.query (termQueryBuilder);
    17. //设置时间
    18. searchSourceBuilder.timeout (new TimeValue (60, TimeUnit.SECONDS));
    19. //5.构建搜索
    20. searchRequest.source (searchSourceBuilder);
    21. //6.执行请求
    22. SearchResponse searchResponse = restHighLevelClient.search (searchRequest, RequestOptions.DEFAULT);
    23. //打印
    24. System.out.println (JSON.toJSONString (searchResponse.getHits ()));
    25. SearchHit[] hits = searchResponse.getHits ().getHits ();
    26. for (SearchHit hit : hits) {
    27. System.out.println (hit.getSourceAsMap ());
    28. }
    29. }

    16、分页:https://blog.csdn.net/u012500848/article/details/109119728
    17、FunctionScore:
    17-1、https://blog.csdn.net/qq_43292052/article/details/112297768?utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.base&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.base
    17-2、https://www.jianshu.com/p/0bfdc8e5e975?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

    1. SearchRequestBuilder searchRequestBuilder = esClient.client
    2. .prepareSearch({index}).setTypes({type});
    3. BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery()
    4. .should(QueryBuilders.matchPhraseQuery("name", keyWord));
    5. Map<String, Object> params = new HashMap<>();
    6. FieldValueFactorFunctionBuilder fieldQuery = new FieldValueFactorFunctionBuilder(
    7. "score");
    8. // 额外分数=log(1+score)
    9. fieldQuery.factor(0.1f);
    10. fieldQuery.modifier(FieldValueFactorFunction.Modifier.LOG1P);
    11. // 最终分数=_score+额外分数
    12. FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders
    13. .functionScoreQuery(queryBuilder, fieldQuery)
    14. .boostMode(CombineFunction.SUM);
    15. //根据分值倒序排列
    16. searchRequestBuilder.addSort("_score", SortOrder.DESC);
    17. searchRequestBuilder.setQuery(functionScoreQueryBuilder);
    18. //设置获取位置个数
    19. searchRequestBuilder.setFrom(0).setSize(100);
    20. //设置查询分片,防止震荡问题,生产中可以用 用户id 填入,对同一个用户保证多次查询结果相同
    21. searchRequestBuilder.setPreference("233");
    1. public void seltest(){
    2. try {
    3. SearchRequest searchRequest = new SearchRequest("user_info");
    4. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    5. BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    6. boolQueryBuilder.should(QueryBuilders.rangeQuery("age").gte(18).lte(38));
    7. boolQueryBuilder.should(QueryBuilders.rangeQuery("height").gte(160).lte(175));
    8. boolQueryBuilder.should(QueryBuilders.rangeQuery("weight").gte(45).lte(65));
    9. boolQueryBuilder.should(QueryBuilders.matchPhraseQuery("roley_type","VIP"));
    10. boolQueryBuilder.should(QueryBuilders.matchPhraseQuery("roley_type","VIP"));
    11. boolQueryBuilder.should(QueryBuilders.matchQuery("emotional_state",4));
    12. // String[] purposes = new String[]{"聊天","朋友","约会"};
    13. // List<String> purposesList = Arrays.asList(purposes);
    14. boolQueryBuilder.should(QueryBuilders.matchQuery("purpose","聊天,朋友,约会").operator(Operator.AND));
    15. boolQueryBuilder.filter(QueryBuilders.termsQuery("gender","女"));
    16. boolQueryBuilder.filter(QueryBuilders.termsQuery("status","0"));
    17. boolQueryBuilder.filter(QueryBuilders.termsQuery("user_status","999"));
    18. GaussDecayFunctionBuilder age = ScoreFunctionBuilders.gaussDecayFunction("age", 22, 4, 2).setWeight(10);
    19. GaussDecayFunctionBuilder height = ScoreFunctionBuilders.gaussDecayFunction("height", 160, 10, 5).setWeight(10);
    20. GaussDecayFunctionBuilder weight = ScoreFunctionBuilders.gaussDecayFunction("weight", 60, 10, 3).setWeight(10);
    21. FunctionScoreQueryBuilder.FilterFunctionBuilder[] filterFunctionBuilders =
    22. new FunctionScoreQueryBuilder.FilterFunctionBuilder[3];
    23. filterFunctionBuilders[0] = new FunctionScoreQueryBuilder.FilterFunctionBuilder(age);
    24. filterFunctionBuilders[1] = new FunctionScoreQueryBuilder.FilterFunctionBuilder(height);
    25. filterFunctionBuilders[2] = new FunctionScoreQueryBuilder.FilterFunctionBuilder(weight);
    26. FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(boolQueryBuilder, filterFunctionBuilders).scoreMode(FunctionScoreQuery.ScoreMode.SUM).boostMode(CombineFunction.SUM);
    27. searchSourceBuilder.query(functionScoreQueryBuilder);
    28. //向搜索请求对象中设置搜索源
    29. searchRequest.source(searchSourceBuilder);
    30. //发送搜索请求
    31. log.info("查询准备 ");
    32. SearchResponse searchResponse = null;
    33. //此处未做ES Client的吊起 所以做try catch
    34. try {
    35. log.info("client为"+client);
    36. searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    37. } catch (Exception e) {
    38. log.info("重新发起查询------");
    39. searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    40. }
    41. log.info("查询已完成 - 查询社区首页 -- searchResponse "+searchResponse);
    42. SearchHit[] searchHits = searchResponse.getHits().getHits();
    43. if (searchHits == null || searchHits.length <= 0) {
    44. log.info("--------------搜索结果为空 ---------- ");
    45. }
    46. for (SearchHit hit : searchHits) {
    47. String sourceAsString = hit.getSourceAsString();
    48. log.info("当前查询结果 :" + JSON.toJSON(sourceAsString));
    49. }
    50. } catch (Exception e) {
    51. e.printStackTrace();
    52. }
    53. }

    18、分数文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html

    1. package com.xiao.springcloud.test;
    2. import com.xiao.spring.cloud.search.dto.ElasticSearchDoc;
    3. import com.xiao.spring.cloud.search.es.client.ElasticSearchClient;
    4. import com.xiao.spring.cloud.search.service.SearchManangerService;
    5. import org.elasticsearch.action.search.SearchRequestBuilder;
    6. import org.elasticsearch.action.search.SearchResponse;
    7. import org.elasticsearch.client.transport.TransportClient;
    8. import org.elasticsearch.common.lucene.search.function.CombineFunction;
    9. import org.elasticsearch.common.lucene.search.function.FunctionScoreQuery;
    10. import org.elasticsearch.index.query.BoolQueryBuilder;
    11. import org.elasticsearch.index.query.QueryBuilder;
    12. import org.elasticsearch.index.query.QueryBuilders;
    13. import org.elasticsearch.index.query.functionscore.*;
    14. import org.elasticsearch.search.SearchHit;
    15. import org.elasticsearch.search.SearchHits;
    16. import org.junit.Test;
    17. import org.springframework.beans.factory.annotation.Autowired;
    18. import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
    19. import java.util.HashMap;
    20. import java.util.Map;
    21. /**
    22. * [简要描述]:
    23. * [详细描述]:
    24. *
    25. * @author llxiao
    26. * @version 1.0, 2018/10/9 11:07
    27. * @since JDK 1.8
    28. */
    29. public class SearchManagerTest extends SearchApplicationTest
    30. {
    31. @Autowired
    32. private SearchManangerService searchManangerService;
    33. @Autowired
    34. private ElasticSearchClient esClient;
    35. @Test
    36. public void testGetByCommoNo() throws Exception
    37. {
    38. this.isPrint = true;
    39. String url = "/search/manager/getById";
    40. Map<String, String> params = new HashMap<>(1);
    41. params.put("id", "002000000662");
    42. params.put("index", "purcotton");
    43. // 字符串包含Contains
    44. // this.testBasePostApi(url, params, null)
    45. // .andExpect(MockMvcResultMatchers.content().string(new Contains("002000000662")));
    46. // json ID处理
    47. this.testBasePostApi(url, params, null).andExpect(MockMvcResultMatchers.jsonPath("$.id").value("002000000662"));
    48. }
    49. @Test
    50. public void testDel()
    51. {
    52. searchManangerService.deleteData("50101001", "10000");
    53. searchManangerService.deleteData("50101002", "10000");
    54. searchManangerService.deleteData("50101003", "10000");
    55. searchManangerService.deleteData("50101004", "10000");
    56. }
    57. /**
    58. * [简要描述]:在Index为10000下查找标题包含“IPhone”,优先取“品牌手机”这个分类,销量越高越前,结果随机给用户展示<br/>
    59. * [详细描述]:<br/>
    60. * <p>
    61. * llxiao 2019/1/31 - 15:44
    62. **/
    63. @Test
    64. public void testFilterQuery()
    65. {
    66. String searchContent = "IPhone";
    67. TransportClient client = esClient.getTransportClient();
    68. String index = "10000";
    69. SearchRequestBuilder searchBuilder = client.prepareSearch(index);
    70. //分页
    71. searchBuilder.setFrom(0).setSize(10);
    72. //explain为true表示根据数据相关度排序,和关键字匹配最高的排在前面
    73. searchBuilder.setExplain(true);
    74. BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
    75. // 搜索 title字段包含IPhone的数据
    76. queryBuilder.must(QueryBuilders.matchQuery("title", searchContent));
    77. FunctionScoreQueryBuilder.FilterFunctionBuilder[] filterFunctionBuilders = new FunctionScoreQueryBuilder.FilterFunctionBuilder[3];
    78. //过滤条件1:分类为:品牌手机最重要 -- 权重查询Weight
    79. ScoreFunctionBuilder<WeightBuilder> scoreFunctionBuilder = new WeightBuilder();
    80. scoreFunctionBuilder.setWeight(2);
    81. QueryBuilder termQuery = QueryBuilders.termQuery("categoryName", "品牌手机");
    82. FunctionScoreQueryBuilder.FilterFunctionBuilder category = new FunctionScoreQueryBuilder.FilterFunctionBuilder(termQuery, scoreFunctionBuilder);
    83. filterFunctionBuilders[0] = category;
    84. // 过滤条件2:销量越高越排前 --计分查询 FieldValueFactor
    85. ScoreFunctionBuilder<FieldValueFactorFunctionBuilder> fieldValueScoreFunction = new FieldValueFactorFunctionBuilder("salesVolume");
    86. ((FieldValueFactorFunctionBuilder) fieldValueScoreFunction).factor(1.2f);
    87. FunctionScoreQueryBuilder.FilterFunctionBuilder sales = new FunctionScoreQueryBuilder.FilterFunctionBuilder(fieldValueScoreFunction);
    88. filterFunctionBuilders[1] = sales;
    89. // 给定每个用户随机展示: --random_score
    90. ScoreFunctionBuilder<RandomScoreFunctionBuilder> randomScoreFilter = new RandomScoreFunctionBuilder();
    91. ((RandomScoreFunctionBuilder) randomScoreFilter).seed(2);
    92. FunctionScoreQueryBuilder.FilterFunctionBuilder random = new FunctionScoreQueryBuilder.FilterFunctionBuilder(randomScoreFilter);
    93. filterFunctionBuilders[2] = random;
    94. // 多条件查询 FunctionScore
    95. FunctionScoreQueryBuilder query = QueryBuilders.functionScoreQuery(queryBuilder, filterFunctionBuilders)
    96. .scoreMode(FunctionScoreQuery.ScoreMode.SUM).boostMode(CombineFunction.SUM);
    97. searchBuilder.setQuery(query);
    98. SearchResponse response = searchBuilder.execute().actionGet();
    99. SearchHits hits = response.getHits();
    100. String searchSource;
    101. for (SearchHit hit : hits)
    102. {
    103. searchSource = hit.getSourceAsString();
    104. System.out.println(searchSource);
    105. }
    106. // long took = response.getTook().getMillis();
    107. long total = hits.getTotalHits();
    108. System.out.println(total);
    109. }
    110. }

    18、分数计算: https://blog.csdn.net/wwd0501/article/details/78652850
    19、字段相关性分数计算:https://www.cnblogs.com/guanxiaohe/p/12936336.html
    20、es语句:

    1. {
    2. "query" : {
    3. "function_score": {
    4. "query": {"multi_match" : {
    5. "query" : "生物医药",
    6. "fields": ["title"],
    7. "type":"phrase"
    8. }
    9. },
    10. "functions": [
    11. {
    12. "gauss": {
    13. "pdate": {
    14. "origin": "2019-07-01",
    15. "scale": "180d",
    16. "decay": 0.5,
    17. "offset": "30d"
    18. }
    19. }
    20. },
    21. {
    22.   "script_score": {
    23.    "script": "return doc ['org_name'].value == '国信证券' ? 1.9 : 1.0"
    24.   }
    25. }
    26. ],
    27. "score_mode": "sum",
    28. "boost_mode": "multiply"
    29. }},
    30. "_source":["title","org_name","pdate"]
    31. }

    21、es介绍:https://www.cnblogs.com/kukafeiso/p/13947142.html
    22、分数计算:https://blog.csdn.net/wwd0501/article/details/78652850
    23、es分数计算原理:https://blog.csdn.net/weixin_40341116/article/details/80913067
    24、唤起打电话:https://www.cnblogs.com/jackkwok/p/11064173.html
    25、聚合搜索:https://blog.csdn.net/zkf541076398/article/details/79974019?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-2.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-2.nonecase
    26、kibana使用:https://www.cnblogs.com/longronglang/p/12006939.html
    27、es报错:https://zyc88.blog.csdn.net/article/details/102496673
    28、js分数计算: https://www.cnblogs.com/a-du/p/10755787.html

    1. GET es_user_aliase/_search
    2. {
    3. "query": {
    4. "function_score": {
    5. "query": {
    6. "bool": {
    7. "must": [
    8. {
    9. "match": {
    10. "user_name": "张"
    11. }
    12. }
    13. ]
    14. }
    15. },
    16. "functions": [
    17. {
    18. "field_value_factor": {
    19. "field": "post_date",
    20. "modifier": "log",
    21. "factor": 0.1
    22. }
    23. },
    24. {
    25. "exp": {
    26. "post_date": {
    27. "origin": "2021-06-15",
    28. "scale": "1d",
    29. "decay": 0.9
    30. }
    31. }
    32. },
    33. {
    34. "script_score": {
    35. "script": "return doc ['age'].value != null ? (_score + 1.0) : (_score)"
    36. }
    37. }
    38. ],
    39. "boost_mode": "sum"
    40. }
    41. },
    42. "sort": {
    43. "_score": {
    44. "order": "desc"
    45. }
    46. }
    47. }

    29、高斯分数计算 http://t.zoukankan.com/amoyzhu-p-8657969.html
    30、js计算分数 https://blog.csdn.net/mythest/article/details/91823871
    31、https://www.cnblogs.com/zxbdboke/p/14540526.html
    32、打分原理:https://blog.csdn.net/molong1208/article/details/50623948
    33、逆向函数: https://www.elastic.co/guide/cn/elasticsearch/guide/2.x/nested-aggregation.html
    34、 https://www.elastic.co/guide/cn/elasticsearch/guide/current/_aggregation_test_drive.html
    35、 https://www.cnblogs.com/qdhxhz/p/11556764.html
    36、 es搜索教程: https://www.tizi365.com/archives/646.html
    37、数据迁移:https://developer.aliyun.com/article/708030
    38、聚合语法:https://www.cnblogs.com/cnjavahome/p/9164078.html