4-Elasticsearch字段类型

Elasticsearch字段类型

字段类型分类
  1. 常用类型
    1. binary:存储编码为Base64的字符串或二进制值
    2. boolean:存储true或false
    3. keyword:存储时不会分词处理,适合统计分析,不能全文检索。
    4. numbers:表示数字类型
    5. date:表示日期类型
    6. alias:现有字段的别名
    7. text:存储时会进行分词并建立索引,适合去问搜索,不能进行统计分析。
  2. 对象和关系类型
    1. object:表示JSON对象
    2. nested:嵌套类型
    3. array:数组,默认包含0个或多个值,数组中的数据类型必须相同。
  3. 其余类型
    1. range:范围类型
    2. rank_feature:排名类型
    3. token_count:令牌技术类型
    4. ip:IP地址的存储和查询类型
    5. geo_point,geo_shape:地理位置和空间位置
1、alias类型

只能使用别名搜索,不能写入。

# alias
PUT /user_info
{
  "mappings": {
    "properties": {
      "id":{
        "type": "long"
      },
      "user_id":{
        "type": "alias",
        "path":"id"
      },
      "user_name":{
        "type": "keyword"
      }
    }
  }
}

PUT /user_info/_doc/1
{
  "id":1,
  "user_name":"欧冶子"
}

GET /user_info/_doc/_search
{
  "query": {
    "range": {
      "user_id": {
        "gte": 1
      }
    }
  }
}

查询结果:

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "user_info",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "id" : 1,
          "user_name" : "欧冶子"
        }
      }
    ]
  }
}
2、数组类型
# 数组类型
PUT idx_student_info/_doc/1
{
  "student_name": "小花",
  "tags": [
    "优秀团员",
    "三好学生"
  ],
  "interest_lists": [
    {
      "name": "篮球",
      "describe": "练球三小时"
    },
    {
      "name": "跑步",
      "describe": "每天五公里"
    }
  ]
}

PUT idx_student_info/_doc/2
{
  "student_name": "小明",
  "tags":"三好学生",
  "interest_lists": {
      "name": "篮球",
      "describe": "练球三小时"
    }
}

GET idx_student_info/_search
{
  "query":{
    "match":{
      "tags":"三好学生"
    }
  }
}

查询结果:

{
  "took" : 960,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.84443676,
    "hits" : [
      {
        "_index" : "idx_student_info",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.84443676,
        "_source" : {
          "student_name" : "小明",
          "tags" : "三好学生",
          "interest_lists" : {
            "name" : "篮球",
            "describe" : "练球三小时"
          }
        }
      },
      {
        "_index" : "idx_student_info",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.641772,
        "_source" : {
          "student_name" : "小花",
          "tags" : [
            "优秀团员",
            "三好学生"
          ],
          "interest_lists" : [
            {
              "name" : "篮球",
              "describe" : "练球三小时"
            },
            {
              "name" : "跑步",
              "describe" : "每天五公里"
            }
          ]
        }
      }
    ]
  }
}
3、binary类型

# binary类型
PUT idx_binary
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text"
      },
      "content": {
        "type": "binary"
      }
    }
  }
}

PUT idx_binary/_doc/1
{
  "title":"每日一思",
  "content":"55Sf5rS75pei6KaB5bC95b+D77yM5Lmf6KaB6ZqP5b+D77yM5pei6KaB5Yaz5b+D77yM5Lmf6KaB5byA5b+D44CC5b+D5pyJ6Ziz5YWJ77yM5aSE5aSE5ZKM5ZaE5a6J5a6B77yb5b+D5oCA6LGB6L6+77yM5aSE5aSE5rW36ZiU5aSp56m644CC6LWw6L+H5Z2O5Z2377yM5pu05Yqg5Z2a5by677yM6Zev6L+H6aOO6Zuo77yM5pu05Yqg5LmQ6KeC77yM5LiA5Liq5bm06b6E5LiA5Liq5b+D5oCB77yM5b6A5ZCO5L2Z55Sf77yM5YGa5LiA5Liq566A5Y2V5b+r5LmQ55qE5Lq644CC"
}

GET idx_binary/_search

查询结果:

{
  "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "idx_binary",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "title" : "每日一思",
          "content" : "55Sf5rS75pei6KaB5bC95b+D77yM5Lmf6KaB6ZqP5b+D77yM5pei6KaB5Yaz5b+D77yM5Lmf6KaB5byA5b+D44CC5b+D5pyJ6Ziz5YWJ77yM5aSE5aSE5ZKM5ZaE5a6J5a6B77yb5b+D5oCA6LGB6L6+77yM5aSE5aSE5rW36ZiU5aSp56m644CC6LWw6L+H5Z2O5Z2377yM5pu05Yqg5Z2a5by677yM6Zev6L+H6aOO6Zuo77yM5pu05Yqg5LmQ6KeC77yM5LiA5Liq5bm06b6E5LiA5Liq5b+D5oCB77yM5b6A5ZCO5L2Z55Sf77yM5YGa5LiA5Liq566A5Y2V5b+r5LmQ55qE5Lq644CC"
        }
      }
    ]
  }
}
4、boolean类型

