如何指定ElasticSearch copy_to命令?

时间:2015-04-09 09:11:26

标签: elasticsearch

ElasticSearch能够将值复制到其他字段(在索引时),使您能够在多个字段上搜索,就像它是一个字段(Core Types: copy_to)一样。

但是,似乎没有任何方法可以指定应该复制这些值的顺序。当短语匹配时,这可能很重要:

curl -XDELETE 'http://10.11.12.13:9200/helloworld'
curl -XPUT 'http://10.11.12.13:9200/helloworld'
# copy_to is ordered alphabetically!
curl -XPUT 'http://10.11.12.13:9200/helloworld/_mapping/people' -d '
{
  "people": {
    "properties": {
      "last_name": {
        "type": "string",
        "copy_to": "full_name"
      },
      "first_name": {
        "type": "string",
        "copy_to": "full_name"
      },
      "state": {
        "type": "string"
      },
      "city": {
        "type": "string"
      },
      "full_name": {
        "type": "string"
      }
    }
  }
}
'

curl -X POST "10.11.12.13:9200/helloworld/people/dork" -d '{"first_name": "Jim", "last_name": "Bob", "state": "California", "city": "San Jose"}'
curl -X POST "10.11.12.13:9200/helloworld/people/face" -d '{"first_name": "Bob", "last_name": "Jim", "state": "California", "city": "San Jose"}'


curl "http://10.11.12.13:9200/helloworld/people/_search" -d '
{
  "query": {
    "match_phrase": {
      "full_name": {
        "query":    "Jim Bob"
      }
    }
  }
}
'

只返回“Jim Bob”;似乎字段按字段名字母顺序复制。

如何切换 copy_to 订单,以便返回“Bob Jim”的人?

1 个答案:

答案 0 :(得分:3)

通过在映射中注册transform script来确定性地控制这一点。

类似的东西:

"transform" : [
    {"script": "ctx._source['full_name'] = [ctx._source['first_name'] + " " + ctx._source['last_name'], ctx._source['last_name'] + " " + ctx._source['first_name']]"}
]

此外,转换脚本可以是" native",即java代码,通过在elasticsearch类路径中提供自定义类并通过以下方式注册为本机脚本,使群集中的所有节点可用;设置:

script.native.<name>.type=<fully.qualified.class.name>

在您的映射中,您可以将本机脚本注册为如下所示的转换:

"transform" : [
    {
        "script" : "<name>",
        "params" : {
            "param1": "val1",
            "param2": "val2"
        },
        "lang": "native"
    }
],