其他分享
首页 > 其他分享> > ES使用笔记

ES使用笔记

作者:互联网

父类Service服务接口

 1 package com.lcb.soa.search.item.service;
 2 
 3 import com.lcb.soa.misc.service.IService;
 4 import com.lcb.soa.search.item.contract.IndexAliasVO;
 5 
 6 import java.util.Collection;
 7 import java.util.List;
 8 
 9 /**
10  * @author caiyike
11  * @version 1.0
12  * @date 2019-03-03 16:09
13  */
14 public interface ISearchService extends IService {
15 
16     /**
17      * 获取IndexAliasVO
18      */
19     IndexAliasVO getIndexAlias(String staticIndexName, List<String> staticAllIndexName);
20 
21     void resetIndex(String indexName, String settingFile, String mappingFile, String type);
22 
23     void refreshIndex(String indexName);
24 
25     boolean indexExists(String indexName);
26 
27     boolean switchIndexAlias(List<String> staticAllIndexName, String staticIndexName, String newIndexName);
28 
29     /**
30      * 插入单条数据
31      */
32     String indexItem(List<String> staticAllIndexName, String staticIndexName, String indexName, Object doc);
33 
34     /**
35      * 插入多条数据
36      *
37      */
38     boolean indexItems(List<String> staticAllIndexName, String staticIndexName, String indexName, Collection<?> docs);
39 }


