时间:2023-03-20 06:18:01 | 来源:电子商务
时间:2023-03-20 06:18:01 来源:电子商务
建立索引mappingPUT _index_template/index_search_product_template{ "version": 3, "priority": 200, "template": { "settings": { "index": { "number_of_shards": "5", "refresh_interval": "300s" } }, "mappings": { "properties": { "imgs": { "type": "keyword" }, "majorProdProp": { "type": "keyword" }, "keywords": { "analyzer": "whitespace", "type": "text" }, "productAttrValue": { "analyzer": "ik_smart", "type": "text" }, "productId": { "type": "long" }, "userSource": { "type": "keyword" }, "companyAddr": { "analyzer": "ik_smart", "type": "text" }, "source": { "type": "keyword" }, "categoryName": { "type": "keyword" }, "productName": { "analyzer": "ik_smart", "type": "text" }, "productAttr": { "analyzer": "ik_smart", "type": "text" }, "productZoneId": { "type": "long" }, "calCeil": { "type": "keyword" }, "minordernum": { "type": "integer" }, "compId": { "type": "long" }, "isPromote": { "type": "integer" }, "price": { "type": "long" }, "productStar": { "type": "integer" }, "shopId": { "type": "long" }, "categoryId": { "type": "keyword" }, "deliveryProvinceId": { "type": "integer" } } } }, "index_patterns": [ "index_product_*" ], "composed_of": [ "common_integer_id_template", "common_long_id_template" ]}
构造查询DSLGET index_product_payed/_search/{ "query": { "function_score": { "query": { "bool": { "should": [ { "term": { "productName": { "value": "灯泡", "boost": 300 } } } ] } }, "functions": [ { "field_value_factor": { "field": "productScore", "factor": 1, "modifier": "sqrt", "missing": 1 }} ], "boost_mode": "multiply" } }, "from": 0, "size": 0, "explain" : false, "profile": false, "aggs": { "group_by_userId": { "terms": { "field": "userId", "size": 400, "order": { "maxPrice": "desc" } } ,"aggregations": { "onlyOne": { "top_hits": { "size": 1, "sort": [ { "_score":{ "order": "desc" } } ], "explain" : false, "_source": { "includes": ["_id"","companyName","_score","price","isPromote","productScore"] } } }, "maxPrice":{ "max": { "script": { "source": "_score" } } } } } }}
在这个DSL 中用到了聚合分组排序,用到了functions,field_value_factor中的排序影响的方式,但是这个只是一个简化版,实际查询比以上DSL稍微复杂一些。GET /index_wantbuy/_search{ "from":0, "size":100, "query":{ "script_score":{ "query":{ "bool":{ "should":[ { "multi_match":{ "query":"高价回收整厂设备 高价回收食品设备 高价回收面制品设备 高价回收肉制品设备", "fields":[ "productName^1.0", "remark^1.0", "tittle^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 } } ], "adjust_pure_negative":true, "boost":1 } }, "script":{ "source":"if(doc['offerDeadline'].size()==0){ return 0; } return decayDateGauss(params.origin, params.scale, params.offset, params.decay, doc['offerDeadline'].value ) * _score ;", "lang":"painless", "params":{ "offset":"3d", "origin":"2022-01-12T15:14:35.525+0800", "scale":"30d", "decay":0.5 } }, "boost":1 } }, "_source":{ "includes":[ "tittle", "companyName", "buytype", "wantBuyId", "totalProd", "offerDeadline", "publicTime", "cityid", "userId", "provinceid", "areaid", "auditStatus", "unit" ], "excludes":[ ] }}
这个查询中没有用上已经被下个版本过期的function,而是直接使用scriptGET /index_wantbuy/_search{ "from":0, "size":100, "query":{ "script_score":{ "query":{ "bool":{ "should":[ { "multi_match":{ "query":"高价回收整厂设备 高价回收食品设备 高价回收面制品设备 高价回收肉制品设备", "fields":[ "productName^1.0", "remark^1.0", "tittle^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 } } ], "adjust_pure_negative":true, "boost":1 } }, "script":{ "source":"if(doc['offerDeadline'].size()==0){ return 0; } return decayDateGauss(params.origin, params.scale, params.offset, params.decay, doc['offerDeadline'].value ) * _score ;", "lang":"painless", "params":{ "offset":"3d", "origin":"2022-01-12T15:14:35.525+0800", "scale":"30d", "decay":0.5 } }, "boost":1 } }, "_source":{ "includes":[ "tittle", "companyName", "buytype", "wantBuyId", "totalProd", "offerDeadline", "publicTime", "cityid", "userId", "provinceid", "areaid", "auditStatus", "unit" ], "excludes":[ ] }}
关键词:索引,实例,垂直