1. 创建index,命名为twitter
PUT localhost:9200/twitter
{
\\\"settings\\\":{\\\"number_of_shards\\\":1}
}
设置shard分片数为1。
随后,也修改 number_of_replicas=0。
master 节点名称:_Yg0Q2v
2. 为现有索引定义mapping 映射
根据Elasticsearch蕞佳实践,建议完成mapping设定后,再写入document数据。
{
\\\"properties\\\": {
\\\"address\\\": {
\\\"type\\\": \\\"text\\\",
\\\"fields\\\": {
\\\"keyword\\\": {
\\\"type\\\": \\\"keyword\\\",
\\\"ignore_above\\\":256
}
}
},
\\\"city\\\": {
\\\"type\\\": \\\"keyword\\\"
},
\\\"country\\\": {
\\\"type\\\": \\\"keyword\\\"
},
\\\"location\\\": {
\\\"type\\\": \\\"geo_point\\\"
},
\\\"province\\\": {
\\\"type\\\": \\\"keyword\\\"
},
\\\"uid\\\": {
\\\"type\\\": \\\"long\\\"
},
\\\"user\\\": {
\\\"type\\\": \\\"text\\\",
\\\"fields\\\": {
\\\"keyword\\\": {
\\\"type\\\": \\\"keyword\\\",
\\\"ignore_above\\\": 256
}
}
}
}
}
对于只需要做精确匹配的字段,应该设置为不做分词,这里通过type=keyword来设定。
通过 GET twitter/_mapping 验证一下创建完成的mapping映射。
地理位置数据类型:geo_point
地理位置,其值可以有如下四中表现形式:
object对象:\\\"location\\\": {\\\"lat\\\": 41.12, \\\"lon\\\": -71.34}字符串:\\\"location\\\": \\\"41.12,-71.34\\\"geohash:\\\"location\\\": \\\"drm3btev3e86\\\"数组:\\\"location\\\": [ -71.34, 41.12 ]3. 创建具有显式映射的索引
也可以将创建index和mapping 合并在一步完成。
创建索引 twitter2 以及对应的mapping映射。
因为在本地localhost环境,特意设置number_of_shards=1和number_of_replicas=0,生产环境根据实际情况进行设置。
PUT twitter2/
{
\\\"settings\\\":{
\\\"number_of_shards\\\": 1,
\\\"number_of_replicas\\\": 0
},
\\\"mappings\\\": {
\\\"_doc\\\" : {
\\\"properties\\\": {
\\\"address\\\": {
\\\"type\\\": \\\"text\\\",
\\\"fields\\\": {
\\\"keyword\\\": {
\\\"type\\\": \\\"keyword\\\",
\\\"ignore_above\\\":256
}
}
},
\\\"city\\\": {
\\\"type\\\": \\\"keyword\\\"
},
\\\"country\\\": {
\\\"type\\\": \\\"keyword\\\"
},
\\\"location\\\": {
\\\"type\\\": \\\"geo_point\\\"
},
\\\"province\\\": {
\\\"type\\\": \\\"keyword\\\"
},
\\\"uid\\\": {
\\\"type\\\": \\\"long\\\"
},
\\\"user\\\": {
\\\"type\\\": \\\"text\\\",
\\\"fields\\\": {
\\\"keyword\\\": {
\\\"type\\\": \\\"keyword\\\",
\\\"ignore_above\\\": 256
}
}
}
}
}
}
}
上述脚本在Elasticsearch 6.4.3 环境执行成功。
执行返回结果:
{
\\\"acknowledged\\\": true,
\\\"shards_acknowledged\\\": true,
\\\"index\\\": \\\"twitter2\\\"
}
如果在Elasticsearch 7.3.0 环境中,去除\\\"_doc\\\" 节点,可以实现相同的效果,同时创建好索引twitter2和对应的mapping。
在Elasticsearch 7.3.0环境中,执行完成,返回结果。
{
\\\"acknowledged\\\" : true,
\\\"shards_acknowledged\\\" : true,
\\\"index\\\" : \\\"twitter2\\\"
}
如下是创建具有显式映射的索引在Elasticsearch 6.* 和 7.* 版本的官方文档。其中差异部分,进行了标识,主要在7.* 版本中,已经移除了type类型。
https://www.elastic.co/guide/en/elasticsearch/reference/6.4/mapping.html
https://www.elastic.co/guide/en/elasticsearch/reference/7.3/mapping.html
4. 插入document 数据
单条doc插入;
POST twitter/_doc/1
{
\\\"user\\\":\\\"good man\\\",
\\\"uid\\\":1,
\\\"city\\\": \\\"武汉\\\",
\\\"province\\\": \\\"湖北\\\",
\\\"country\\\": \\\"中国\\\",
\\\"location\\\": {
\\\"lat\\\": \\\"29\\\",
\\\"lon\\\": \\\"221\\\"
}
}
批量操作 bulk
POST _bulk
{\\\"index\\\": {\\\"_index\\\": \\\"twitter\\\", \\\"_type\\\":\\\"_doc\\\"}}
{\\\"user\\\":\\\"rickie\\\", \\\"uid\\\": 2, \\\"city\\\":\\\"shanghai\\\", \\\"province\\\":\\\"shanghai\\\",\\\"country\\\":\\\"China\\\", \\\"address\\\":\\\"No. 1801\\\", \\\"location\\\":{\\\"lat\\\":\\\"21\\\", \\\"lon\\\":\\\"100\\\"}}
{\\\"index\\\": {\\\"_index\\\": \\\"twitter\\\", \\\"_type\\\":\\\"_doc\\\"}}
{\\\"user\\\":\\\"abc\\\", \\\"message\\\":\\\"hello world\\\", \\\"uid\\\": 3,\\\"age\\\":20, \\\"city\\\":\\\"上海\\\", \\\"province\\\":\\\"shanghai\\\",\\\"country\\\":\\\"中国\\\", \\\"address\\\":\\\"#1801\\\", \\\"location\\\":{\\\"lat\\\":\\\"21\\\", \\\"lon\\\":\\\"100\\\"}}
{\\\"index\\\": {\\\"_index\\\": \\\"twitter\\\", \\\"_type\\\":\\\"_doc\\\"}}
{\\\"user\\\":\\\"康小姐\\\", \\\"message\\\":\\\"hello world\\\", \\\"uid\\\": 4,\\\"age\\\":20, \\\"city\\\":\\\"北京\\\", \\\"province\\\":\\\"北京\\\",\\\"country\\\":\\\"China\\\", \\\"address\\\":\\\"#1801\\\", \\\"location\\\":{\\\"lat\\\":\\\"21\\\", \\\"lon\\\":\\\"100\\\"}}
{\\\"index\\\": {\\\"_index\\\": \\\"twitter\\\", \\\"_type\\\":\\\"_doc\\\"}}
{\\\"user\\\":\\\"tom\\\", \\\"message\\\":\\\"hello tom\\\", \\\"uid\\\": 5,\\\"age\\\":36, \\\"city\\\":\\\"shanghai\\\", \\\"province\\\":\\\"shanghai\\\",\\\"country\\\":\\\"China\\\", \\\"address\\\":\\\"#1801\\\", \\\"location\\\":{\\\"lat\\\":\\\"21\\\", \\\"lon\\\":\\\"100\\\"}}
{\\\"index\\\": {\\\"_index\\\": \\\"twitter\\\", \\\"_type\\\":\\\"_doc\\\"}}
{\\\"user\\\":\\\"Jacky\\\", \\\"message\\\":\\\"hello Jacky\\\", \\\"uid\\\": 6,\\\"age\\\":40, \\\"city\\\":\\\"shanghai\\\", \\\"province\\\":\\\"shanghai\\\",\\\"country\\\":\\\"China\\\", \\\"address\\\":\\\"#1801\\\", \\\"location\\\":{\\\"lat\\\":\\\"21\\\", \\\"lon\\\":\\\"100\\\"}}
document 插入完成之后,确认一下。
海外精品引流脚本--最强海外引流