如何增加完成建议字段的权重?

时间:2016-05-14 21:54:53

标签: elasticsearch

我正在完成建议。我想通过递增它们来增加一些索引文档的权重。我有:

POST /tester/
{
        "mappings": {
          "song": {
            "properties": {
              "suggest": {
                "type": "completion",
                "analyzer": "simple",
                "search_analyzer" : "simple",
                "payloads": true,
                "preserve_separators": true,
                "preserve_position_increments": true,
                "max_input_length": 100
              }
            }
          }
        }
        }

//索引文档

PUT tester/song/1
{
    "name" : "Nevermind",
    "suggest" : {
        "input": [ "Nevermind", "Nirvana" ],
        "output": "Nirvana - Nevermind",
        "payload" : { "artistId" : 2321 },
        "weight" : 1
    }
}

//增加重量

POST /tester/song/1
{
    "script" : {
        "inline": "ctx._source.suggest.weight += 1"
    }
}

// GET / tester

的结果
{
        "_index": "tester",
        "_type": "song",
        "_id": "1",
        "_score": 1,
        "_source": {
          "script": {
            "inline": "ctx._source.suggest.weight += 1"
          }
        }
      }

不是增加权重而是重写文档。我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

首先,将这些行添加到您的配置中,您应该enable dynamic scripting

script.inline: true
script.indexed: true

然后您需要使用_update端点进行更新:

POST 'localhost:9200/tester/song/1/_update' -d '
{
    "script" : {
        "inline": "ctx._source.suggest.weight += 1"
    }
}'

检查:https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html#_scripted_updates