父类abstract class

  1 package com.lcb.soa.search.item.service;
  2 
  3 import com.lcb.soa.misc.common.helper.SoaHelper;
  4 import com.lcb.soa.misc.common.util.GeneralHelper;
  5 import com.lcb.soa.search.item.contract.IndexAliasVO;
  6 import lombok.extern.slf4j.Slf4j;
  7 import org.apache.commons.collections.CollectionUtils;
  8 import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequestBuilder;
  9 import org.elasticsearch.action.admin.indices.alias.get.GetAliasesAction;
 10 import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequestBuilder;
 11 import org.elasticsearch.client.Client;
 12 import org.elasticsearch.cluster.metadata.AliasMetaData;
 13 import org.elasticsearch.common.collect.ImmutableOpenMap;
 14 import org.elasticsearch.common.text.Text;
 15 import org.elasticsearch.search.suggest.Suggest;
 16 import org.slf4j.Logger;
 17 import org.slf4j.LoggerFactory;
 18 import org.springframework.beans.factory.annotation.Autowired;
 19 import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
 20 import org.springframework.data.elasticsearch.core.query.IndexQuery;
 21 
 22 import java.io.UnsupportedEncodingException;
 23 import java.text.MessageFormat;
 24 import java.util.ArrayList;
 25 import java.util.Collection;
 26 import java.util.List;
 27 import java.util.Map;
 28 
 29 /**
 30  * @author caiyike
 31  * @version 1.0
 32  * @date 2019-03-03 16:11
 33  */
 34 @Slf4j
 35 public abstract class SearchService implements ISearchService {
 36 
 37     private static Logger logger = LoggerFactory.getLogger(SearchService.class);
 38 
 39     @Autowired
 40     private Client esClient;
 41 
 42     @Autowired
 43     private ElasticsearchTemplate esTemplate;
 44 
 45     @Override
 46     public IndexAliasVO getIndexAlias(String staticIndexName, List<String> staticAllIndexName) {
 47         GetAliasesAction act = GetAliasesAction.INSTANCE;
 48         GetAliasesRequestBuilder req = act.newRequestBuilder(esClient);
 49         req.setAliases(staticIndexName);
 50         ImmutableOpenMap<String, List<AliasMetaData>> resp = req.execute().actionGet().getAliases();
 51         IndexAliasVO vo = new IndexAliasVO();
 52         vo.setAlias(staticIndexName);
 53 
 54         List<String> candidateIndices = new ArrayList<>(staticAllIndexName);
 55         vo.setCandidateIndices(candidateIndices);
 56         for (String index : staticAllIndexName) {
 57             if (SoaHelper.isNotNullOrEmpty(resp.get(index))) {
 58                 vo.setCurrentIndex(index);
 59                 candidateIndices.remove(index);
 60                 break;
 61             }
 62         }
 63         return vo;
 64     }
 65 
 66     @Override
 67     public void resetIndex(String indexName, String settingFile, String mappingFile, String type) {
 68         if (esTemplate.indexExists(indexName))
 69             esTemplate.deleteIndex(indexName);
 70 
 71         createIndexIfNotExists(indexName, settingFile, mappingFile, type);
 72     }
 73 
 74 
 75     @Override
 76     public void refreshIndex(String indexName) {
 77         esTemplate.refresh(indexName);
 78     }
 79 
 80     @Override
 81     public boolean indexExists(String indexName) {
 82         return esTemplate.indexExists(indexName);
 83     }
 84 
 85     @Override
 86     public boolean switchIndexAlias(List<String> staticAllIndexName, String staticIndexName, String newIndexName) {
 87         checkIndexName(staticAllIndexName, staticIndexName, newIndexName, false);
 88 
 89         if (!esTemplate.indexExists(newIndexName)) {
 90             throw new RuntimeException(String.format("index not exists: '%s'", newIndexName));
 91         }
 92 
 93         IndexAliasVO vo = getIndexAlias(staticIndexName, staticAllIndexName);
 94 
 95         if (newIndexName.equals(vo.getCurrentIndex()))
 96             return true;
 97 
 98         refreshIndex(newIndexName);
 99 
100         IndicesAliasesRequestBuilder req = esClient.admin().indices().prepareAliases();
101         if (GeneralHelper.isStrNotEmpty(vo.getCurrentIndex())) {
102             req.removeAlias(vo.getCurrentIndex(), staticIndexName);
103         }
104         return req.addAlias(newIndexName, staticIndexName).execute().actionGet().isAcknowledged();
105     }
106 
107 
108     @Override
109     public String indexItem(List<String> staticAllIndexName, String staticIndexName, String indexName, Object doc) {
110         checkIndexName(staticAllIndexName, staticIndexName, indexName, true);
111 
112         IndexQuery req = new IndexQuery();
113         req.setIndexName(indexName);
114         req.setObject(doc);
115         return esTemplate.index(req);
116     }
117 
118 
119     @Override
120     public boolean indexItems(List<String> staticAllIndexName, String staticIndexName, String indexName, Collection<?> docs) {
121 
122         try {
123             checkIndexName(staticAllIndexName, staticIndexName, indexName, true);
124 
125             if (SoaHelper.isNullOrEmpty(docs))
126                 return false;
127 
128             List<IndexQuery> list = new ArrayList<>(docs.size());
129 
130             for (Object doc : docs) {
131                 IndexQuery req = new IndexQuery();
132                 req.setIndexName(indexName);
133                 req.setObject(doc);
134                 list.add(req);
135             }
136 
137             if (CollectionUtils.isNotEmpty(list)) {
138                 logger.info("导入es数据量: {}", list.size());
139                 logger.info("导入批次item起始: {}----item结束: {}", list.get(0).getObject().toString(), list.get(list.size() - 1).getObject().toString());
140             }
141 
142             esTemplate.bulkIndex(list);
143         } catch (RuntimeException e) {
144             logger.info("导入商品入es失败index: {}", indexName);
145 
146             e.printStackTrace();
147         }
148         return true;
149     }
150 
151 
152     private void createIndexIfNotExists(String indexName, String settingFile, String mappingFile, String type) {
153         if (!esTemplate.indexExists(indexName)) {
154             String setting = ElasticsearchTemplate.readFileFromClasspath(settingFile);
155             String mapping = ElasticsearchTemplate.readFileFromClasspath(mappingFile);
156 
157             try {
158                 esTemplate.createIndex(indexName, setting);
159                 esTemplate.putMapping(indexName, type, mapping);
160             } catch (Exception e) {
161                 if (esTemplate.indexExists(indexName))
162                     esTemplate.deleteIndex(indexName);
163 
164                 String msg = MessageFormat.format("create index {0} fail", indexName);
165                 log.error(msg, e);
166                 throw new RuntimeException(msg, e);
167             }
168         }
169     }
170 
171     private void checkIndexName(List<String> staticAllIndexName, String staticIndexName, String indexName, boolean withAliasName) {
172         boolean isOK = staticAllIndexName.contains(indexName);
173 
174         if (!isOK && withAliasName)
175             isOK = staticIndexName.equals(indexName);
176 
177         if (!isOK)
178             throw new RuntimeException(String.format("invald indexName: '%s'", indexName));
179     }
180 
181     public String getStartChinese(String text) {
182         StringBuffer sb = new StringBuffer();
183         for (int i = 0; i < text.length(); i++) {
184             char c = text.charAt(i);
185             try {
186                 if (String.valueOf(c).getBytes("UTF-8").length > 1) {
187                     sb.append(c);
188                 } else {
189                     break;
190                 }
191             } catch (UnsupportedEncodingException e) {
192                 e.printStackTrace();
193             }
194         }
195         if (sb.length() != 0) {
196             return sb.toString();
197         }
198         return null;
199     }
200 
201     public Map<String, Integer> getOptionMap(Map<String, Integer> map, Suggest.Suggestion<? extends Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option>> suggestion) {
202         if (suggestion != null) {
203             Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option> entry = suggestion.getEntries().get(0);
204             if (entry != null) {
205                 List<? extends Suggest.Suggestion.Entry.Option> pinyinoptions = entry.getOptions();
206                 if (CollectionUtils.isNotEmpty(pinyinoptions)) {
207                     for (Suggest.Suggestion.Entry.Option pinyinoption : pinyinoptions) {
208                         Text text = pinyinoption.getText();
209                         Integer integer = map.get(text.toString());
210                         if (integer == null) {
211                             map.put(text.toString(), 1);
212                         } else {
213                             map.put(text.toString(), integer + 1);
214                         }
215                     }
216                 }
217             }
218         }
219         return map;
220     }
221 }


