Multi-Fields

It is often useful to index the same field in different ways for different purposes. This is the purpose of multi-fields. For instance, a string field could be mapped as a text field for full-text search, and as a keyword field for sorting or aggregations:

PUT index_test

{

  "mappings": {

    "_doc": {

      "properties": {

        "city": {

          "type""text",                 //The city field can be used for full text search.

          "fields": {

            "raw": {                     //The city.raw field is a keyword version of the city field.

              "type":  "keyword"         //The city.raw field can be used for sorting and aggregations  

            }

          }

        }

      }

    }

  }

}

Thus we can use the city field for full text search and city.raw field for sorting and aggregations.

What if we need the attribute itself to have 2 different types?

Let's say we have an attribute Org_Id, it has long type in DB but now we need to do full-text search to it  like below graph shows. 

We cannot index the attribute by just setting its type as "text" for someday in the future we would need its numberic type for range/comprison calculation. 

 

We can index the attribute id like below and post some data to it:

PUT testkeyword

{

  "mappings": {

    "doc":{

      "properties": {

        "name":{

          "type""text",

          "fields": {

            "keyword":{

              "type""keyword",

              "ignore_above"256

            }

          }

        },

        "id":{

          "type""long",

          "fields": {

            "textval":{

              "type""text"

            },

            "keywordval":{

              "type""keyword"

            }

          }

        }

      }

    }

  }

}

 

 

POST testkeyword/doc

{

  "name""betty",

  "id"201602241

}

 

POST testkeyword/doc

{

  "name""Ziv",

  "id"201602242

}

 

POST testkeyword/doc

{

  "name""Sara",

  "id"201702242

}

 

Full-text search and aggregation:

GET testkeyword/_search

{

  "aggs": {

    "my_buckets": {

      "composite": {

        "sources": [

          {

            "id": {

              "terms": {

                "field""id.keywordval" 

              }

            }

          }

        ]

      }

    }

  },

  "query": {

    "bool": {

      "minimum_should_match"1,

      "should": [

        {

          "query_string": {

            "default_field""id.textval",

            "query""2016*"

          }

        },

        {

          "match": {

            "id.textval""2016*"

          }

        }

      ],

      "filter": {

        "range": {

          "id": {

            "gte"201602242           //As a numberic type

          }

        }

      }

    }

  },

  "size"0

}

Aggregation Results 

{

  "took"7,

  "timed_out"false,

  "_shards": {

    "total"5,

    "successful"5,

    "skipped"0,

    "failed"0

  },

  "hits": {

    "total"1,

    "max_score"0,

    "hits": []

  },

  "aggregations": {

    "my_buckets": {

      "after_key": {

        "id""201602242"

      },

      "buckets": [

        {

          "key": {

            "id""201602242"

          },

          "doc_count"1

        }

      ]

    }

  }

}

 

 

Ref

https://www.elastic.co/guide/en/elasticsearch/reference/current/multi-fields.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值