Seb
Seb

Reputation: 3902

Elastic how to define a mapping given another field value

Here is what I have as data :

 {
          "firstname" : "blablabla",
              ...
          "otherDatas" : [
            {
              "reference" : "40xxx1372",
              "universe" : "marketing",
              "dataType" : "id"
            },
            {
              "reference" : "roleA",
              "universe" : "finance",
              "dataType" : "role"
            }
          ],
        }

What I would need to do is create a mapping for otherDatas special case like having otherDatas.role.reference : keyword only if my otherDatas has the datatype "role"

So I could search for all user having a given role in the organisation

Is there a way to achieve that ?

Upvotes: 0

Views: 45

Answers (1)

JBone
JBone

Reputation: 1784

It is also equally important to understand what type values go into the key before you design your mapping. Please look into the differences between text , and keyword types. I assume all those fields are keyword, meaning one word and not needed any analysis on those values.

Coming to your mapping, I believe nested object probably confused you. Here is how you go about it (and let me know if you have any specific questions)

PUT so_test14
{
  "mappings": {
    "_doc":{
      "properties":{
        "firstname": {"type":"keyword"},

        "otherDatas": {
          "type": "nested",
          "properties": {
            "reference": {"type": "keyword"},
            "universe": {"type": "keyword"},
            "dataType": {"type": "keyword"}
          }
        }
      }
    }
  }
}

Upvotes: 2

Related Questions