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

猜你喜欢

转载自blog.csdn.net/bettyHHUC/article/details/89707225