0

I understand how to build a mapping for any index and type. But, I want to have a field my_field_1 and my_field_2 which will not be analyzed for all the indexes and types that will be created in future.

PUT /address_index
{
    "mappings":{
    "address":{
     "properties":{
      "state":{
       "type":"string",
       "fields":{
        "raw":{
            "type":"string",
            "index":"not_analyzed"
        }
       }
      }
     }
    }
   }
}

I also saw in one of the links on how to do for all the strings. But, I am unable to add it for just the fields mentioned above.

I will be implementing this in Java. However, just DSL JSON would be good headstart.

Community
  • 1
  • 1
AbhishekAsh
  • 421
  • 2
  • 15

1 Answers1

0

You can do this by creating an index template with a pattern of "*" meaning it will apply to all indices you create in the future and defining this mapping in there.

PUT 127.0.0.1:9200/_template/stacktest
{
    "template": "*",
    "settings": {
        "number_of_shards": 1
    },
    "mappings": {
        "address": {
            "properties": {
                "state": {
                    "type": "string",
                    "fields": {
                        "raw": {
                            "type": "string",
                            "index": "not_analyzed"
                        }
                    }
                }
            }
        }
    }
}

Now you can create an index with any name and this mapping will apply to it.

PUT 127.0.0.1:9200/testindex/

GET 127.0.0.1:9200/testindex/_mapping

{
    "testindex": {
        "mappings": {
            "address": {
                "properties": {
                    "state": {
                        "type": "text",
                        "fields": {
                            "raw": {
                                "type": "keyword"
                            }
                        }
                    }
                }
            }
        }
    }
}

Note that the index: not_analyzed part was transformed into the keyword datatype, as string has been deprecated. You should use text and keyword if you are on version 5.x.


Edit to address your comments

To adapt this to the specific two fields mentioned by you, the following request would create the template:

{
    "template": "*",
    "settings": {
        "number_of_shards": 1
    },
    "mappings": {
        "_default_": {
            "properties": {
                "my_field_1": {
                    "type": "string",
                    "index": "not_analyzed"
                },
                "my_field_2": {
                    "type": "string",
                    "index": "not_analyzed"
                }
            }
        }
    }
}

If you now index a document into a new index, those two fields will be not analyzed for any type of document, any other string fields will be analyzed, which is the way I understood your original question.

PUT 127.0.0.1:9200/testindex/address
{
    "my_field_1": "this is not_analyzed",
    "my_field_2": "this is not_analyzed either",
    "other_field": "this however is analyzed"
}

PUT 127.0.0.1:9200/testindex/differenttype
{
    "my_field_1": "this is not_analyzed",
    "my_field_2": "this is not_analyzed either",
    "other_field": "this however is analyzed"
}

Now check the mapping and notice the difference:

    {
    "testindex": {
        "mappings": {
            "differenttype": {
                "properties": {
                    "my_field_1": {
                        "type": "keyword"
                    },
                    "my_field_2": {
                        "type": "keyword"
                    },
                    "other_field": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    }
                }
            },
            "address": {
                "properties": {
                    "my_field_1": {
                        "type": "keyword"
                    },
                    "my_field_2": {
                        "type": "keyword"
                    },
                    "other_field": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    }
                }
            },
            "_default_": {
                "properties": {
                    "my_field_1": {
                        "type": "keyword"
                    },
                    "my_field_2": {
                        "type": "keyword"
                    }
                }
            }
        }
    }
}
Sönke Liebau
  • 1,943
  • 14
  • 23
  • I donot want it for all the fields , i want to do only for specific feilds mentioned in my question that is "my_field1" and "my_field2" – AbhishekAsh Feb 18 '17 at 12:47
  • what are u saying is basically what has been said in http://stackoverflow.com/questions/36448941/how-to-have-not-analyzed-for-all-new-indexes-by-default and mentioned in my question in the "strings" hyperlink. – AbhishekAsh Feb 18 '17 at 13:02
  • 2
    Actually what I am saying differs from the link that you mention in your question (which I looked at before answering) since it only defines the mapping for a specific field (**state** in this case) - not for all strings. I will add to the answer to illustrate the difference. – Sönke Liebau Feb 18 '17 at 13:15
  • I was stupid and forgot the obvious solution, I have edited my answer again, so that it now fully addresses your issue as I understand it. – Sönke Liebau Feb 19 '17 at 11:17
  • Abishek: Did the proposed answer address your question? If so, would you mind accepting the answer? If not, could you please add a brief comment what is missing - to my understanding this offers the functionality you enquired about? – Sönke Liebau Feb 22 '17 at 07:54