boolean类型不能使用0或1代替。

# boolean类型
PUT idx_boolean
{
  "mappings": {
    "properties": {
      "is_published":{
        "type": "boolean"
      }
    }
  }
}

POST idx_boolean/_doc
{
  "is_published":true
}

GET idx_boolean/_search

查询结果:

{
  "took" : 27,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "idx_boolean",
        "_type" : "_doc",
        "_id" : "UMb6tIYBI9BugZ8gkK_q",
        "_score" : 1.0,
        "_source" : {
          "is_published" : true
        }
      }
    ]
  }
}

5、date类型
PUT idx_date_1
{
  "mappings": {
    "properties": {
      "add_date":{
        "type": "date"
      }
    }
  }
}

POST idx_date_1/_doc
{
  "add_date":"2023-03-06"
}

GET idx_date_1/_search

查询结果:

{
  "took" : 28,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "idx_date_1",
        "_type" : "_doc",
        "_id" : "UcZ7tYYBI9BugZ8gzK8z",
        "_score" : 1.0,
        "_source" : {
          "add_date" : "2023-03-06"
        }
      }
    ]
  }
}

带格式的日期类型:


PUT idx_date_2
{
  "mappings": {
    "properties": {
      "add_date":{
        "type": "date",
        "format": "yyyy/MM/dd"
      }
    }
  }
}

POST idx_date_2/_doc
{
  "add_date":"2023/03/06"
}

GET idx_date_2/_search

查询结果:

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "idx_date_2",
        "_type" : "_doc",
        "_id" : "UsZ_tYYBI9BugZ8g-a_T",
        "_score" : 1.0,
        "_source" : {
          "add_date" : "2023/03/06"
        }
      }
    ]
  }
}
6、nested类型

# nested -- 嵌套类型
# 创建索引并指定user字段未一个嵌套类型
PUT idx_object_nested
{
  "mappings": {
    "properties": {
      "user":{
        "type": "nested"
      }
    }
  }
}

# 在索引中插入文档数据,user字段中嵌套了键值对
POST idx_object_nested/_doc
{
  "group": "fans",
  "user": [
    {
      "first_name": "John",
      "last_name": "Smith"
    },
    {
      "first_name": "Alice",
      "last_name": "White"
    }
  ]
}

# 查询user索引库中user.first_name的值为Alice
GET idx_object_nested/_search
{
  "query": {
    "nested": {
      "path": "user",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "user.first_name": "Alice"
              }
            },
            {
              "match": {
                "user.last_name": "White"
              }
            }
          ]
        }
      }
    }
  }
}

查询结果:

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.3862942,
    "hits" : [
      {
        "_index" : "idx_object_nested",
        "_type" : "_doc",
        "_id" : "U8bxtYYBI9BugZ8gYq-Z",
        "_score" : 1.3862942,
        "_source" : {
          "group" : "fans",
          "user" : [
            {
              "first_name" : "John",
              "last_name" : "Smith"
            },
            {
              "first_name" : "Alice",
              "last_name" : "White"
            }
          ]
        }
      }
    ]
  }
}

查询


# 查询user索引库中user.first_name的值为Alice
GET idx_object_nested/_search
{
  "query": {
    "nested": {
      "path": "user",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "user.first_name": "Alice"
              }
            },
            {
              "match": {
                "user.last_name": "Smith"
              }
            }
          ]
        }
      }
    }
  }
}

查询结果:

{
  "took" : 19,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}
7、range类型

range表示介于上限和下限之间的连续范围。

运算符:

  1. gt:大于
  2. gte:大于等于
  3. lt:小于
  4. lte:小于等于

每种范围类型说明:

范围类型说明
integer_range有符号32位整数
float_range单精度浮点数
long_range有符号64位整数
double_range双精度浮点数
date_range日期范围
ip_range表示IPv4或IPv6地址的一系列值

# range类型
# 创建一个索引映射,
# 指定expected_attendees为整数类范围类型
# 指定time_frame字段类型为日期范围类型
PUT idx_range
{
  "mappings": {
    "properties": {
      "expected_attendees":{
        "type": "integer_range"
      },
      "time_frame":{
        "type": "date_range",
        "format": "yyyy-MM-dd HH:mm:ss||yyy-MM-dd||epoch_millis"
      }
    }
  }
}
# 插入索引文档
POST idx_range/_doc
{
  "expected_attendees": {
    "gte": 10,
    "lt": 20
  },
  "time_frame":{
    "gte":"2023-03-01 10:10:10",
    "lt":"2023-05-01"
  }
}

GET idx_range/_search
{
  "query": {
    "term": {
      "expected_attendees": {
        "value": "22"
      }
    }
  }
}

查询:

{
  "took" : 145,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "idx_range",
        "_type" : "_doc",
        "_id" : "VMY8toYBI9BugZ8go69M",
        "_score" : 1.0,
        "_source" : {
          "expected_attendees" : {
            "gte" : 10,
            "lt" : 20
          },
          "time_frame" : {
            "gte" : "2023-03-01 10:10:10",
            "lt" : "2023-05-01"
          }
        }
      }
    ]
  }
}
GET idx_range/_search
{
  "query": {
    "range": {
      "time_frame": {
        "gte": "2023-04-04 00:00:00",
        "lte": "2023-04-05"
      }
    }
  }
}

