일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 차트
- Elasticsearch
- high level client
- 900gle
- docker
- Python
- aggregation
- sort
- Mac
- license delete
- aggs
- flask
- licence delete curl
- Kafka
- zip 암호화
- API
- zip 파일 암호화
- TensorFlow
- ELASTIC
- springboot
- Java
- License
- token filter test
- plugin
- matplotlib
- query
- analyzer test
- 파이썬
- Test
- MySQL
Archives
- Today
- Total
개발잡부
[900] Java High Level REST Client Query 3 본문
반응형
인덱스 생성 setting, mapping Class
너무 복잡스럽네.. 나중에 index template 으로 변경해야 할듯..
package com.bbongdoo.doo.apis;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import java.io.IOException;
public class IndexGoodsApi {
public static CreateIndexRequest createIndex(String indexName) throws IOException {
CreateIndexRequest request = new CreateIndexRequest(indexName);
request.settings(Settings.builder()
.put("index.number_of_shards", 3)
.put("index.number_of_replicas", 0)
);
XContentBuilder settingBuilder = XContentFactory.jsonBuilder()
.startObject()
.field("number_of_shards", 3)
.field("number_of_replicas", 0)
.startObject("analysis")
.startObject("tokenizer")
.startObject("doo-nori-tokenizer")
.field("type", "nori_tokenizer")
.field("decompound_mode", "mixed")
.field("user_dictionary", "user_dictionary.txt")
.endObject()
.endObject()
.startObject("analyzer")
.startObject("jamo-analyzer")
.field("type", "custom")
.field("tokenizer", "standard")
.array("filter", new String[]{
"doo-jamo"
}
)
.endObject()
.startObject("chosung-analyzer")
.field("type", "custom")
.field("tokenizer", "standard")
.array("filter", new String[]{
"doo-chosung"
}
)
.endObject()
.startObject("doo-nori-analyzer")
.field("type", "custom")
.field("tokenizer", "doo-nori-tokenizer")
.array("filter", new String[]{
"doo-nori-posfilter",
"nori_readingform",
"doo-synonym-filter",
"doo-stop-filter"
}
)
.endObject()
.endObject()
.startObject("filter")
.startObject("doo-nori-posfilter")
.field("type", "nori_part_of_speech")
.array("stoptaags", new String[]{
"E", "IC", "J", "MAG", "MM", "NA", "NR", "SC",
"SE", "SF", "SH", "SL", "SN", "SP", "SSC", "SSO",
"SY", "UNA", "UNKNOWN", "VA", "VCN", "VCP", "VSV",
"VV", "VX", "XPN", "XR", "XSA", "XSN", "XSV"
}
)
.endObject()
.startObject("doo-synonym-filter")
.field("type", "synonym")
.field("synonyms_path", "synonymsFilter.txt")
.endObject()
.startObject("doo-stop-filter")
.field("type", "stop")
.field("stopwords_path", "stopFilter.txt")
.endObject()
.endObject()
.endObject()
.endObject();
request.settings(settingBuilder);
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
{
builder.startObject("properties");
{
builder.startObject("id");
{
builder.field("type", "long");
}
builder.endObject();
builder.startObject("name");
{
builder.field("type", "text");
builder.startObject("fields");
builder.startObject("nori");
builder.field("type", "text");
builder.field("analyzer", "doo-nori-analyzer");
builder.endObject();
builder.startObject("jamo");
builder.field("type", "text");
builder.field("analyzer", "jamo-analyzer");
builder.endObject();
builder.startObject("chosung");
builder.field("type", "text");
builder.field("analyzer", "chosung-analyzer");
builder.endObject();
builder.startObject("keyword");
builder.field("type", "keyword");
builder.endObject();
builder.endObject();
}
builder.endObject();
builder.startObject("category");
{
builder.field("type", "text");
builder.startObject("fields");
builder.startObject("keyword");
builder.field("type", "keyword");
builder.endObject();
builder.endObject();
}
builder.endObject();
builder.startObject("category1");
{
builder.field("type", "keyword");
}
builder.endObject();
builder.startObject("category2");
{
builder.field("type", "keyword");
}
builder.endObject();
builder.startObject("category3");
{
builder.field("type", "keyword");
}
builder.endObject();
builder.startObject("category4");
{
builder.field("type", "keyword");
}
builder.endObject();
builder.startObject("category5");
{
builder.field("type", "keyword");
}
builder.endObject();
builder.startObject("price");
{
builder.field("type", "long");
}
builder.endObject();
builder.startObject("brand");
{
builder.field("type", "keyword");
}
builder.endObject();
builder.startObject("keyword");
{
builder.field("type", "keyword");
}
builder.endObject();
builder.startObject("image");
{
builder.field("type", "text");
}
builder.endObject();
builder.startObject("popular");
{
builder.field("type", "integer");
builder.field("null_value", "1");
}
builder.endObject();
builder.startObject("weight");
{
builder.field("type", "float");
}
builder.endObject();
builder.startObject("feature_vector");
{
builder.field("type", "dense_vector");
builder.field("dims", 512);
}
builder.endObject();
builder.startObject("type");
{
builder.field("type", "keyword");
}
builder.endObject();
builder.startObject("created_time");
{
builder.field("type", "date");
builder.field("format", "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis||strict_date_hour_minute_second_millis||strict_date_optional_time");
}
builder.endObject();
builder.startObject("updated_time");
{
builder.field("type", "date");
builder.field("format", "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis||strict_date_hour_minute_second_millis||strict_date_optional_time");
}
builder.endObject();
}
builder.endObject();
}
builder.endObject();
request.mapping(builder);
return request;
}
}
상품검색 Service
package com.bbongdoo.doo.service;
import com.bbongdoo.doo.component.TextEmbedding;
import com.bbongdoo.doo.dto.TextEmbeddingDTO;
import com.bbongdoo.doo.model.response.CommonResult;
import com.google.common.base.Functions;
import lombok.RequiredArgsConstructor;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.lucene.search.function.FunctionScoreQuery;
import org.elasticsearch.common.lucene.search.function.ScoreFunction;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.*;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptType;
import org.elasticsearch.script.mustache.SearchTemplateRequest;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.*;
import static java.util.Collections.emptyMap;
@Service
@RequiredArgsConstructor
public class GoodsService {
private final ResponseService responseService;
private final RestHighLevelClient client;
private final String ALIAS = "goods";
public CommonResult getProducts(String searchWord) {
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices(ALIAS);
try {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
Vector<Double> vectors = TextEmbedding.getVector(
TextEmbeddingDTO.builder()
.tensorApiUrl("http://localhost:5000/vectors")
.keyword(searchWord).build()
);
// String[] includeFields = new String[]{"name", "category"};
String[] includeFields = new String[]{};
String[] excludeFields = new String[]{"feature_vector"};
searchSourceBuilder.fetchSource(includeFields, excludeFields);
Map<String, Object> map = new HashMap<>();
map.put("query_vector", vectors);
FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(
QueryBuilders.multiMatchQuery(searchWord, "name", "category"),
new FunctionScoreQueryBuilder.FilterFunctionBuilder[]{
new FunctionScoreQueryBuilder.FilterFunctionBuilder(
new ScriptScoreFunctionBuilder(
new Script(
ScriptType.INLINE,
Script.DEFAULT_SCRIPT_LANG,
"cosineSimilarity(params.query_vector, 'feature_vector') * doc['weight'].value * doc['popular'].value / doc['name.keyword'].length + doc['category.keyword'].length",
map)
)
)
}
);
searchSourceBuilder.query(functionScoreQueryBuilder);
searchSourceBuilder.size(8);
searchRequest.source(searchSourceBuilder);
System.out.println(searchRequest.source());
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
List<Map<String, Object>> returnValue = new ArrayList<>();
SearchHit[] results = searchResponse.getHits().getHits();
Arrays.stream(results).forEach(hit -> {
Map<String, Object> result = hit.getSourceAsMap();
System.out.println(result.get("name"));
returnValue.add(result);
});
return responseService.getListResult(returnValue);
} catch (IOException e) {
e.printStackTrace();
}
return new CommonResult();
}
}
{
"size":8,
"query":{
"function_score":{
"query":{
"multi_match":{
"query":"나이키 남성 운동화",
"fields":[
"category^1.0",
"name^1.0"
],
"type":"best_fields",
"operator":"OR",
"slop":0,
"prefix_length":0,
"max_expansions":50,
"zero_terms_query":"NONE",
"auto_generate_synonyms_phrase_query":true,
"fuzzy_transpositions":true,
"boost":1.0
}
},
"functions":[
{
"filter":{
"match_all":{
"boost":1.0
}
},
"script_score":{
"script":{
"source":"cosineSimilarity(params.query_vector, 'feature_vector') * doc['weight'].value * doc['popular'].value / doc['name.keyword'].length + doc['category.keyword'].length",
"lang":"painless",
"params":{
"query_vector":[
-0.01783534698188305,
0.028291134163737297,
-0.051035501062870026,
0.054820429533720016,
0.027929922565817833,
0.017169641330838203,
0.011979538016021252,
-0.05850313603878021,
0.044365379959344864,
0.009025814011693,
-0.03733707591891289,
-0.10298466682434082,
0.02317744866013527,
-0.07803352922201157,
-0.020095884799957275,
-0.008477125316858292,
0.025829441845417023,
-0.038644202053546906,
-0.04253268241882324,
-0.05130152776837349,
-0.03544012829661369,
0.009226053021848202,
0.07006046175956726,
0.01600527949631214,
0.033890847116708755,
-0.046707652509212494,
-0.03490631282329559,
0.013379461131989956,
-0.028714260086417198,
-0.01032843068242073,
0.029945025220513344,
0.01121340598911047,
-0.00766092911362648,
-0.043811339884996414,
0.011022261343896389,
0.032461993396282196,
-0.05491628125309944,
-0.04712033271789551,
0.08782849460840225,
-0.02819782681763172,
-0.017050541937351227,
-0.06452379375696182,
0.05998484045267105,
-0.02132139541208744,
-0.019660331308841705,
0.07570209354162216,
0.03409837186336517,
-0.013013171032071114,
0.010583481751382351,
0.06911060214042664,
-0.018828244879841805,
-0.01857609860599041,
0.021606246009469032,
-0.04790184646844864,
-0.02281707152724266,
0.061350952833890915,
-0.048473190516233444,
0.04262073338031769,
-0.002188287442550063,
-0.030631892383098602,
-0.04041444510221481,
-0.009986499324440956,
0.04439890757203102,
0.06143871322274208,
-0.07807828485965729,
0.06688584387302399,
-0.014343985356390476,
-0.04246576502919197,
0.06150832399725914,
-0.03237711265683174,
0.058323077857494354,
-0.025104200467467308,
0.038587383925914764,
0.006657795049250126,
-0.05576033517718315,
0.04625697806477547,
0.02731202356517315,
0.039105966687202454,
-0.014960955828428268,
-0.023232806473970413,
-0.06607051193714142,
-0.08393224328756332,
-0.04015430808067322,
-0.011636581271886826,
0.01587875932455063,
0.029612351208925247,
0.02600022405385971,
-0.05479007586836815,
-0.06921359151601791,
0.01765221729874611,
0.021196676418185234,
-0.01816057227551937,
0.030068017542362213,
-0.06171903759241104,
0.016738448292016983,
0.0523839145898819,
-0.032775819301605225,
-0.028141705319285393,
-0.042401671409606934,
-0.030543366447091103,
0.05307570844888687,
-0.025897487998008728,
0.038627058267593384,
0.07968731224536896,
-0.008310479111969471,
-0.05887969955801964,
-0.06645319610834122,
0.06675948947668076,
-0.045124296098947525,
-0.042290717363357544,
-0.013654535636305809,
0.052231356501579285,
0.03062576800584793,
0.0275048166513443,
-0.008610906079411507,
-0.05523301661014557,
0.060493018478155136,
-0.040937576442956924,
0.03592194616794586,
0.026478486135601997,
-0.08006712794303894,
-0.015282375738024712,
-0.057419825345277786,
-0.07199839502573013,
-0.09131123125553131,
0.06324076652526855,
-0.010039282031357288,
0.01268684957176447,
0.002484845696017146,
-0.02940390259027481,
-0.07914348691701889,
-0.030601706355810165,
-0.0805981382727623,
0.04624423384666443,
0.033734407275915146,
0.041221559047698975,
0.008298572152853012,
0.020846804603934288,
-0.04406823217868805,
-0.030515696853399277,
0.017557485029101372,
0.004726874642074108,
-0.010652369819581509,
0.05359775945544243,
0.005503631662577391,
0.019552107900381088,
0.0760716125369072,
-0.057036396116018295,
-0.03059239126741886,
-0.03450867533683777,
-0.05008009076118469,
-0.002218289067968726,
0.025835255160927773,
-0.08084441721439362,
0.02783598005771637,
-0.004801533650606871,
-0.04888444393873215,
0.012859920039772987,
0.006441579200327396,
0.0111191775649786,
-0.06802095472812653,
0.06256851553916931,
-0.0036608283407986164,
0.031122487038373947,
0.11587122082710266,
0.05910079926252365,
-7.537452038377523E-4,
-0.06773117184638977,
0.024877198040485382,
-0.005462797824293375,
-0.060681503266096115,
-0.03033784031867981,
-0.06249096244573593,
0.02812526561319828,
-0.025594163686037064,
-0.021147441118955612,
0.03743883594870567,
5.748088005930185E-4,
-0.01924670673906803,
0.0030797277577221394,
-0.027562135830521584,
-0.006474004592746496,
0.023891866207122803,
-0.024531953036785126,
0.04025788977742195,
-0.07262440770864487,
-0.050749003887176514,
0.030679568648338318,
-0.06508737802505493,
0.03920619562268257,
-0.023163050413131714,
0.05318004637956619,
-0.04039723426103592,
0.008136074990034103,
-0.08745787292718887,
0.019231945276260376,
0.11376377195119858,
0.052507538348436356,
0.03611056134104729,
-0.03525193780660629,
0.02564230002462864,
-0.05084886774420738,
-0.05223260819911957,
-0.0019226045114919543,
-0.03266549110412598,
-0.01512992661446333,
0.00866792257875204,
-0.018258634954690933,
0.09033113718032837,
-0.005090631078928709,
0.03951198235154152,
0.01415377389639616,
-0.013442199677228928,
0.007249172776937485,
0.04827774316072464,
-0.005285612307488918,
0.024326294660568237,
-0.07627052068710327,
0.03834182769060135,
-0.05790305510163307,
0.05020635947585106,
-0.01807631365954876,
-0.007658456452190876,
-0.006444056984037161,
0.0759313702583313,
-0.046016525477170944,
-0.060634858906269073,
0.004257454536855221,
0.015468151308596134,
0.077139712870121,
-0.03647364675998688,
0.02071676217019558,
-0.01788727566599846,
-0.006531927268952131,
0.04345313459634781,
-0.0700472742319107,
-0.03440530598163605,
-0.05666129291057587,
0.05075377970933914,
0.063567154109478,
0.014754833653569221,
-0.040222711861133575,
-0.010176907293498516,
-0.0190846249461174,
0.0027531483210623264,
-0.02631864883005619,
-0.08312750607728958,
0.06420870870351791,
-0.004327443893998861,
0.04925470054149628,
-0.004224173258990049,
-0.04223926365375519,
0.04616564139723778,
-0.028053782880306244,
-0.017959242686629295,
-0.05146334320306778,
0.018302710726857185,
0.011149038560688496,
-0.06056154891848564,
-0.06611237674951553,
0.07526402175426483,
-0.005474192090332508,
0.06838629394769669,
0.048831019550561905,
-0.056410837918519974,
0.016849460080266,
0.01601099781692028,
0.03554239496588707,
0.06393233686685562,
-0.06488537788391113,
0.05639263987541199,
-0.018783170729875565,
0.07832738012075424,
-0.0700223445892334,
-0.03872187063097954,
-0.017148694023489952,
-0.08718454837799072,
-0.05375022813677788,
-0.029187707230448723,
-0.003949631471186876,
-0.008052702061831951,
-0.005827619228512049,
0.050092343240976334,
-0.06038618087768555,
-0.05776825547218323,
-0.021324174478650093,
0.06058727949857712,
0.07819847017526627,
0.00656857481226325,
0.004850942641496658,
0.03215176239609718,
0.054927926510572433,
-0.009101991541683674,
0.0016856255242601037,
0.008569959551095963,
-0.05747344717383385,
0.020043140277266502,
-0.05439617112278938,
0.008628442883491516,
-0.01120012253522873,
0.08674459904432297,
0.06713598221540451,
-0.04398341476917267,
0.00842932891100645,
0.0828486904501915,
-0.01904502883553505,
-0.021480543538928032,
-0.06565101444721222,
-0.013197344727814198,
0.04555181413888931,
-0.034539565443992615,
-0.009414632804691792,
-0.05564870685338974,
0.07357703894376755,
0.010261047631502151,
0.06214478239417076,
-0.068907730281353,
0.06337913125753403,
-0.01842559315264225,
-0.08013724535703659,
-0.0017635372933000326,
0.01618683524429798,
0.02291957288980484,
0.020204320549964905,
-0.0295335054397583,
-0.05076831579208374,
-0.0368347242474556,
0.022316593676805496,
0.03472404181957245,
-0.02249951660633087,
0.10348224639892578,
-0.04131448641419411,
-0.02858748286962509,
0.060875147581100464,
-0.06776422262191772,
-0.029975812882184982,
-0.09226447343826294,
-0.03163890540599823,
0.015190225094556808,
0.006155380513519049,
0.009056687355041504,
-0.06455060094594955,
0.01459125429391861,
-0.03637377545237541,
-0.013699883595108986,
-0.06002623587846756,
0.09297078847885132,
-0.01978960447013378,
0.025375010445713997,
0.03340047970414162,
-0.0779186263680458,
-0.005654047708958387,
-0.03397609665989876,
-0.03158871456980705,
0.06985488533973694,
0.10523305833339691,
-0.018389001488685608,
0.05042172595858574,
-0.04038520157337189,
0.013389055617153645,
0.02053850144147873,
0.03769739717245102,
0.040632061660289764,
-0.02480444684624672,
0.012078494764864445,
-0.025408677756786346,
-0.017464905977249146,
0.048615653067827225,
0.037303727120161057,
0.00485125370323658,
0.0057110656052827835,
0.030486444011330605,
0.016826502978801727,
-0.02103612944483757,
-0.07064355164766312,
0.005326008889824152,
0.039049241691827774,
1.5932407404761761E-4,
-0.11488569527864456,
0.010769741609692574,
0.04596007987856865,
-0.02675880864262581,
-0.021509436890482903,
-0.023381832987070084,
-0.008561831898987293,
-0.06839767843484879,
-0.005717229563742876,
-0.02219361811876297,
-0.005433613900095224,
0.02138259820640087,
-0.044919684529304504,
0.023178424686193466,
0.006426565814763308,
0.016503291204571724,
0.02336784452199936,
-0.010466599836945534,
-0.02711222879588604,
0.029232213273644447,
0.07771873474121094,
0.09354236721992493,
-0.017144929617643356,
0.013840984553098679,
0.043031781911849976,
-0.06660119444131851,
0.022176649421453476,
-0.011803872883319855,
-0.059219878166913986,
0.019354386255145073,
0.04323773831129074,
-0.05551077798008919,
0.09132117033004761,
0.02272982895374298,
0.007490749936550856,
-0.02416570670902729,
0.051705215126276016,
0.007749069947749376,
-0.009509134106338024,
-0.009893808513879776,
0.030548686161637306,
0.020791849121451378,
0.04029146209359169,
-0.01979292929172516,
-0.04630599543452263,
-0.0630963146686554,
0.06564517319202423,
0.04045657441020012,
-0.0888107493519783,
-0.050061676651239395,
-0.04721546173095703,
0.01405966468155384,
-0.055679433047771454,
-0.040541958063840866,
0.005533390678465366,
-2.4519889848306775E-5,
-0.03881422057747841,
0.0657784640789032,
0.08573056012392044,
0.07639463990926743,
0.05013161152601242,
0.02805153653025627,
-0.029026102274656296,
0.036410585045814514,
-0.012257304973900318,
-0.008881817571818829,
-0.0024676595348864794,
-0.019361622631549835,
-0.007612998131662607,
-0.023158639669418335,
-0.0714624673128128,
-0.032874695956707,
0.09102648496627808,
-0.01825752481818199,
0.04092354327440262,
0.04671444371342659,
-0.044041525572538376,
0.044264331459999084,
0.012870525009930134,
-0.03877687081694603,
0.029036875814199448,
0.018522486090660095,
-0.02240757830440998,
0.04270267114043236,
-0.05230139195919037,
0.01732807420194149,
-0.0844956710934639,
-0.026445137336850166,
0.09322874993085861,
-0.0160403773188591,
-0.043467551469802856,
0.01106617134064436,
-0.07462729513645172,
-0.039132680743932724,
-0.0085717448964715,
-0.04183008894324303,
0.025934385135769844,
0.0034887059591710567,
0.058674998581409454,
0.044816792011260986,
0.03984472155570984,
0.024097049608826637,
-0.039036933332681656,
0.0021058996208012104,
-0.016108140349388123,
0.003467126749455929,
0.06161462888121605,
0.06478812545537949,
-0.0223933607339859,
0.0013947086408734322,
0.03981046751141548,
-0.011152718216180801,
0.04979434236884117,
-0.06996355205774307,
0.04421763867139816,
-0.019177554175257683,
0.08038360625505447,
0.030324578285217285,
-0.028664056211709976,
0.04545765370130539,
-0.02127218060195446,
-0.04266335070133209,
0.01528213731944561,
0.03942035138607025,
0.05611054599285126,
0.05635473504662514,
-0.03293705731630325,
0.05270226299762726,
-0.055361323058605194,
0.01769181527197361,
0.025039086118340492,
0.0030021152924746275,
0.0581924133002758,
0.052919354289770126
]
}
}
}
}
],
"score_mode":"multiply",
"max_boost":3.4028235E38,
"boost":1.0
}
},
"_source":{
"includes":[
],
"excludes":[
"feature_vector"
]
}
}
반응형
'900gle shopping > api' 카테고리의 다른 글
[900gle] Java High Level REST Client Query 2 (0) | 2022.01.26 |
---|---|
[900gle] Search Template (0) | 2022.01.25 |
[900] Java High Level REST Client Query (0) | 2022.01.24 |
[900] Search API (0) | 2022.01.19 |
[900gle] Text embedding API (0) | 2022.01.19 |
Comments