elasticsearch:_id字段的特殊行为?

时间:2014-07-06 22:06:59

标签: mongodb elasticsearch n-gram elasticsearch-mongo-river

我有一些我想要使用的推特数据。我希望能够搜索一个名字。当试图生成名称'和' _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中提取数据。

感谢您的帮助

的Mirko

1 个答案:

答案 0 :(得分:0)

看起来像'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"
}'