elasticsearch: copying meta-field _id to other field while creating document

1.2k views Asked by At

I am using elasticsearch. I see there is meta-field _id for each document. I want to search document using this meta-field as I don't have any other field as unique field in document. But _id is a string and can have dashes which are not possible to search unless we add mapping for field as type :keyword. But it is possible as mentioned here. So now I am thinking to add another field newField in document and make it same as _id. One way to do it is: first create document and assign _id to that field and save document again. But this will have 2 connections which is not that good. So I want to find some solution to set newField while creating document itself. Is it even possible?

2

There are 2 answers

0
mel On

You can search for a document that contains dashes:

PUT my_index/tweet/testwith-
{
  "fullname" : "Jane Doe",
  "text" : "The twitter test!"
}

We just created a document with a dash in its id

GET my_index/tweet/_search
{
  "query": {
    "terms": {
      "_id": [
        "testwith-"
      ]
    }
  }
} 

We search for the document that have the following id: testwith-

{
  "took": 9,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "my_index",
        "_type": "tweet",
        "_id": "testwith-",
        "_score": 1,
        "_source": {
          "fullname": "Jane Doe",
          "text": "The twitter test!"
        }
      }
    ]
  }
}

We found it. We can search on document that have - in it.

1
alr On

you could also use a set processor when using an ingest pipeline to store the id in an additional field, see https://www.elastic.co/guide/en/elasticsearch/reference/5.5/accessing-data-in-pipelines.html and https://www.elastic.co/guide/en/elasticsearch/reference/5.5/set-processor.html