Service  interface

 1 package com.lcb.soa.search.item.service;
 2 
 3 import com.lcb.soa.search.item.contract.IndexAliasVO;
 4 import com.lcb.soa.search.item.contract.resp.originalItem.OriItemAutoMatchingResponse;
 5 import com.lcb.soa.search.item.model.OriginalItemDoc;
 6 
 7 import java.util.List;
 8 
 9 /**
10  * @author caiyike
11  * @version 1.0
12  * @date 2019-03-03 17:05
13  */
14 public interface IOriginalItemSearchService extends ISearchService {
15 
16     /**
17      * 获取IndexAliasVO
18      */
19     IndexAliasVO getIndexAlias();
20 
21     /**
22      * 重置 (先删除再新建)
23      */
24     void resetIndex(String indexName);
25 
26     /**
27      * 更新
28      */
29     void refreshIndex();
30 
31     /**
32      * 更新
33      *
34      * @param indexName
35      */
36     void refreshIndex(String indexName);
37 
38     /**
39      * 索引是否存在
40      *
41      * @param indexName
42      * @return
43      */
44     boolean indexExists(String indexName);
45 
46     /**
47      * 切换
48      */
49     boolean switchIndexAlias(String newIndexName);
50 
51     /**
52      * 插入数据
53      */
54     boolean indexItems(String indexName, List<OriginalItemDoc> docs);
55 
56 
57     //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
58 
59 
60     OriItemAutoMatchingResponse autoMatching(String text);
61 
62 
63 }


Service impl

  1 package com.lcb.soa.search.item.service;
  2 
  3 import com.alibaba.dubbo.config.annotation.Service;
  4 import com.lcb.soa.search.item.constanct.RegexConstanct;
  5 import com.lcb.soa.search.item.contract.IndexAliasVO;
  6 import com.lcb.soa.search.item.contract.resp.originalItem.OriItemAutoMatchingResponse;
  7 import com.lcb.soa.search.item.domain.OriginalItemDocInner;
  8 import com.lcb.soa.search.item.model.OriginalItemDoc;
  9 import lombok.extern.slf4j.Slf4j;
 10 import org.apache.commons.collections.CollectionUtils;
 11 import org.apache.commons.lang.StringUtils;
 12 import org.elasticsearch.action.search.SearchRequestBuilder;
 13 import org.elasticsearch.action.search.SearchResponse;
 14 import org.elasticsearch.action.search.SearchType;
 15 import org.elasticsearch.client.Client;
 16 import org.elasticsearch.search.SearchHit;
 17 import org.elasticsearch.search.builder.SearchSourceBuilder;
 18 import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
 19 import org.elasticsearch.search.suggest.Suggest;
 20 import org.elasticsearch.search.suggest.SuggestBuilder;
 21 import org.elasticsearch.search.suggest.completion.CompletionSuggestion;
 22 import org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder;
 23 import org.slf4j.Logger;
 24 import org.slf4j.LoggerFactory;
 25 import org.springframework.beans.factory.annotation.Autowired;
 26 import org.elasticsearch.search.suggest.Suggest.Suggestion;
 27 import org.elasticsearch.search.suggest.Suggest.Suggestion.Entry;
 28 import org.elasticsearch.search.suggest.Suggest.Suggestion.Entry.Option;
 29 
 30 import java.util.ArrayList;
 31 import java.util.HashMap;
 32 import java.util.List;
 33 import java.util.Map;
 34 
 35 import static com.lcb.soa.search.item.domain.OriginalItemConfig.*;
 36 
 37 
 38 /**
 39  * @author caiyike
 40  * @version 1.0
 41  * @date 2019-03-03 17:08
 42  */
 43 @Slf4j
 44 @Service(timeout = 5000)
 45 public class OriginalItemSearchService extends SearchService implements IOriginalItemSearchService {
 46 
 47     private static Logger logger = LoggerFactory.getLogger(OriginalItemSearchService.class);
 48 
 49     @Autowired
 50     private Client esClient;
 51 
 52 
 53     @Override
 54     public IndexAliasVO getIndexAlias() {
 55         return super.getIndexAlias(INDEX_NAME, ALL_INDEX_NAMES);
 56     }
 57 
 58     @Override
 59     public void resetIndex(String indexName) {
 60         super.resetIndex(indexName, INDEX_SETTING_FILE, INDEX_ITEM_TYPE_MAPPING_FILE, ITEM_TYPE);
 61     }
 62 
 63 
 64     @Override
 65     public void refreshIndex() {
 66         super.refreshIndex(INDEX_NAME);
 67     }
 68 
 69     @Override
 70     public boolean switchIndexAlias(String newIndexName) {
 71         return super.switchIndexAlias(ALL_INDEX_NAMES, INDEX_NAME, newIndexName);
 72     }
 73 
 74     @Override
 75     public boolean indexItems(String indexName, List<OriginalItemDoc> docs) {
 76         for (OriginalItemDoc doc : docs) {
 77             if (doc.getId() == null) {
 78                 throw new RuntimeException("doc id is null");
 79             }
 80 
 81         }
 82         ArrayList<OriginalItemDocInner> docList = new ArrayList<>();
 83         try {
 84             for (OriginalItemDoc doc : docs) {
 85                 docList.add(OriginalItemDocInner.formDoc(doc));
 86             }
 87         } catch (Exception e) {
 88 
 89         }
 90         return super.indexItems(ALL_INDEX_NAMES, INDEX_NAME, indexName, docList);
 91     }
 92 
 93     @Override
 94     public OriItemAutoMatchingResponse autoMatching(String text) {
 95         OriItemAutoMatchingResponse response = new OriItemAutoMatchingResponse();
 96         String trim = StringUtils.trim(text);
 97         if (StringUtils.isBlank(trim)) {
 98             return response;
 99         }
100         response = searchAutoMatching(text);
101         return response;
102     }
103 
104     private OriItemAutoMatchingResponse searchAutoMatching(String text) {
105         OriItemAutoMatchingResponse response = new OriItemAutoMatchingResponse();
106         // 指定库
107         String indexName = INDEX_NAME;
108         SearchRequestBuilder prepareSearch = esClient.prepareSearch(indexName).setSearchType(SearchType.DEFAULT);
109 
110         // 设置指定返回字段
111         setSourceFields(prepareSearch);
112 
113         // 文本查询
114         SuggestBuilder suggestBuilder = setSearchConditionForAutoComplete(text);
115         prepareSearch = prepareSearch.suggest(suggestBuilder);
116         // 执行查询
117         logger.info("itemSearchAutoComplete search query: -> {}", prepareSearch.toString());
118         SearchResponse searchResponse = prepareSearch.execute().actionGet();
119         if (suggestBuilder == null) {
120             return response;
121         }
122 
123         Suggestion<? extends Entry<? extends Option>> keyWordsSuggestion = searchResponse.getSuggest()
124                 .getSuggestion("keywords");
125         Suggestion<? extends Entry<? extends Option>> pinyinSuggestion = searchResponse.getSuggest()
126                 .getSuggestion("pinyinKeywords");
127 
128 
129         Map<String, String> hashMap = new HashMap<>();
130         addOptionMap(hashMap, keyWordsSuggestion);
131         addOptionMap(hashMap, pinyinSuggestion);
132         response.setAutoComplete(hashMap);
133         return response;
134     }
135 
136     private Map<String, String> addOptionMap(Map<String, String> map, Suggest.Suggestion<? extends Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option>> suggestion) {
137         if (suggestion != null) {
138             List entries = suggestion.getEntries();
139             if (CollectionUtils.isNotEmpty(entries)) {
140                 CompletionSuggestion.Entry entry = (CompletionSuggestion.Entry) entries.get(0);
141                 if (entry != null) {
142                     List<CompletionSuggestion.Entry.Option> options = entry.getOptions();
143                     if (CollectionUtils.isNotEmpty(options)) {
144                         for (CompletionSuggestion.Entry.Option option : options) {
145                             SearchHit hit = option.getHit();
146                             Map<String, Object> sourceAsMap = hit.getSourceAsMap();
147                             String itemName = sourceAsMap.get("itemName").toString();
148                             String oeNumber = sourceAsMap.get("oeNumber").toString();
149                             map.put(itemName,oeNumber);
150                         }
151                     }
152                 }
153             }
154         }
155         return map;
156     }
157 
158     private void setSourceFields(SearchRequestBuilder prepareSearch) {
159         String[] fields = {"oeNumber", "itemName"};
160 
161         FetchSourceContext sourceContext = new FetchSourceContext(true, fields, null);
162 
163         SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
164 
165         searchSourceBuilder.fetchSource(sourceContext);
166 
167         prepareSearch.setSource(searchSourceBuilder);
168     }
169 
170     private SuggestBuilder setSearchConditionForAutoComplete(String text) {
171         SuggestBuilder suggestBuilder = null;
172         text = StringUtils.trim(text);
173         CompletionSuggestionBuilder csb = null;
174         CompletionSuggestionBuilder pinyinCsb = null;
175         if (StringUtils.isNotBlank(text)) {
176             suggestBuilder = new SuggestBuilder();
177             if (text.matches(RegexConstanct.ALL_ENGLISH_ALPHABET)) {
178                 pinyinCsb = new CompletionSuggestionBuilder("keywords.pinyin").text(text).size(10000);
179                 suggestBuilder.addSuggestion("pinyinKeywords", pinyinCsb);
180             } else {
181                 String quertString = getStartChinese(text);
182                 if (quertString == null) {
183                     quertString = text;
184                 }
185                 csb = new CompletionSuggestionBuilder("keywords.suggest").text(quertString).size(10000);
186                 suggestBuilder.addSuggestion("keywords", csb);
187 
188             }
189         }
190 
191         return suggestBuilder;
192     }
193 
194 }