查询结果:

{
  "took" : 82,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "idx_range",
        "_type" : "_doc",
        "_id" : "VMY8toYBI9BugZ8go69M",
        "_score" : 1.0,
        "_source" : {
          "expected_attendees" : {
            "gte" : 10,
            "lt" : 20
          },
          "time_frame" : {
            "gte" : "2023-03-01 10:10:10",
            "lt" : "2023-05-01"
          }
        }
      }
    ]
  }
}

8、数字类型

数字类型分类:

  • long:有符号64位
  • integer:有符号32位
  • short:有符号16位
  • byte:有符号8位
  • double:双精度浮点数
  • float:单精度浮点数
  • half_float:
  • unsigned_long
# 数字类型
PUT idx_number_type
{
  "mappings": {
    "properties": {
      "student_id": {
        "type": "integer"
      },
      "height": {
        "type": "float"
      },
      "weight": {
        "type": "double"
      },
      "score": {
        "type": "scaled_float",
        "scaling_factor": 100 # 表示数值在存储时使用缩放因子,该值在存储时会乘以缩放因子并四舍五入到最接近long类型的值。
      }
    }
  }
}

POST idx_number_type/_doc
{
  "student_id": 101,
  "height": 175.5,
  "weight": 68.7,
  "score": 99.99
}

GET idx_number_type/_search

查询结果:

{
  "took" : 217,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "idx_number_type",
        "_type" : "_doc",
        "_id" : "VcapuYYBI9BugZ8gka-v",
        "_score" : 1.0,
        "_source" : {
          "student_id" : 101,
          "height" : 175.5,
          "weight" : 68.7,
          "score" : 99.99
        }
      }
    ]
  }
}
9、rank_feature类型

rank_feature类型的字段可以存储数字,并且对搜索文档的分数有所影响。

文档的分数就是用户搜索的内容和搜索返回文档的匹配度,分数越高,匹配度越高。


# rank_feature排名类型
DELETE idx_rank_feature
PUT idx_rank_feature
{
  "mappings": {
    "properties": {
      "page_rank": {
        "type": "rank_feature"
      },
      "url_length": {
        "type": "rank_feature",
        "positive_score_impact": false
      },
      "topics": {
        "type": "rank_features"
      }
    }
  }
}

# 插入索引文档
POST idx_rank_feature/_doc
{
  "url": "http://www.baidu.com",
  "content": "2022",
  "page_rank": 50.3,
  "url_length": 20,
  "topics": {
    "sports": 50,
    "brazil": 30
  }
}

POST idx_rank_feature/_doc
{
  "url": "http://www.google.com",
  "content": "2023",
  "page_rank": 50.3,
  "url_length": 21,
  "topics": {
    "sports": 33,
    "brazil": 45
  }
}

# 条件查询
GET idx_rank_feature/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "content": "2022"
          }
        }
      ],
      "should": [
        {
          "rank_feature":{
            "field":"page_rank"
          }
        }
      ]
    }
  }
}
10、search_as_you_type类型

search_as_you_type字段类型和text字段类型很相似。

语法:

PUT idx_search_as_you_type
{
  "mapping":{
    "properties":{
      "my_field":{
        "type":"search_as_you_type"
      }
    }
  }
}

上面的映射模板,会将my_field字段创建为以下子字段。

创建的字段说明
my_field按照映射中的配置进行分析,如果未配置分词器,则使用默认的分词器
my_field._2gram用大小为2的single token filter分词器对my_field进行分词
my_field._3gram用大小为3的single token filter分词器对my_field进行分词
my_field._index_prefix用edge ngram token filter打包my_field._3gram的分词器
# search_as_you_type类型
# 创建my_field字段为search_as_you_type类型
DELETE idx_search_as_you_type
PUT idx_search_as_you_type
{
  "mappings": {
    "properties": {
      "content":{
        "type": "search_as_you_type"
      }
    }
  }
}

# 插入索引文档
PUT idx_search_as_you_type/_doc/1?refresh
{
  "content":"人活着,不要想太多,想太多,心会累,心累了,你在意什么,什么就会折磨你,你计较什么,什么就会困扰你,你越计较就会越痛苦。所以,要学会放下,有些东西,得之我幸,失之我命,放下了,就会轻装前行。"
}

# 查询索引
GET idx_search_as_you_type/_search
{
  "query": {
    "multi_match": {
      "query": "活着 太多",
      "type": "bool_prefix",
      "fields": [
        "content",
        "content._2gram",
        "content._3gram"
      ]
    }
  }
}

查询结果:

{
  "took" : 31,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 3.2691183,
    "hits" : [
      {
        "_index" : "idx_search_as_you_type",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 3.2691183,
        "_source" : {
          "content" : "人活着,不要想太多,想太多,心会累,心累了,你在意什么,什么就会折磨你,你计较什么,什么就会困扰你,你越计较就会越痛苦。所以,要学会放下,有些东西,得之我幸,失之我命,放下了,就会轻装前行。"
        }
      }
    ]
  }
}
11、ip类型
# ip类型
# 创建索引映射
PUT idx_ip_type
{
  "mappings": {
    "properties": {
      "ip_addr": {
        "type": "ip"
      }
    }
  }
}

