mongodb - elasticsearch:_id字段的特殊行为?

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

我有一些要使用的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/

相关文章:

php - Heroku远程MongoDB与php连接

elasticsearch - Elasticsearch转义斜线

r - 在 R 中查找 ngram 并比较跨语料库的 ngram

python - 在 NLP 中使用 tf-idf 如何从 python 中的语料库(包含大量文档)中查找特定单词的频率

Mongodb:按日期计算项目然后计算唯一项目

mongodb - 将字符串转换为objectid mongodb

elasticsearch - Elasticsearch-在一起键入的单词中检测单词

search - Drupal 的搜索模块可以搜索子字符串吗? (部分搜索)

node.js - 如何使用 Mongoose 聚合框架对数组元素进行分组

amazon-web-services - AWS Elasticsearch:如何将数据从一个集群加载到另一个集群