model

 1 package com.lcb.soa.search.item.model;
 2 
 3 import lombok.Data;
 4 
 5 import java.io.Serializable;
 6 import java.util.Set;
 7 
 8 /**
 9  * @author caiyike
10  * @version 1.0
11  * @date 2019-03-04 10:47
12  */
13 @Data
14 public class OriginalItemDoc implements Serializable {
15 
16     private Long id;
17 
18     private String oeNumber;
19 
20     private String itemName;
21 
22     private Long manufacturerId;
23 
24     private String manufacturerName;
25 
26     private Long categoryId;
27 
28     private String categoryName;
29 
30     private Long createdTime;
31 
32     private Long updatedTime;
33 
34     private Set<String> keywords;
35 
36     private Set<String> pinyinKeywords;
37 }


modelDoc

 1 package com.lcb.soa.search.item.domain;
 2 
 3 import com.lcb.soa.search.item.model.OriginalItemDoc;
 4 import org.springframework.data.elasticsearch.annotations.Document;
 5 import org.springframework.data.elasticsearch.annotations.Mapping;
 6 import org.springframework.data.elasticsearch.annotations.Setting;
 7 
 8 import static com.lcb.soa.search.item.domain.OriginalItemConfig.INDEX_NAME;
 9 import static com.lcb.soa.search.item.domain.OriginalItemConfig.ITEM_TYPE;
