1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
| package com.yeshimin.yeahboot.merchant.service;
import cn.hutool.core.util.StrUtil; import com.manticoresearch.client.ApiClient; import com.manticoresearch.client.ApiException; import com.manticoresearch.client.api.IndexApi; import com.manticoresearch.client.api.SearchApi; import com.manticoresearch.client.model.*; import com.yeshimin.yeahboot.common.common.consts.FulltextTableConsts; import com.yeshimin.yeahboot.common.common.properties.ManticoreSearchProperties; import com.yeshimin.yeahboot.data.domain.entity.ProductSpuEntity; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service;
import java.util.HashMap; import java.util.List; import java.util.Map;
@Slf4j @Service @RequiredArgsConstructor public class FullTextSearchService {
private final ManticoreSearchProperties properties;
public void syncProduct(ProductSpuEntity productSpu, List<String> skuNames, List<String> skuSpecs, boolean update) { Map<String, Object> doc = new HashMap<>(); doc.put("id", productSpu.getId()); doc.put("product_name", productSpu.getName()); doc.put("sku_names", StrUtil.join(" ", skuNames)); doc.put("sku_specs", StrUtil.join(" ", skuSpecs)); doc.put("sales", productSpu.getSales()); doc.put("sku_min_price", productSpu.getMinPrice()); doc.put("sku_max_price", productSpu.getMaxPrice());
if (update) { this.replace(FulltextTableConsts.PRODUCT, doc); } else { this.insert(FulltextTableConsts.PRODUCT, doc); } }
public SearchResponse search(String tableName, String queryString) { ApiClient client = this.newClient();
SearchQuery searchQuery = new SearchQuery(); searchQuery.setQueryString(queryString);
SearchRequest searchRequest = new SearchRequest(); searchRequest.table(tableName).query(searchQuery);
SearchApi searchApi = new SearchApi(client);
try { SearchResponse response = searchApi.search(searchRequest); log.debug("Search response: {}", response); return response; } catch (ApiException e) { this.printError(e); throw new RuntimeException(e); } }
public SuccessResponse insert(String tableName, Map<String, Object> doc) { ApiClient client = this.newClient();
Long id = Long.parseLong(doc.remove("id").toString());
InsertDocumentRequest request = new InsertDocumentRequest(); request.table(tableName).id(id).doc(doc);
IndexApi indexApi = new IndexApi(client); try { SuccessResponse response = indexApi.insert(request); log.debug("Insert response: {}", response); return response; } catch (ApiException e) { this.printError(e); throw new RuntimeException(e); } }
public SuccessResponse replace(String tableName, Map<String, Object> doc) { ApiClient client = this.newClient();
Long id = Long.parseLong(doc.remove("id").toString());
InsertDocumentRequest request = new InsertDocumentRequest(); request.table(tableName).id(id).doc(doc);
IndexApi indexApi = new IndexApi(client); try { SuccessResponse response = indexApi.replace(request); log.debug("Replace response: {}", response); return response; } catch (ApiException e) { this.printError(e); throw new RuntimeException(e); } }
public UpdateResponse update(String tableName, Map<String, Object> doc) { ApiClient client = this.newClient();
Long id = Long.parseLong(doc.remove("id").toString());
UpdateDocumentRequest updateRequest = new UpdateDocumentRequest(); updateRequest.table(tableName).id(id).doc(doc);
IndexApi indexApi = new IndexApi(client); try { UpdateResponse response = indexApi.update(updateRequest); log.debug("Update response: {}", response); return response; } catch (ApiException e) { this.printError(e); throw new RuntimeException(e); } }
public DeleteResponse deleteIndex(String tableName, Long id) { ApiClient client = this.newClient();
DeleteDocumentRequest deleteRequest = new DeleteDocumentRequest(); deleteRequest.table(tableName).id(id);
IndexApi indexApi = new IndexApi(client); try { DeleteResponse response = indexApi.delete(deleteRequest); log.debug("Delete response: {}", response); return response; } catch (ApiException e) { this.printError(e); throw new RuntimeException(e); } }
private ApiClient newClient() { ApiClient client = new ApiClient(); client.setBasePath(properties.getBasePath()); return client; }
private void printError(ApiException e) { log.error("Exception when calling Api function: {}, Status code: {}, Reason: {}, Response headers: {}", e.getMessage(), e.getCode(), e.getResponseBody(), e.getResponseHeaders()); } }
|