# 插入文档
PUT idx_ip_type/_doc/1
{
  "ip_addr":"192.168.1.1"
}

PUT idx_ip_type/_doc/2
{
  "ip_addr":"192.168.1.2"
}

GET idx_ip_type/_search

查询结果:

{
  "took" : 12,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "idx_ip_type",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "ip_addr" : "192.168.1.1"
        }
      },
      {
        "_index" : "idx_ip_type",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "ip_addr" : "192.168.1.2"
        }
      }
    ]
  }
}

精确查找:

GET idx_ip_type/_search
{
  "query": {
    "term": {
      "ip_addr": "192.168.1.1"
    }
  }
}

结果:

{
  "took" : 88,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "idx_ip_type",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "ip_addr" : "192.168.1.1"
        }
      }
    ]
  }
}

范围查找:

GET idx_ip_type/_search
{
  "query": {
    "terms": {
      "ip_addr": [
        "192.168.1.1",
        "192.168.1.3"
      ]
    }
  }
}

结果:

{
  "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "idx_ip_type",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "ip_addr" : "192.168.1.1"
        }
      },
      {
        "_index" : "idx_ip_type",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "ip_addr" : "192.168.1.3"
        }
      }
    ]
  }
}
12、token_count类型

token_count(令牌统计)类型的字段实际上是一个integer类型的字段,它可以对内容进行分词分析,存储内容被分词的数量。

# token_count类型
# 创建name字段添加子字段,其名为length,类型为token_count,使用standard分词器分词
PUT idx_tokencount
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text",
        "fields": {
          "length": {
            "type": "token_count",
            "analyzer": "standard"
          }
        }
      }
    }
  }
}

PUT idx_tokencount/_doc/1
{
  "name":"John Smith"
}

PUT idx_tokencount/_doc/2
{
  "name":"Rachel Alice Williams"
}

# 查询分词后,分词数量等于2的文档
GET idx_tokencount/_search
{
  "query": {
    "term": {
      "name.length": {
        "value": "2"
      }
    }
  }
}

查询结果:

{
  "took" : 20,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "idx_tokencount",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "John Smith"
        }
      }
    ]
  }
}

分词

GET idx_tokencount/_analyze
{
  "analyzer": "standard",
  "text": [
    "John Smith"
  ]
}

结果:

{
  "tokens" : [
    {
      "token" : "john",
      "start_offset" : 0,
      "end_offset" : 4,
      "type" : "<ALPHANUM>",
      "position" : 0
    },
    {
      "token" : "smith",
      "start_offset" : 5,
      "end_offset" : 10,
      "type" : "<ALPHANUM>",
      "position" : 1
    }
  ]
}
13、object类型
# object类型
# 创建索引文档
PUT idx_object_type
{
  "mappings": {
    "properties": {
      "region": {
        "type": "keyword"
      },
      "manager": {
        "properties": {
          "name": {
            "properties": {
              "first_name": {
                "type": "text"
              },
              "last_name": {
                "type": "text"
              }
            }
          },
          "age": {
            "type": "integer"
          }
        }
      }
    }
  }
}

# 使用嵌套JSON格式写入
PUT idx_object_type/_doc/1
{
  "region":"China",
  "manager":{
    "name":{
      "first_name":"昭先",
      "last_name":"王"
    },
    "age":23
  }
}

# 使用简单JSON的格式写入
PUT idx_object_type/_doc/2
{
  "region": "China",
  "manager.name.first_name": "四",
  "manager.name.last_name": "李",
  "manager.age": 30
}

GET idx_object_type/_search

查询结果:

{
  "took" : 685,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "idx_object_type",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "region" : "China",
          "manager" : {
            "name" : {
              "first_name" : "昭先",
              "last_name" : "王"
            },
            "age" : 23
          }
        }
      },
      {
        "_index" : "idx_object_type",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "region" : "China",
          "manager.name.first_name" : "四",
          "manager.name.last_name" : "李",
          "manager.age" : 30
        }
      }
    ]
  }
}
14、geo_point类型

语法:

PUT idx_geo_point
{
  "mapping":{
    "properties":{
      "location":{
        "type":"geo_point"
      }
    }
  }
}

案例:

# geo_point
PUT idx_geo_point_type
{
  "mappings": {
    "properties": {
      "hotel_name":{
        "type": "text"
      },
      "location": {
        "type": "geo_point"
      }
    }
  }
}

PUT idx_geo_point_type/_doc/1
{
  "hotel_name": "国泰大酒店",
  "location": {
    "lat": 38.17,
    "lon": 114.58
  }
}

GET idx_geo_point_type/_search

查询结果:

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "idx_geo_point_type",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "hotel_name" : "国泰大酒店",
          "location" : {
            "lat" : 38.17,
            "lon" : 114.58
          }
        }
      }
    ]
  }
}

