我有一些要使用的Twitter数据。我希望能够搜索一个名字。当尝试生成“名称”和“_id”的ngram时,我遇到了一些麻烦。
首先,我创建了分析器:
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成为多字段,就像我对name所做的那样。但这也不起作用。
_id的行为方式与其他字段不同吗?还是我在这里做错了什么?
编辑:使用elasticsearch v1.1.2并使用河插件从mongodb中提取数据。
谢谢你的帮助
米尔科
最佳答案
看起来是'copy_to'的问题,但是为什么不直接将'id'值插入'id'字段呢?
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"
}'
关于mongodb - elasticsearch:_id字段的特殊行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24600760/