10 import static com.lcb.soa.search.item.domain.OriginalItemConfig.INDEX_ITEM_TYPE_MAPPING_FILE;
11 import static com.lcb.soa.search.item.domain.OriginalItemConfig.INDEX_SETTING_FILE;
12 
13 /**
14  * @author caiyike
15  * @version 1.0
16  * @date 2019-03-04 14:22
17  */
18 @Setting(settingPath = INDEX_SETTING_FILE)
19 @Mapping(mappingPath = INDEX_ITEM_TYPE_MAPPING_FILE)
20 @Document(indexName = INDEX_NAME, type = ITEM_TYPE, createIndex = false)
21 public class OriginalItemDocInner extends OriginalItemDoc {
22 
23     public static OriginalItemDocInner formDoc(OriginalItemDoc doc) {
24         if (doc == null)
25             return null;
26         OriginalItemDocInner inner = new OriginalItemDocInner();
27         copy(doc, inner);
28         return inner;
29     }
30 
31     public static OriginalItemDoc toDoc(OriginalItemDocInner inner) {
32         if (inner == null)
33             return null;
34         OriginalItemDoc doc = new OriginalItemDoc();
35         copy(inner, doc);
36         return doc;
37     }
38 
39     private static void copy(OriginalItemDoc source, OriginalItemDoc target) {
40         target.setId(source.getId());
41         target.setOeNumber(source.getOeNumber());
42         target.setItemName(source.getItemName());
43         target.setManufacturerId(source.getManufacturerId());
44         target.setManufacturerName(source.getManufacturerName());
45         target.setCategoryId(source.getCategoryId());
46         target.setCategoryName(source.getCategoryName());
47         target.setCreatedTime(source.getCreatedTime());
48         target.setUpdatedTime(source.getUpdatedTime());
49         target.setKeywords(source.getKeywords());
50         target.setPinyinKeywords(source.getPinyinKeywords());
51     }
52 }