搜索两个地理位置形成的矩形范围内的信息


# 搜索两个地理位置形成的矩形范围内的信息
GET idx_geo_point_type/_search
{
  "query": {
    "bool": {
      "must": [
        {"match_all": {}}
      ],
      "filter": [
        {"geo_bounding_box": {
          "location": {
            "top_left": {
              "lat": 40.01,
              "lon": 113.32
            },
            "bottom_right": {
              "lat": 38.12,
              "lon": 114.59
            }
          }
        }}
      ]
    }
  }
}

查询结果:

{
  "took" : 6,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "idx_geo_point_type",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "hotel_name" : "国泰大酒店",
          "location" : {
            "lat" : 38.17,
            "lon" : 114.58
          }
        }
      }
    ]
  }
}

搜索多个地理位置形成的多边形范围内的信息


# 搜索多个地理位置形成的多边形范围内的信息
GET idx_geo_point_type/_search
{
  "query": {
    "bool": {
      "must": [
        { "match_all": {}}
      ],
      "filter": [
        {"geo_polygon": {
          "location": {
            "points": [
              {
                "lat": 40.73,
                "lon": 112.10
              },
              {
                "lat": 38.12,
                "lon": 114.80
              },
              {
                "lat": 37.83,
                "lon": 110.12
              }
            ]
          }
        }}
      ]
    }
  }
}

查询结果:

{
  "took" : 37,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "idx_geo_point_type",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "hotel_name" : "国泰大酒店",
          "location" : {
            "lat" : 38.17,
            "lon" : 114.58
          }
        }
      }
    ]
  }
}

搜索指定地理位置100km范围内的信息

# 搜索指定地理位置100km范围内的信息
GET idx_geo_point_type/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        }
      ],
      "filter": [
        {
          "geo_distance": {
            "distance": "100km",
            "location": {
              "lat": 38.12,
              "lon": 114.10
            }
          }
        }
      ]
    }
  }
}

查询结果:

{
  "took" : 11,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "idx_geo_point_type",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "hotel_name" : "国泰大酒店",
          "location" : {
            "lat" : 38.17,
            "lon" : 114.58
          }
        }
      }
    ]
  }
}

15、geo_shape类型

geo_shape(空间位置)类型支持地理形状的搜索。

支持点,线,权,多边形搜索。


# geo_shape
# 创建索引映射
PUT idx_geo_shape_type
{
  "mappings": {
    "properties": {
      "location":{
        "type": "geo_shape"
      }
    }
  }
}

# 插入索引文档
POST idx_geo_shape_type/_doc?refresh
{
  "name": "海淀区,晴天,东风1级",
  "location": {
    "type": "point",
    "coordinates": [116.20,39.88]
  }
}

# 搜索指定两个地址位置范围的地点
GET idx_geo_shape_type/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        }
      ],
      "filter": [
        {
          "geo_shape": {
            "location": {
              "shape": {
                "type": "envelope",
                "coordinates": [
                  [
                    116.1,
                    39.9
                  ],
                  [
                    116.3,
                    39.1
                  ]
                ]
              },
              "relation": "within"
            }
          }
        }
      ]
    }
  }
}

查询结果:

{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "idx_geo_shape_type",
        "_type" : "_doc",
        "_id" : "XMbGv4YBI9BugZ8gqa9Z",
        "_score" : 1.0,
        "_source" : {
          "name" : "海淀区,晴天,东风1级",
          "location" : {
            "type" : "point",
            "coordinates" : [
              116.2,
              39.88
            ]
          }
        }
      }
    ]
  }
}
16、keyword类型

keyword类型用于存储结构化的内容。

此类型通常用于排序,聚合查询等等。

查询过程不会对字段内容进行分词,需要完全匹配。


# keyword类型
PUT idx_keyword_type
{
  "mappings": {
    "properties": {
      "tag":{
        "type": "keyword"
      }
    }
  }
}

PUT idx_keyword_type/_doc/1
{
  "tag":"中国"
}

PUT idx_keyword_type/_doc/2
{
  "tag":"中国北京人"
}

GET idx_keyword_type/_doc/_search?q=tag:中国

查询结果:

{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.6931471,
    "hits" : [
      {
        "_index" : "idx_keyword_type",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.6931471,
        "_source" : {
          "tag" : "中国"
        }
      }
    ]
  }
}
17、text类型

text类型进行全文搜索。

text类型允许用户在每个全文字段中搜索单个单词。

text类型的字段不适合进行排序,也不适合进行聚合计算。

如果字段需要聚合计算和排序推荐使用keyword

keyword类型和text类型的区别:

​ keyword类型字段内容不会被分词

​ text类型字段内容会被分词


# text类型
PUT idx_text_type
{
  "mappings": {
    "properties": {
      "content": {
        "type": "text"
      }
    }
  }
}

PUT idx_text_type/_doc/1
{
  "content":"山东省"
}

PUT idx_text_type/_doc/2
{
  "content":"河南省"
}

GET idx_text_type/_search
{
  "query": {
    "match": {
      "content": "海南省"
    }
  }
}

查询结果:

{
  "took" : 196,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.8754687,
    "hits" : [
      {
        "_index" : "idx_text_type",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.8754687,
        "_source" : {
          "content" : "河南省"
        }
      },
      {
        "_index" : "idx_text_type",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.18232156,
        "_source" : {
          "content" : "山东省"
        }
      }
    ]
  }
}

想要对文本字段执行更多操作,可以使用多字段映射,其中既有text类型用于全文检索,又有keyword类型可用聚合分析。


PUT idx_text_keyword_type
{
  "mappings": {
    "properties": {
      "tagname": {
        "type": "text",
        "fields": {
          "kw": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

PUT idx_text_keyword_type/_doc/1
{
  "tagname":"江苏省"
}

PUT idx_text_keyword_type/_doc/2
{
  "tagname":"山东省"
}

GET idx_text_keyword_type/_search
{
  "query": {
    "match": {
      "tagname": "河南省"
    }
  }
}

查询结果:

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.18232156,
    "hits" : [
      {
        "_index" : "idx_text_keyword_type",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.18232156,
        "_source" : {
          "tagname" : "江苏省"
        }
      },
      {
        "_index" : "idx_text_keyword_type",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.18232156,
        "_source" : {
          "tagname" : "山东省"
        }
      }
    ]
  }
}

不分词搜索

GET idx_text_keyword_type/_search
{
  "query": {
    "match": {
      "tagname.kw": "山东省"
    }
  }
}

结果:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.6931471,
    "hits" : [
      {
        "_index" : "idx_text_keyword_type",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.6931471,
        "_source" : {
          "tagname" : "山东省"
        }
      }
    ]
  }
}

总结:

# 字段类型
# alias类型
PUT /user_info
{
  "mappings": {
    "properties": {
      "id":{
        "type": "long"
      },
      "user_id":{
        "type": "alias",
        "path":"id"
      },
      "user_name":{
        "type": "keyword"
      }
    }
  }
}

PUT /user_info/_doc/1
{
  "id":1,
  "user_name":"欧冶子"
}

GET /user_info/_doc/_search
{
  "query": {
    "range": {
      "user_id": {
        "gte": 1
      }
    }
  }
}

# 数组类型
PUT idx_student_info/_doc/1
{
  "student_name": "小花",
  "tags": [
    "优秀团员",
    "三好学生"
  ],
  "interest_lists": [
    {
      "name": "篮球",
      "describe": "练球三小时"
    },
    {
      "name": "跑步",
      "describe": "每天五公里"
    }
  ]
}

PUT idx_student_info/_doc/2
{
  "student_name": "小明",
  "tags":"三好学生",
  "interest_lists": {
      "name": "篮球",
      "describe": "练球三小时"
    }
}

GET idx_student_info/_search
{
  "query":{
    "match":{
      "tags":"三好学生"
    }
  }
}

# binary类型
PUT idx_binary
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text"
      },
      "content": {
        "type": "binary"
      }
    }
  }
}

PUT idx_binary/_doc/1
{
  "title":"每日一思",
  "content":"55Sf5rS75pei6KaB5bC95b+D77yM5Lmf6KaB6ZqP5b+D77yM5pei6KaB5Yaz5b+D77yM5Lmf6KaB5byA5b+D44CC5b+D5pyJ6Ziz5YWJ77yM5aSE5aSE5ZKM5ZaE5a6J5a6B77yb5b+D5oCA6LGB6L6+77yM5aSE5aSE5rW36ZiU5aSp56m644CC6LWw6L+H5Z2O5Z2377yM5pu05Yqg5Z2a5by677yM6Zev6L+H6aOO6Zuo77yM5pu05Yqg5LmQ6KeC77yM5LiA5Liq5bm06b6E5LiA5Liq5b+D5oCB77yM5b6A5ZCO5L2Z55Sf77yM5YGa5LiA5Liq566A5Y2V5b+r5LmQ55qE5Lq644CC"
}

GET idx_binary/_search

# boolean类型
PUT idx_boolean
{
  "mappings": {
    "properties": {
      "is_published":{
        "type": "boolean"
      }
    }
  }
}

POST idx_boolean/_doc
{
  "is_published":true
}

GET idx_boolean/_search

# date类型
PUT idx_date_1
{
  "mappings": {
    "properties": {
      "add_date":{
        "type": "date"
      }
    }
  }
}

POST idx_date_1/_doc
{
  "add_date":"2023-03-06"
}

GET idx_date_1/_search

PUT idx_date_2
{
  "mappings": {
    "properties": {
      "add_date":{
        "type": "date",
        "format": "yyyy/MM/dd"
      }
    }
  }
}

POST idx_date_2/_doc
{
  "add_date":"2023/03/06"
}

GET idx_date_2/_search

# nested -- 嵌套类型
# 创建索引并指定user字段未一个嵌套类型
PUT idx_object_nested
{
  "mappings": {
    "properties": {
      "user":{
        "type": "nested"
      }
    }
  }
}

# 在索引中插入文档数据,user字段中嵌套了键值对
POST idx_object_nested/_doc
{
  "group": "fans",
  "user": [
    {
      "first_name": "John",
      "last_name": "Smith"
    },
    {
      "first_name": "Alice",
      "last_name": "White"
    }
  ]
}

# 查询user索引库中user.first_name的值为Alice
GET idx_object_nested/_search
{
  "query": {
    "nested": {
      "path": "user",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "user.first_name": "Alice"
              }
            },
            {
              "match": {
                "user.last_name": "Smith"
              }
            }
          ]
        }
      }
    }
  }
}

# range类型
# 创建一个索引映射,
# 指定expected_attendees为整数类范围类型
# 指定time_frame字段类型为日期范围类型
PUT idx_range
{
  "mappings": {
    "properties": {
      "expected_attendees":{
        "type": "integer_range"
      },
      "time_frame":{
        "type": "date_range",
        "format": "yyyy-MM-dd HH:mm:ss||yyy-MM-dd||epoch_millis"
      }
    }
  }
}
# 插入索引文档
POST idx_range/_doc
{
  "expected_attendees": {
    "gte": 10,
    "lt": 20
  },
  "time_frame":{
    "gte":"2023-03-01 10:10:10",
    "lt":"2023-05-01"
  }
}

GET idx_range/_search
{
  "query": {
    "term": {
      "expected_attendees": {
        "value": "22"
      }
    }
  }
}

GET idx_range/_search
{
  "query": {
    "range": {
      "time_frame": {
        "gte": "2023-04-04 00:00:00",
        "lte": "2023-04-05"
      }
    }
  }
}

# 数字类型
PUT idx_number_type
{
  "mappings": {
    "properties": {
      "student_id": {
        "type": "integer"
      },
      "height": {
        "type": "float"
      },
      "weight": {
        "type": "double"
      },
      "score": {
        "type": "scaled_float",
        "scaling_factor": 100
      }
    }
  }
}

POST idx_number_type/_doc
{
  "student_id": 101,
  "height": 175.5,
  "weight": 68.7,
  "score": 99.99
}

GET idx_number_type/_search

# rank_feature排名类型
DELETE idx_rank_feature
PUT idx_rank_feature
{
  "mappings": {
    "properties": {
      "page_rank": {
        "type": "rank_feature"
      },
      "url_length": {
        "type": "rank_feature",
        "positive_score_impact": false
      },
      "topics": {
        "type": "rank_features"
      }
    }
  }
}

# 插入索引文档
POST idx_rank_feature/_doc
{
  "url": "http://www.baidu.com",
  "content": "2022",
  "page_rank": 50.3,
  "url_length": 20,
  "topics": {
    "sports": 50,
    "brazil": 30
  }
}

POST idx_rank_feature/_doc
{
  "url": "http://www.google.com",
  "content": "2023",
  "page_rank": 50.3,
  "url_length": 21,
  "topics": {
    "sports": 33,
    "brazil": 45
  }
}

# 条件查询
GET idx_rank_feature/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "content": "2022"
          }
        }
      ],
      "should": [
        {
          "rank_feature":{
            "field":"page_rank"
          }
        }
      ]
    }
  }
}

# search_as_you_type类型
# 创建my_field字段为search_as_you_type类型
DELETE idx_search_as_you_type
PUT idx_search_as_you_type
{
  "mappings": {
    "properties": {
      "content":{
        "type": "search_as_you_type"
      }
    }
  }
}

# 插入索引文档
PUT idx_search_as_you_type/_doc/1?refresh
{
  "content":"人活着,不要想太多,想太多,心会累,心累了,你在意什么,什么就会折磨你,你计较什么,什么就会困扰你,你越计较就会越痛苦。所以,要学会放下,有些东西,得之我幸,失之我命,放下了,就会轻装前行。"
}

# 查询索引
GET idx_search_as_you_type/_search
{
  "query": {
    "multi_match": {
      "query": "活着 太多",
      "type": "bool_prefix",
      "fields": [
        "content",
        "content._2gram",
        "content._3gram"
      ]
    }
  }
}

# ip类型
# 创建索引映射
PUT idx_ip_type
{
  "mappings": {
    "properties": {
      "ip_addr": {
        "type": "ip"
      }
    }
  }
}

# 插入文档
PUT idx_ip_type/_doc/1
{
  "ip_addr":"192.168.1.1"
}

PUT idx_ip_type/_doc/2
{
  "ip_addr":"192.168.1.2"
}

PUT idx_ip_type/_doc/3
{
  "ip_addr":"192.168.1.3"
}

GET idx_ip_type/_search


GET idx_ip_type/_search
{
  "query": {
    "terms": {
      "ip_addr": [
        "192.168.1.1",
        "192.168.1.3"
      ]
    }
  }
}

# token_count类型
# 创建name字段添加子字段,其名为length,类型为token_count,使用standard分词器分词
PUT idx_tokencount
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text",
        "fields": {
          "length": {
            "type": "token_count",
            "analyzer": "standard"
          }
        }
      }
    }
  }
}

PUT idx_tokencount/_doc/1
{
  "name":"John Smith"
}

PUT idx_tokencount/_doc/2
{
  "name":"Rachel Alice Williams"
}

# 查询分词后,分词数量等于2的文档
GET idx_tokencount/_search
{
  "query": {
    "term": {
      "name.length": {
        "value": "2"
      }
    }
  }
}


GET idx_tokencount/_analyze
{
  "analyzer": "standard",
  "text": [
    "John Smith"
  ]
}

# object类型
# 创建索引文档
PUT idx_object_type
{
  "mappings": {
    "properties": {
      "region": {
        "type": "keyword"
      },
      "manager": {
        "properties": {
          "name": {
            "properties": {
              "first_name": {
                "type": "text"
              },
              "last_name": {
                "type": "text"
              }
            }
          },
          "age": {
            "type": "integer"
          }
        }
      }
    }
  }
}

# 使用嵌套JSON格式写入
PUT idx_object_type/_doc/1
{
  "region":"China",
  "manager":{
    "name":{
      "first_name":"昭先",
      "last_name":"王"
    },
    "age":23
  }
}

# 使用简单JSON的格式写入
PUT idx_object_type/_doc/2
{
  "region": "China",
  "manager.name.first_name": "四",
  "manager.name.last_name": "李",
  "manager.age": 30
}

GET idx_object_type/_search


# geo_point
PUT idx_geo_point_type
{
  "mappings": {
    "properties": {
      "hotel_name":{
        "type": "text"
      },
      "location": {
        "type": "geo_point"
      }
    }
  }
}

PUT idx_geo_point_type/_doc/1
{
  "hotel_name": "国泰大酒店",
  "location": {
    "lat": 38.17,
    "lon": 114.58
  }
}

GET idx_geo_point_type/_search


# 搜索指定的两个地理位置形成的矩形范围内的信息
GET idx_geo_point_type/_search
{
  "query": {
    "bool": {
      "must": [
        {"match_all": {}}
      ],
      "filter": [
        {"geo_bounding_box": {
          "location": {
            "top_left": {
              "lat": 40.01,
              "lon": 113.32
            },
            "bottom_right": {
              "lat": 38.12,
              "lon": 114.59
            }
          }
        }}
      ]
    }
  }
}


# 搜索多个地理位置形成的多边形范围内的信息
GET idx_geo_point_type/_search
{
  "query": {
    "bool": {
      "must": [
        { "match_all": {}}
      ],
      "filter": [
        {"geo_polygon": {
          "location": {
            "points": [
              {
                "lat": 40.73,
                "lon": 112.10
              },
              {
                "lat": 38.12,
                "lon": 114.80
              },
              {
                "lat": 37.83,
                "lon": 110.12
              }
            ]
          }
        }}
      ]
    }
  }
}

# 搜索指定地理位置1000km范围内的信息
GET idx_geo_point_type/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        }
      ],
      "filter": [
        {
          "geo_distance": {
            "distance": "100km",
            "location": {
              "lat": 38.12,
              "lon": 114.10
            }
          }
        }
      ]
    }
  }
}

# geo_shape
# 创建索引映射
PUT idx_geo_shape_type
{
  "mappings": {
    "properties": {
      "location":{
        "type": "geo_shape"
      }
    }
  }
}

# 插入索引文档
POST idx_geo_shape_type/_doc?refresh
{
  "name": "海淀区,晴天,东风1级",
  "location": {
    "type": "point",
    "coordinates": [116.20,39.88]
  }
}

# 搜索指定两个地址位置范围的地点
GET idx_geo_shape_type/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        }
      ],
      "filter": [
        {
          "geo_shape": {
            "location": {
              "shape": {
                "type": "envelope",
                "coordinates": [
                  [
                    116.1,
                    39.9
                  ],
                  [
                    116.3,
                    39.1
                  ]
                ]
              },
              "relation": "within"
            }
          }
        }
      ]
    }
  }
}

# keyword类型
PUT idx_keyword_type
{
  "mappings": {
    "properties": {
      "tag":{
        "type": "keyword"
      }
    }
  }
}

PUT idx_keyword_type/_doc/1
{
  "tag":"中国"
}

PUT idx_keyword_type/_doc/2
{
  "tag":"中国北京人"
}

GET idx_keyword_type/_doc/_search?q=tag:中国

# text类型
PUT idx_text_type
{
  "mappings": {
    "properties": {
      "content": {
        "type": "text"
      }
    }
  }
}

PUT idx_text_type/_doc/1
{
  "content":"山东省"
}

PUT idx_text_type/_doc/2
{
  "content":"河南省"
}

GET idx_text_type/_search
{
  "query": {
    "match": {
      "content": "海南省"
    }
  }
}


PUT idx_text_keyword_type
{
  "mappings": {
    "properties": {
      "tagname": {
        "type": "text",
        "fields": {
          "kw": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

PUT idx_text_keyword_type/_doc/1
{
  "tagname":"江苏省"
}

PUT idx_text_keyword_type/_doc/2
{
  "tagname":"山东省"
}

GET idx_text_keyword_type/_search
{
  "query": {
    "match": {
      "tagname": "河南省"
    }
  }
}


GET idx_text_keyword_type/_search
{
  "query": {
    "match": {
      "tagname.kw": "山东省"
    }
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值