【问题标题】:elasticsearch: special behaviour of _id field?elasticsearch:_id 字段的特殊行为?
【发布时间】:2014-08-27 08:31:24
【问题描述】:

我有一些想要使用的 Twitter 数据。我希望能够搜索一个名字。在尝试生成“name”和“_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 并使用 River 插件从 mongodb 中提取数据。

感谢您的帮助

米尔科

【问题讨论】:

  • 我在这里遇到了同样的问题.. 尝试将分析器添加到 _id 字段。你有没有解决这个问题?
  • _id 字段在 elasticsearch 中不再可配置,请检查 this

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


【解决方案1】:

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

【讨论】:

  • 好的,我应该说我正在使用 River-plugin 来获取数据(在本例中来自 mongodb)。除非有一种简单的方法可以在该步骤中生成新字段,否则我无法按照您的方式进行。
猜你喜欢
  • 1970-01-01
  • 2016-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多