我有一些Twitter数据,我想与之合作。我希望能够搜索一个名字。当试图生成'名字'和'_id'的时候,我遇到了一些麻烦。elasticsearch:_id字段的特殊行为?
第一,我创建了分析:
curl -XPUT 'localhost:9200/twitter_users' -d '
{
"settings": {
"analysis": {
"analyzer": {
"str_search_analyzer": {
"tokenizer": "keyword",
"filter": [
"lowercase"
]
},
"str_index_analyzer": {
"tokenizer": "keyword",
"filter": [
"lowercase",
"ngram"
]
}
},
"filter": {
"ngram": {
"type": "ngram",
"min_gram": 3,
"max_gram": 20
}
}
}
}
}'
然后我定义我的映射:
curl -XPUT 'http://localhost:9200/twitter_users/users/_mapping' -d '
{
"users": {
"type" : "object",
"properties": {
"_id": {
"type": "string",
"copy_to": "id"
},
"id": {
"type": "string",
"search_analyzer": "str_search_analyzer",
"index_analyzer": "str_index_analyzer",
"index": "analyzed"
},
"name": {
"type": "multi_field",
"fields": {
"name": {
"type": "string",
"index": "not_analyzed"
},
"ngrams": {
"type": "string",
"search_analyzer": "str_search_analyzer",
"index_analyzer": "str_index_analyzer",
"index": "analyzed"
}
}
}
}
}
}'
,并插入一些测试数据:
curl -XPUT "localhost:9200/twitter_users/users/johndoe" -d '{
"_id" : "johndoe",
"name" : "John Doe"
}'
curl -XPUT "localhost:9200/twitter_users/users/janedoe" -d '{
"_id" : "janedoe",
"name" : "Jane Doe"
}'
查询的名字让我的预期成果:
curl -XPOST "http://localhost:9200/twitter_users/users/_search" -d '{
"query": {
"match": {
"name.ngrams": "doe"
}
}
}'
分
,但查询的ID没有给我的结果:
curl -XPOST "http://localhost:9200/twitter_users/users/_search" -d '{
"query": {
"match": {
"id": "doe"
}
}
}'
我也测试,以_id多场就像我的名字一样。但是那也行不通。
是_id行为不同于其他领域?或者我在这里做错了什么?
编辑:使用elasticsearch v1.1.2并从一个河流插件中将数据从mongodb中提取出来。
感谢您的帮助
米尔科
我在这里有同样的问题..试图将分析器添加到_id字段。你有没有解决这个问题? – yash
'_id'字段不能在elasticsearch中配置,请检查[this](https://www.elastic.co/blog/great-mapping-refactoring#meta-fields) – Rohanil