栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

elasticsearch使用painless脚本进行自定义算分

elasticsearch使用painless脚本进行自定义算分

###查看es版本
GET  http://172.16.101.102:9200?pretty


### 查看索引mapping
get http://172.16.101.102:9200/godproduct/product/_mapping?pretty 
Content-Type: application/application/json

### 查看写入分词情况
GET http://172.16.101.102:9200/godproduct/_analyze?pretty&analyzer=ik_max_word&text=雅诗兰黛眼霜

### 搜索分词情况
GET http://172.16.101.102:9200/godproduct/_analyze?pretty&analyzer=ik_smart&text=雅诗兰黛眼霜

###添加mapping字段
PUT http://172.16.101.102:9200/godproduct/product/_mapping
Content-Type: application/json

{
    "properties": {
        "tempfield01": {
            "type": "keyword"
        },
         "tempfield02": {
            "type": "integer"
        }
    }
}

### 修改字段 update_by_query
POST http://172.16.101.102:9200/godproduct/product/_update_by_query
Content-Type: application/json

{
     "query":{
        "term":{
            "pid":"p3274226"
        } 
    },
    "script":{
        "inline":"ctx._source.tempfield01=params.tempfield01;ctx._source.tempfield02=params.tempfield02",
        "params":{ "tempfield01":"update_by_query_test02",
            "tempfield02":1010  },
        "lang":"painless"
    }
}

### update 通过_updateAPI的script,我们可以增加一个新的field:nick
POST http://172.16.101.102:9200/godproduct/product/208330615/_update
Content-Type: application/json

{
    "script":{
        "inline":"ctx._source.nickname=params.tempfield01;ctx._source.tempfield01=params.tempfield01",
        "params":{ "tempfield01":"update_by_query_test04",
            "tempfield02":1011  },
        "lang":"painless"
    }
}

### update keyword字段为数组,必须一次性保存成数组,否则就变成了字符串
POST http://172.16.101.102:9200/godproduct/product/208330615/_update
Content-Type: application/json

{
    "script":{
        "inline":"ctx._source.promotions=params.proms",
        "params":{ "proms":["10003:2","1000:1"]},
                 
        "lang":"painless"
    }
}


### 追加数组
POST http://172.16.101.102:9200/godproduct/product/208330615/_update
Content-Type: application/json

{
    "script":{
        "inline":"ctx._source.promotions.add(params.proms)",
        "params":{ "proms":"10002:3"},
                 
        "lang":"painless"
    }
}

### get document by docid
GET http://172.16.101.102:9200/godproduct/product/208330615/?pretty

### match搜索
POST  http://172.16.101.102:9200/godproduct/product/_search?pretty
Content-Type: application/json

{
    "query": {
        "match": {
            "title": {
                "query": "专题"
            }
        }
    }
}

### term 查询
POST  http://172.16.101.102:9200/godproduct/product/_search?pretty
Content-Type: application/json

{
    "query": {
        "term": {
            "pid":  "p3274226"           
        }
    }
}

### match_phrase查询 会对输入做分词,但是需要结果中也包含所有的分词,而且顺序要求一样
POST  http://172.16.101.102:9200/godproduct/product/_search?pretty
Content-Type: application/json

{
    "query": {
        "match_phrase": {
            "title":  "樱花微精华露200ml"           
        }
    }
}

### query_string查询 和match类似,但是match需要指定字段名,query_string是在所有字段中搜索,范围更广泛
POST  http://172.16.101.102:9200/godproduct/product/_search?pretty
Content-Type: application/json

{
    "query": {
        "query_string": {
            "query":  "雅诗兰黛"           
        }
    }
}

### 复杂bool query,采用了“匹配越多越好”的方法,因此来自每个匹配的must或should子句的分数将添加在一起,以提供每个文档的最终_score。
POST  http://172.16.101.102:9200/godproduct/product/_search?pretty
Content-Type: application/json

{
  "query": {
    "bool" : {
      "must" : {
        "match" : { "title" : "雅诗兰黛眼霜" }
      },
      "filter": {
        "term" : { "bid" : 10123 }
      },
      "must":{
          "range":{
            "stock":{ "gte" : 1, "lte" : 500} 
          }
      },
       "must_not" : {
        "term" : {   "isshow" : false  }
      },
      "should" : [{
        "nested": {
        "path": "catalogs",
        "query": 
            {"term": {"catalogs.sku" :  "1910040"}}       
        }
    },
        {"term":{"issellout": false}}
    ],
     "minimum_should_match" : 1,
      "boost" : 1.0
    } 
  }
}


###  function score ,自定义算分
### modifiers有:none(默认值),log,log1p,log2p,ln,ln1p,ln2p,square,sqrt以及reciprocal
POST  http://172.16.101.102:9200/godproduct/product/_search?pretty
Content-Type: application/json

{
	"query": {
		"function_score": {
			"query": {
				"match": {
					"title": {
						"query": "雅诗兰黛眼霜"
					}
				}
			},
			"functions": [				
                {
		            "weight":20  ,
		            "filter": { "term": { "fcateid":1086 }}   
	            },
                {
		            "weight":20  ,
		            "filter": { "term": { "fcateid":1087 }}   
	            },                {
		            "weight":30  ,
		            "filter": { "term": { "bid":10123 }}   
	            },                {
                    "weight":100  ,
                     "filter":{"wildcard": { "orTitle":"*樱花微精华露*"} }
                },                {
                 "field_value_factor": {
                        "field": "rankscore",
                        "modifier": "log1p",
                        "factor": 5,
                        "missing":0
                    }
                },                  {
                 "field_value_factor": {
                        "field": "stock",
                        "modifier": "log1p",
                        "factor": 0.001,
                        "missing":0
                    }
                },                {
					"script_score": {
						"script":
                        {   "line": "def sellOut=doc['issellout'].value; def showSellOut=doc['showsellout'].value; if(sellOut&&showSellOut) return params.showSelloutBoost; else if(sellOut) return -params.selloutBoost; else return 0;",
                            "params":{
                                "showSelloutBoost":-50,
                                "selloutBoost":-500000
                            }               
                        }
                    }
				},                {
					"script_score": {
						"script": "int maxSize=1000; int groupId=1000; def groupInId=(long)doc['sgid'].value; def sortnum=(long)doc['gsortnum'].value; if(groupId <= 0) return 0; if(groupInId != groupId) return 0;  return 6.0* (maxSize - 20 * sortnum);"
					}
				},                 {
					"script_score": {
						"script": "def topic='844:'; def list=doc['topics']; if(list == null || list.size()==0) return 0; for(def item :list){if(item.startsWith(topic)){ int index=item.indexOf(':'); if(index>0) return Integer.parseInt(item.substring(index+1))*1;}} return 0; "                       
					}
				},                {
					"script_score": {
						"script":{ 
                            "inline":"def promId=params.promId; if(promId==null) return 0; def list=doc['promotions']; if(list == null || list.size()==0) return 0; for(def id :list){if(id.startsWith(promId)){ int index=id.indexOf(':'); if(index>0) return 6*(params.maxSize-Integer.parseInt(id.substring(index+1))*params.boost);}} return 0; ",                      
                            "params":{
                                    "boost":20,
                                    "promId":"10003",
                                    "maxSize":10000
                                }
                        }
                    }
				}


            ],
			"score_mode": "sum",
			"boost_mode": "replace",
			"max_boost": 1000
        }    
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/582112.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号