how can i create elasticsearch index with multi type field?

Viewed 230
{
 "settings": {
  "index": {
   "mapping": {
    "ignore_malformed": "true",
    "include_type_name": "true"
   }
  }
 },
  "mappings": {
   "properties": {
    "address": {
     "type": "text",
     "field": {
      "type": {
       "type": "keyword"
      },
      "ip": {
       "type": "ip"
      },
      "comment": {
       "analyzer": "whitespace",
       "type": "text"
      }
     }
    }
   }
 }

Error: elasticsearch.exceptions.RequestError: RequestError(400, 'mapper_parsing_exception', "failed to parse field [address] of type [text] in document with id 'UA7RSHUBK7u8_ZjU0JQR'****

This is my code and Error message. How Can I Fix this mapping json? What do you think is causing it? Thanks for your response.

2 Answers

The include_type_name parameter in the index creation, index template, and mapping APIs will default to false. Setting the parameter at all will result in a deprecation warning from Elasticsearch 7.x

The fields setting is allowed to have different settings for fields of the same name in the same index.

Modified index mapping will be :

{
  "settings": {
    "index": {
      "mapping": {                   <-- note this
        "ignore_malformed": "true"
      }
    }
  },
  "mappings": {
    "properties": {
      "address": {
        "type": "text",
        "fields": {          <-- note this
          "type": {
            "type": "keyword"
          },
          "ip": {
            "type": "ip"
          },
          "comment": {
            "analyzer": "whitespace",
            "type": "text"
          }
        }
      }
    }
  }
}

Refer to this Elasticsearch official documentation on fields to know more about this.

Adding a working example with index data, index mapping(same as given above), search query, and search result.

Index data:

{
  "address":"Khajrana circle"
}
{
  "address":"indore"
}
{
  "address":"192.168.1.1"
}

Search Query:

{
  "query": {
    "multi_match": {
      "query": "indore",
      "fields": [
        "address",
        "address.type",
        "address.comment"
      ]
    }
  }
}

Search Result:

"hits": [
      {
        "_index": "64455730",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.0925692,
        "_ignored": [
          "address.ip"
        ],
        "_source": {
          "address": "indore"
        }
      }
    ]

mapper_parsing_exception exception is caused when Elasticsearch is not able to parse the document properly according to its mapping due to various reasons, for ex: if in your mapping, you define a field of type object and try to store text field, a similar question is asked where I explained it in more detail.

As mentioned in your error message, your document id UA7RSHUBK7u8_ZjU0JQR has an improper format for your address field, you need to provide the document JSON for this id in your question and complete error message to further debug the issue.

Related