Config

 1 package com.lcb.soa.search.item.domain;
 2 
 3 import java.util.Arrays;
 4 import java.util.List;
 5 
 6 /**
 7  * @author caiyike
 8  * @version 1.0
 9  * @date 2019-03-04 14:24
10  */
11 public class OriginalItemConfig {
12 
13     public static final String INDEX_NAME                    = "lcb_original_item";
14     public static final String INDEX_NAME_A                    = "lcb_original_item_a";
15     public static final String INDEX_NAME_B                    = "lcb_original_item_b";
16     public static final List<String> ALL_INDEX_NAMES        = Arrays.asList(INDEX_NAME_A, INDEX_NAME_B);
17     public static final String INDEX_SETTING_FILE            = "index-item-setting.json";
18     public static final String ITEM_TYPE                    = "item";
19     public static final String INDEX_ITEM_TYPE_MAPPING_FILE    = "index-type-originalItem-mapping.json";
20 }

 

Respository

 1 package com.lcb.soa.search.item.repository;
 2 
 3 import com.lcb.soa.search.item.domain.OriginalItemDocInner;
 4 import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
 5 
 6 /**
 7  * @author caiyike
 8  * @version 1.0
 9  * @date 2019-03-04 14:39
10  */
11 public interface OriginalItemRespository extends ElasticsearchRepository<OriginalItemDocInner,Long> {
12 }

spring-elasticsearch.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
 5        xsi:schemaLocation="http://www.springframework.org/schema/beans
 6                         http://www.springframework.org/schema/beans/spring-beans.xsd
 7                         http://www.springframework.org/schema/data/elasticsearch
 8                         http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd">
 9 
10     <!-- 配置elasticsearch 连接 -->
11     <elasticsearch:transport-client id="esClient" cluster-nodes="${es.cluster.nodes}"
12                                     client-transport-ignore-cluster-name="true"/>
13 
14     <!-- spring data elasticsearch DAO 必须依赖 elasticsearchTemplate  -->
15     <bean id="esTemplate"
16           class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
17         <constructor-arg name="client" ref="esClient"/>
18     </bean>
19 
20     <!-- 扫描DAO包 自动创建实现 -->
21     <elasticsearch:repositories base-package="com.lcb.soa.search.item.repository"
22                                 elasticsearch-template-ref="esTemplate"/>
23 
24 </beans>

index-item-setting.json

 1 {
 2     "_all": {"enabled": true, "analyzer": "ik_smart"},
 3     "include_in_all": false,
 4     "properties": {
 5         "id": {
 6             "type": "long"
 7         },
 8         "oeNumber": {
 9             "type": "text",
10             "analyzer": "ik_smart",
11             "include_in_all": true
12         },
13         "itemName": {
14             "type": "text",
15             "analyzer": "ik_smart",
16             "include_in_all": true
17         },
18         "manufacturerId": {
19             "type": "integer"
20         },
21         "manufacturerName": {
22             "type": "text",
23             "analyzer": "ik_smart"
24         },
25         "categoryId": {
26             "type": "long"
27         },
28         "categoryName": {
29             "type": "text",
30             "analyzer": "ik_smart"
31         },
32         "createdTime": {
33             "type": "date",
34             "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
35         },
36         "updatedTime": {
37             "type": "date",
38             "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
39         },
40         "keywords": {
41             "type": "text",
42             "analyzer": "uc_keyword_analyzer",
43             "include_in_all": true,
44             "fields": {
45                 "fuzzy": {
46                     "type": "text",
47                     "analyzer": "ik_smart"
48                 },
49                 "suggest": {
50                     "type": "completion",
51                     "analyzer": "ik_smart"
52                 },
53                 "pinyin": {
54                     "type": "completion",
55                     "analyzer": "pinyin_analyzer"
56                 }
57             }
58         },
59         "pinyinKeywords":{
60             "type":"text",
61             "analyzer": "pinyin_analyzer"
62         }
63     }
64 }

