【问题标题】:How to index the MongoDB "id_" field with Elasticsearch?如何使用 Elasticsearch 索引 MongoDB“id_”字段?
【发布时间】:2015-02-04 19:30:49
【问题描述】:

我在 MongoDB 中使用 ElasticSearch,并希望在我的 Elasticsearch 索引中搜索 ObjectID。

我的 MongoDB 包含以下条目:

{ "_id" : ObjectId("54ca06664ceb6693d37de155"), "blabla" : "xxxyyy"}

我尝试创建自定义映射,例如:

curl -XPOST "http://localhost:9200/myIndex/myType/_mapping" -d'
{
"myType":{
  "properties":{
    "_all": {
      "enabled": false
    },
    "_id": {
      "type": "string",
      "store": true,
      "index": "analyzed"
    },
    "blabla": {
      "type": "string",
      "store": true,
      "index": "analyzed"
    }
}

}'

但是当我想用

检查我的映射时
curl -XGET 'http://localhost:9200/_all/_mapping'

我看不到 _id 映射。搜索“54ca06664ceb6693d37de155”也不会返回任何结果。

怎么了?

【问题讨论】:

  • 如何将数据从 MongoDB 加载到 Elasticsearch?您是否在加载任何数据之前创建了此映射?
  • 我正在使用 River (github.com/richardwilly98/elasticsearch-river-mongodb)。首先创建索引,然后创建映射,最后创建河流。它适用于其他字段,但不幸的是不适用于“id”字段。

标签: mongodb elasticsearch


【解决方案1】:

_id 字段是 Elasticsearch 中的默认主键,如 mongoDB。虽然您没有找到 _id 字段的映射,但您可以使用以下查询搜索 _id 字段。欲了解更多信息refer this link.

您可以在退货文件中找到 _id 字段:

 {
"_index" :   "website",
"_type" :    "blog",
"_id" :      "123",
"_version" : 1,
"found" :    true,
"_source" :  {
  "title": "My first blog entry",
  "text":  "Just trying this out...",
  "date":  "2014/01/01"
 }
}

问题 1:

{
"query": {
    "terms": {
       "_id": [
          "VALUE1",
          "VALUE2"
       ]
    }
  }
}

问题 2:

{
  "query": {
    "term": {
       "_id": {
          "value": "VALUE"
          }
       }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-20
    相关资源
    最近更新 更多