Skip to content

Commit

Permalink
Backport mapping example into 2.2 (#3722) (#3723)
Browse files Browse the repository at this point in the history
(cherry picked from commit 47fd39c)

Signed-off-by: Fanit Kolchina <[email protected]>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
1 parent e1521c1 commit 051a3db
Showing 1 changed file with 111 additions and 17 deletions.
128 changes: 111 additions & 17 deletions _opensearch/mappings.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,38 +68,132 @@ If you know exactly what your field data types need to be, you can specify them
---
## Mapping example usage

The following example shows how to create a mapping to specify that OpenSearch should ignore any documents with malformed ip addresses that do not conform to the `ip_range` data type. You accomplish this by setting the `ignore_malformed` parameter to `true`.
The following example shows how to create a mapping to specify that OpenSearch should ignore any documents with malformed IP addresses that do not conform to the [`ip`]({{site.url}}{{site.baseurl}}/opensearch/supported-field-types/ip/) data type. You accomplish this by setting the `ignore_malformed` parameter to `true`.

### Create an index with an ip_range mapping
### Create an index with an `ip` mapping

To create an index, use a PUT request:

```json
PUT _index_ip
PUT /test-index
{
"mappings": {
"dynamic_templates": [
{
"ip_range": {
"match": "*ip_range",
"mapping": {
"type": "ip_range",
"ignore_malformed": true
"mappings" : {
"properties" : {
"ip_address" : {
"type" : "ip",
"ignore_malformed": true
}
}
}
]
}
}
```

You can add a document to your index that has an IP range specified:
You can add a document that has a malformed IP address to your index:

```json
PUT _index_ip/_doc/<id>
PUT /test-index/_doc/1
{
"source_ip_range": "192.168.1.1/32"
"ip_address" : "malformed ip address"
}
```

This indexed ip_range does not throw an error because `ignore_malformed` is set to true.
This indexed IP address does not throw an error because `ignore_malformed` is set to true.

You can query the index using the following request:

```json
GET /test-index/_search
```

The response shows that the `ip_address` field is ignored in the indexed document:

```json
{
"took": 14,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "test-index",
"_id": "1",
"_score": 1,
"_ignored": [
"ip_address"
],
"_source": {
"ip_address": "malformed ip address"
}
}
]
}
}
```

## Get a mapping

To get all mappings for one or more indexes, use the following request:

```json
GET <index>/_mapping
```

In the above request, `<index>` may be an index name or a comma-separated list of index names.

To get all mappings for all indexes, use the following request:

```json
GET _mapping
```

To get a mapping for a specific field, provide the index name and the field name:

```json
GET _mapping/field/<fields>
GET /<index>/_mapping/field/<fields>
```

Both `<index>` and `<fields>` can be specified as one value or a comma-separated list.

For example, the following request retrieves the mapping for the `year` and `age` fields in `sample-index1`:

```json
GET sample-index1/_mapping/field/year,age
```

The response contains the specified fields:

```json
{
"sample-index1" : {
"mappings" : {
"year" : {
"full_name" : "year",
"mapping" : {
"year" : {
"type" : "text"
}
}
},
"age" : {
"full_name" : "age",
"mapping" : {
"age" : {
"type" : "integer"
}
}
}
}
}
}
```

0 comments on commit 051a3db

Please sign in to comment.