index-type-originalItem-mapping.json

 1 {
 2     "_all": {"enabled": true, "analyzer": "ik_smart"},
 3     "include_in_all": false,
 4     "properties": {
 5         "id": {
 6             "type": "long"
 7         },
 8         "oeNumber": {
 9             "type": "text",
10             "analyzer": "ik_smart",
11             "include_in_all": true
12         },
13         "itemName": {
14             "type": "text",
15             "analyzer": "ik_smart",
16             "include_in_all": true
17         },
18         "manufacturerId": {
19             "type": "integer"
20         },
21         "manufacturerName": {
22             "type": "text",
23             "analyzer": "ik_smart"
24         },
25         "categoryId": {
26             "type": "long"
27         },
28         "categoryName": {
29             "type": "text",
30             "analyzer": "ik_smart"
31         },
32         "createdTime": {
33             "type": "date",
34             "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
35         },
36         "updatedTime": {
37             "type": "date",
38             "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
39         },
40         "keywords": {
41             "type": "text",
42             "analyzer": "uc_keyword_analyzer",
43             "include_in_all": true,
44             "fields": {
45                 "fuzzy": {
46                     "type": "text",
47                     "analyzer": "ik_smart"
48                 },
49                 "suggest": {
50                     "type": "completion",
51                     "analyzer": "ik_smart"
52                 },
53                 "pinyin": {
54                     "type": "completion",
55                     "analyzer": "pinyin_analyzer"
56                 }
57             }
58         },
59         "pinyinKeywords":{
60             "type":"text",
61             "analyzer": "pinyin_analyzer"
62         }
63     }
64 }


IndexVo

 1 @Getter
 2 @Setter
 3 @SuppressWarnings("serial")
 4 public class IndexAliasVO implements Serializable
 5 {
 6     String alias;
 7     String currentIndex;
 8     List<String> candidateIndices;
 9     
10     public String getOtherIndex()
11     {
12         return candidateIndices.get(0);
13     }
14 }

 

 

 1     public String switchIndexAlias() {
 2         IndexAliasVO indexAlias = originalItemSearchService.getIndexAlias();
 3         String newIndex = indexAlias.getOtherIndex();
 4         originalItemSearchService.resetIndex(newIndex);
 5         importDoc(newIndex);
 6         originalItemSearchService.switchIndexAlias(newIndex);
 7         return newIndex;
 8     }
 9 
10 
11     private void importDoc(String newIndex) {
12 
13         Long lastId = 0L;
14 
15         while (true) {
16 
17             List<ItOriginalItemInformation> itemInfo = getItemInfoLimit(ITEM_LIMIT_END, lastId);
18 
19             if (itemInfo.isEmpty()) {
20                 break;
21             }
22 
23             List<OriginalItemDoc> itemDocList = parseDoc(itemInfo);
24 
25             if (!originalItemSearchService.indexItems(newIndex, itemDocList)) {
26                 throw new ServiceException("批量将商品数据导入ES索引库失败");
27             }
28 
29             if (itemInfo.size() < ITEM_LIMIT_END) {
30                 break;
31             }
32 
33             lastId = itemInfo.get(itemInfo.size() - 1).getId();
34         }
35 
36     }
37 
38 
39    private List<ItOriginalItemInformation> getItemInfoLimit(int endIndex, Long maxId) {
40         ItOriginalItemInformationExample e = new ItOriginalItemInformationExample();
41         e.setLimitStart(0);
42         e.setLimitEnd(endIndex);
43         e.setOrderByClause("id ASC ");
44         e.createCriteria().andIdGreaterThan(maxId);
45         List<ItOriginalItemInformation> items = itOriginalItemInformationMapper.selectByExample(e);
46         return items;
47     }

 

ES默认服务端口9300

外部通信端口9200

head插件端口9100

 

分享一个较全的使用   https://www.cnblogs.com/leeSmall/p/9218779.html

 

标签:search,indexName,String,text,org,笔记,使用,import,ES
来源: https://www.cnblogs.com/joke0406/p/10532668.html