Java与ElasticSerach的整合
生活随笔
收集整理的這篇文章主要介紹了
Java与ElasticSerach的整合
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
-
通過es的mapping數據結構
-
定義數據庫中的表的結構的定義,通過mapping來控制索引存儲數據的設置
-
定義Index下的字段名(Field Name)
-
定義字段的類型,比如數值型、字符串型、布爾型等
-
定義倒排索引相關的配置,比如documentId、記錄position、打分等
-
-
數據類型
-
-
創建mapping
Java與ElasticSerach的整合
2.PmsSearchSkuInf與ElasticSearch中的數據結構對應
public class PmsSearchSkuInfo implements Serializable{@Idprivate String id;private String skuName;private String skuDesc;private String catalog3Id;private BigDecimal price;private String skuDefaultImg;private double hostScore;private String productId;private List<PmsSkuAttrValue> skuAttrValueList;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getSkuName() {return skuName;}public void setSkuName(String skuName) {this.skuName = skuName;}public String getSkuDesc() {return skuDesc;}public void setSkuDesc(String skuDesc) {this.skuDesc = skuDesc;}public String getCatalog3Id() {return catalog3Id;}public void setCatalog3Id(String catalog3Id) {this.catalog3Id = catalog3Id;}public BigDecimal getPrice() {return price;}public void setPrice(BigDecimal price) {this.price = price;}public String getSkuDefaultImg() {return skuDefaultImg;}public void setSkuDefaultImg(String skuDefaultImg) {this.skuDefaultImg = skuDefaultImg;}public double getHostScore() {return hostScore;}public void setHostScore(double hostScore) {this.hostScore = hostScore;}public String getProductId() {return productId;}public void setProductId(String productId) {this.productId = productId;}public List<PmsSkuAttrValue> getSkuAttrValueList() {return skuAttrValueList;}public void setSkuAttrValueList(List<PmsSkuAttrValue> skuAttrValueList) {this.skuAttrValueList = skuAttrValueList;} }3.SkuService.java
public interface SkuService {List<PmsSkuInfo> getAllSku(); }4.PmsSkuInfoMapper實現mybatis的tk.Mapper
public interface PmsSkuInfoMapper extends Mapper<PmsSkuInfo>{ }5.SkuServiceImpl.java
@Service public class SkuServiceImpl implements SkuService{@AutowiredPmsSkuInfoMapper pmsSkuInfoMapper;@Overridepublic List<PmsSkuInfo> getAllSku() {List<PmsSkuInfo> pmsSkuInfos = pmsSkuInfoMapper.selectAll();for (PmsSkuInfo pmsSkuInfo : pmsSkuInfos) {String skuId = pmsSkuInfo.getId();PmsSkuAttrValue pmsSkuAttrValue=new PmsSkuAttrValue();pmsSkuAttrValue.setSkuId(skuId);List<PmsSkuAttrValue> pmsSkuAttrValues = pmsSkuAttrValueMapper.select(pmsSkuAttrValue);pmsSkuInfo.setSkuAttrValueList(pmsSkuAttrValues);}return pmsSkuInfos;} }6.GmallSearchServiceApplication
public class GmallSearchServiceApplication {@ReferenceSkuService skuService; //查詢MySQL@AutowiredJestClient jestClient;@Testpublic void contextLoads() throws IOException {//查詢MySQL數據庫List<PmsSkuInfo> pmsSkuInfoList=new ArrayList<>();pmsSkuInfoList=skuService.getAllSku();//轉化為es的數據結構List<PmsSearchSkuInfo> pmsSearchSkuInfoList=new ArrayList<>();for (PmsSkuInfo pmsSkuInfo : pmsSkuInfoList) {PmsSearchSkuInfo pmsSearchSkuInfo = new PmsSearchSkuInfo();BeanUtils.copyProperties(pmsSkuInfo,pmsSearchSkuInfo);pmsSearchSkuInfoList.add(pmsSearchSkuInfo);}//導入esfor (PmsSearchSkuInfo pmsSearchSkuInfo : pmsSearchSkuInfoList) {Index put = new Index.Builder(pmsSearchSkuInfo).index("gmall0105").type("PmsSkuInfo").id(pmsSearchSkuInfo.getId()).build();jestClient.execute(put);}}}7.在elastaicSearch中查詢
//先過濾后查詢一個條件中交集 GET gmall0105/PmsSkuInfo/_search {"query": {"bool": {"filter":[{"term": {"skuAttrValueList.valueId": "39"}}, {"term": {"skuAttrValueList.valueId": "43"}}], "must": [{"match": {"skuName": "Apple"}}]}} }ElasticSearch并集與交集查詢
//并集是一個數組 {terms:{"skuAttrValueList.valueId":["39","40","41"]}} //交際是一個具體值 {term:{"skuArrtValueList.valueId":"39"}}
查詢API
總結
以上是生活随笔為你收集整理的Java与ElasticSerach的整合的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 搜索引擎ElasticSearch
- 下一篇: Dubbo启动,调用方法失败【问题:调用