ElasticSearch能够将值复制到其他字段(在索引时),使您能够在多个字段上搜索,就像它是一个字段一样(核心类型: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"
}
}
}
}
'
Run Code Online (Sandbox Code Playgroud)
只返回"Jim Bob"; 似乎字段按字段名字母顺序复制.
如何切换copy_to命令以便返回"Bob Jim"人?
通过在映射中注册转换脚本,可以更确定地控制这一点。
像这样的东西:
"transform" : [
{"script": "ctx._source['full_name'] = [ctx._source['first_name'] + " " + ctx._source['last_name'], ctx._source['last_name'] + " " + ctx._source['first_name']]"}
]
Run Code Online (Sandbox Code Playgroud)
此外,转换脚本可以是“本机”的,即java代码,通过使您的自定义类在elasticsearch类路径中可用并通过设置注册为本机脚本,使集群中的所有节点都可用:
script.native.<name>.type=<fully.qualified.class.name>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,在映射中,您可以将本机脚本注册为转换,如下所示:
"transform" : [
{
"script" : "<name>",
"params" : {
"param1": "val1",
"param2": "val2"
},
"lang": "native"
}
],
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2029 次 |
| 最近记录: |