简体   繁体   中英

How to index the MongoDB “id_” field with Elasticsearch?

I'm using ElasticSearch with MongoDB and want to search for ObjectIDs in my Elasticsearch Index.

My MongoDB contains entries like:

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

I tried to create a custom mapping for example:

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"
    }
}

}'

but when I want to check my mappings with

curl -XGET 'http://localhost:9200/_all/_mapping'

I can't see the _id mapping. Searching for "54ca06664ceb6693d37de155" also does not return any results.

What's wrong?

_id field is default primary key in Elasticsearch like mongoDB.Though you dont find mapping for _id field, you can search for _id field using below query. for more info refer this link.

You can find _id field in returns documents:

 {
"_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"
 }
}

QUERY 1:

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

QUERY 2:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM