---
title: "elasticsearch个性化搜索推荐排序插件"
author: "bywei"
date: "2017-05-04T17:02:35Z"
tags: ["elasticsearch"]
categories: ["开源软件"]
slug: "elasticsearch"
draft: false
lastmod: "2017-05-04T17:03:25Z"
id: 2372651
url: "https://bywei.iteye.com/blog/2372651"
---

## elasticsearch 插件地址

## [https://coding.net/u/bywei/p/elasticsearch-feature-scoring/git](https://coding.net/u/bywei/p/elasticsearch-feature-scoring/git)

## 使用案例

你可以使用这个插件来计算两个特征，如相关性得分：

- 个性化搜索;
- 寻找类似产品;
- 产品推荐;

## [](https://coding.net/u/bywei/p/elasticsearch-feature-scoring/git#user-content-build-plugin-from-code)从代码生成插件

1. 混帐克隆[https://git.coding.net/bywei/elasticsearch-feature-scoring.git](https://git.coding.net/bywei/elasticsearch-feature-scoring.git)
2. MVN清洁套装-DskipTests
3. 创建要素得分插件dircectoy，如$ {} ES\_HOME /插件/功能得分
4. 复制目标/发行/ elasticsearch-feature-scoring-2.1.0.zip到你的插件目录并解压
5. 重启elasticsearch

## [](https://coding.net/u/bywei/p/elasticsearch-feature-scoring/git#user-content-script-parameters)脚本参数

- customSortField：可选，在索引来存储的文件的矢量场;
- customSorts：\*\*不是NULL需要customSortField \*\*，病情，一个地图&lt;字符串，整数&gt;;
- sortFields：可选，条件，一个地图&lt;字符串，整数&gt;;
- 版本：矢量的版本，如果不为空，则应该匹配的文件向量的版本（|，如“20170331 | 0.1”如果使用版本，该字段的值应与“$ VERSION”开始）;

## [](https://coding.net/u/bywei/p/elasticsearch-feature-scoring/git#user-content-about)关于

- QQ：1940775885
- WX：jiaivr
- 网址：[http://www.bywei.cn](http://www.bywei.cn/)

## [](https://coding.net/u/bywei/p/elasticsearch-feature-scoring/git#user-content-example)例

### [](https://coding.net/u/bywei/p/elasticsearch-feature-scoring/git#user-content-create-a-test-index)创建一个测试指标

```
PUT /hotelplatform
{
  "mappings": {
    "hotelDayPrice": {
      "properties": {
        "hotelId": {
          "type": "long"
        },
        "day": {
          "type": "long"
        }
      }
    }
  },
  "settings": {
    "index": {
      "number_of_shards": 1,
      "number_of_replicas": 0
    }
  }
}
```

### [](https://coding.net/u/bywei/p/elasticsearch-feature-scoring/git#user-content-index-some-documents)指数的一些文件

```
POST /hotelplatform/hotelDayPrice/1
{
  "hotelId": "32195",
  "day": "1493740800000"
}

POST /hotelplatform/hotelDayPrice/2
{
  "hotelId": "32250",
  "day": "1493740800000"
}

POST /hotelplatform/hotelDayPrice/3
{
  "hotelId": "32217",
  "day": "1493740800000"
}
```

### [](https://coding.net/u/bywei/p/elasticsearch-feature-scoring/git#user-content-normal-search)正常的搜索

```
POST /hotelplatform/_search
{
  "query": {
    "match": {
      "hotelId": "32217"
    }
  }
}
```

该结果是：

```
{
  "took": 8,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0.14181954,
    "hits": [
      {
        "_index": "hotelplatform",
        "_type": "hotelDayPrice",
        "_id": "2",
        "_score": 0.14181954,
        "_source": {
          "hotelId": "32250",
          "day": "1493740800000"
        }
      },
      {
        "_index": "hotelplatform",
        "_type": "hotelDayPrice",
        "_id": "1",
        "_score": 0.1273061,
        "_source": {
          "hotelId": "32195",
          "day": "1493740800000"
        }
      },
      {
        "_index": "hotelplatform",
        "_type": "hotelDayPrice",
        "_id": "3",
        "_score": 0.1273061,
        "_source": {
          "hotelId": "32217",
          "day": "1493740800000"
        }
      }
    ]
  }
}
```

### [](https://coding.net/u/bywei/p/elasticsearch-feature-scoring/git#user-content-search-whith-feature-score-sortfields)搜索蒙山特征分数sortFields

```
{
  "from" : 0,
  "size" : 10,
  "query" : {
    "bool" : {
      "must" : [ {
        "term" : {
          "channelCode" : "JJ_INTEGRATION_WEIXIN"
        }
      }, {
        "term" : {
          "day" : 1493827200000
        }
      } ]
    }
  },
  "_source" : {
    "includes" : [ "hotelId", "orderScore", "hotelScore", "serviceScore"],
    "excludes" : [ ]
  },
  "sort" : [  {
    "_script" : {
      "script" : {
        "inline" : "feature-scoring",
        "lang" : "native",
        "params" : {
          "sortFields" : {
            "orderScore" : 0.3,
            "hotelScore" : 0.2,
            "serviceScore" : 0.5
          }
        }
      },
      "type" : "number",
      "reverse" : true
    }
  }]
}
```

其结果是：排序（orderScore * 0.3 + hotelScore * 0.2 + serviceScore * 0.5）

### [](https://coding.net/u/bywei/p/elasticsearch-feature-scoring/git#user-content-search-with-feature-score-customsorts-and-customsortfield)搜索与特征分数customSorts和customSortField

```
POST /hotelplatform/_search
{
  "from": 0,
  "size": 100,
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "day": 1493740800000
          }
        }
      ]
    }
  },
  "_source": {
    "includes": [
      "hotelId",
      "day"
    ],
    "excludes": []
  },
  "sort": [
    {
      "_script": {
        "script": {
          "inline": "feature-scoring",
          "lang": "native",
          "params": {
            "customSortField": "hotelId",
            "customSorts": {
              "32195": 200,
              "32217": 300
            }
          }
        },
        "type": "number",
        "reverse": true
      }
    }
  ]
}
```

其结果是：

```
   {
    "took": 36,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 12,
        "max_score": null,
        "hits": [
            {
                "_index": "hotelplatform",
                "_type": "hotelDayPrice",
                "_id": "32217JJ_INTEGRATION_WEIXIN1493740800000",
                "_score": null,
                "_routing": "32217",
                "_parent": "32217",
                "_source": {
                    "hotelId": 32217,
                    "day": 1493740800000
                },
                "sort": [
                    1,
                    300,
                    440
                ]
            },
            {
                "_index": "hotelplatform",
                "_type": "hotelDayPrice",
                "_id": "32195JJ_INTEGRATION_WEIXIN1493740800000",
                "_score": null,
                "_routing": "32195",
                "_parent": "32195",
                "_source": {
                    "hotelId": 32195,
                    "day": 1493740800000
                },
                "sort": [
                    1,
                    200,
                    554
                ]
            },
            {
                "_index": "hotelplatform",
                "_type": "hotelDayPrice",
                "_id": "32250JJ_INTEGRATION_WEIXIN1493740800000",
                "_score": null,
                "_routing": "32250",
                "_parent": "32250",
                "_source": {
                    "hotelId": 32250,
                    "day": 1493740800000
                },
                "sort": [
                    1,
                    0,
                    596
                ]
            },
            {
                "_index": "hotelplatform",
                "_type": "hotelDayPrice",
                "_id": "32216JJ_INTEGRATION_WEIXIN1493740800000",
                "_score": null,
                "_routing": "32216",
                "_parent": "32216",
                "_source": {
                    "hotelId": 32216,
                    "day": 1493740800000
                },
                "sort": [
                    1,
                    0,
                    646
                ]
            },
            {
                "_index": "hotelplatform",
                "_type": "hotelDayPrice",
                "_id": "32247JJ_INTEGRATION_WEIXIN1493740800000",
                "_score": null,
                "_routing": "32247",
                "_parent": "32247",
                "_source": {
                    "hotelId": 32247,
                    "day": 1493740800000
                },
                "sort": [
                    1,
                    0,
                    4079
                ]
            },
            {
                "_index": "hotelplatform",
                "_type": "hotelDayPrice",
                "_id": "32219JJ_INTEGRATION_WEIXIN1493740800000",
                "_score": null,
                "_routing": "32219",
                "_parent": "32219",
                "_source": {
                    "hotelId": 32219,
                    "day": 1493740800000
                },
                "sort": [
                    1,
                    0,
                    4909
                ]
            },
            {
                "_index": "hotelplatform",
                "_type": "hotelDayPrice",
                "_id": "21400JJ_INTEGRATION_WEIXIN1493740800000",
                "_score": null,
                "_routing": "21400",
                "_parent": "21400",
                "_source": {
                    "hotelId": 21400,
                    "day": 1493740800000
                },
                "sort": [
                    0,
                    0,
                    1
                ]
            },
            {
                "_index": "hotelplatform",
                "_type": "hotelDayPrice",
                "_id": "22292JJ_INTEGRATION_WEIXIN1493740800000",
                "_score": null,
                "_routing": "22292",
                "_parent": "22292",
                "_source": {
                    "hotelId": 22292,
                    "day": 1493740800000
                },
                "sort": [
                    0,
                    0,
                    12
                ]
            },
            {
                "_index": "hotelplatform",
                "_type": "hotelDayPrice",
                "_id": "22290JJ_INTEGRATION_WEIXIN1493740800000",
                "_score": null,
                "_routing": "22290",
                "_parent": "22290",
                "_source": {
                    "hotelId": 22290,
                    "day": 1493740800000
                },
                "sort": [
                    0,
                    0,
                    18
                ]
            },
            {
                "_index": "hotelplatform",
                "_type": "hotelDayPrice",
                "_id": "21382JJ_INTEGRATION_WEIXIN1493740800000",
                "_score": null,
                "_routing": "21382",
                "_parent": "21382",
                "_source": {
                    "hotelId": 21382,
                    "day": 1493740800000
                },
                "sort": [
                    0,
                    0,
                    150
                ]
            },
            {
                "_index": "hotelplatform",
                "_type": "hotelDayPrice",
                "_id": "21440JJ_INTEGRATION_WEIXIN1493740800000",
                "_score": null,
                "_routing": "21440",
                "_parent": "21440",
                "_source": {
                    "hotelId": 21440,
                    "day": 1493740800000
                },
                "sort": [
                    0,
                    0,
                    488
                ]
            },
            {
                "_index": "hotelplatform",
                "_type": "hotelDayPrice",
                "_id": "20166JJ_INTEGRATION_WEIXIN1493740800000",
                "_score": null,
                "_routing": "20166",
                "_parent": "20166",
                "_source": {
                    "hotelId": 20166,
                    "day": 1493740800000
                },
                "sort": [
                    0,
                    0,
                    755
                ]
            }
        ]
    }
}
```

### [](https://coding.net/u/bywei/p/elasticsearch-feature-scoring/git#user-content-a-personalized-search-case-details)个性化搜索案例的详细情况

锦江App应用