Olivier
Olivier

Reputation: 834

Couchbase - Elasticsearch: custom dynamic type

I'm using XDCR replication to sync the data between CB and Elasticsearch, using the couchbase transport plugin for Elasticsearch. As far as i understand, all documents in Couchbase will come with the type "couchbaseDocument". But I have different documents types with a specific mapping for each document.

Is there a way to have specific dynamic type instead of the default "couchbaseDocument"?

(where if the json document have a field "type":"beer" it will be indexed in ES as _type:"beer" and if "type":"wine" it will be indexed as _type:"wine")


What I have in couchbase:

bucket: "drinks", 
beer_1234: 
{
  "type": "beer",
  "name": "leffe"
}

How it's indexed in Elasticsearch:

{
  "_index": "drinks",
  "_type": "couchbaseDocument", // <======================== ????
  "_id": "beer_1234",
  "_version": 1,
  "_source": {
    "doc": {
       "type": "beer",
       "name": "leffe"
    },
    "meta": {
       "id": "beer_1234",
       "rev": "9-000049e945bd62fa0000000000000000",
       "expiration": 0,
       "flags": 0
    }
}

What I need:

{
  "_index": "drinks",
  "_type": "beer",  // <======================== NICE TYPE
  "_id": "beer_1234",
  "_version": 1,
  "_source": {
    "doc": {
       "type": "beer",
       "name": "leffe"
    },
    "meta": {
       "id": "beer_1234",
       "rev": "9-000049e945bd62fa0000000000000000",
       "expiration": 0,
       "flags": 0
    }
}

Thanks

Upvotes: 2

Views: 255

Answers (1)

John Fadria
John Fadria

Reputation: 1945

The philosophy is to modify the default transport mapping to index your type field. For example:

curl -XPUT 'http: //localhost:9200/drinks/'-d'{
    "mappings": {
        "couchbaseCheckpoint": {
            "dynamic": "true",
            "_source": {
                "includes": [
                    "doc.*"
                ]
            },
            "dynamic_templates": [
                {
                    "store_no_index": {
                        "match": "*",
                        "mapping": {
                            "store": "no",
                            "index": "no",
                            "include_in_all": false
                        }
                    }
                }
            ]
        },
        "couchbaseDocument": {
            "_all": {
                "enabled": false
            },
            "dynamic": "true",
            "_source": {
                "includes": [
                    "meta.*"
                ]
            },
            "dynamic_templates": [
                {
                    "all_strings_to_avoid_collisions": {
                        "match": "*",
                        "mapping": {
                            "store": "no",
                            "index": "not_analyzed",
                            "include_in_all": false,
                            "type": "string",
                            "analyzer": "whitespace"
                        }
                    }
                }
            ],
            "properties": {
                "doc": {
                    "properties": {
                        "type": {
                            "type": "string"
                        }
                    }
                },
                "meta": {
                    "properties": {
                        "id": {
                            "type": "string",
                            "analyzer": "whitespace"
                        }
                    }
                }
            }
        }
    }
}'

Upvotes: 0

Related Questions