当前位置: 移动技术网 > IT编程>开发语言>Java > Elasticsearch Postman简单入门(创建索引并新增)

Elasticsearch Postman简单入门(创建索引并新增)

2018年09月05日  | 移动技术网IT编程  | 我要评论

当然es的安装我就不详细的写了,百度一大堆,本人现在用的是6.2.3 的版本。

1.安装es后我们开始创建索引和mapping;

 

--put http://www.lhsxpumps.com/_localhost:9200/local_mst_student_idx_1   

 local_mst_student_idx_1:代表索引名。

mappings:

{
    "mappings":{
        "mst_student":{
            "properties":{
                "id":{
                    "type":"long",
                    "fields":{
                        "keyword":{
                            "type":"keyword",
                            "ignore_above":256
                        }
                    }
                },
                "stu_code":{
                    "type":"text",
                    "fields":{
                        "keyword":{
                            "type":"keyword",
                            "ignore_above":256
                        }
                    }
                },
                "stu_name":{
                    "type":"text",
                    "fields":{
                        "keyword":{
                            "type":"keyword",
                            "ignore_above":256
                        }
                    }
                },
                "stu_age":{
                    "type":"integer",
                    "fields":{
                        "keyword":{
                            "type":"keyword",
                            "ignore_above":256
                        }
                    }
                },
                "stu_date":{
                    "type":"long",
                    "fields":{
                        "keyword":{
                            "type":"keyword",
                            "ignore_above":256
                        }
                    }
                },
                "stu_bool":{
                    "type":"boolean",
                    "fields":{
                        "keyword":{
                            "type":"keyword",
                            "ignore_above":256
                        }
                    }
                }
            }
        }
    }
}

mapping创建后我们可以查看一下mapping是否已经在es中存在:

--get  http://localhost:9200/local_mst_student_idx_2/_mapping?pretty

 

确认完后我们就开始新增操作了;

--post http://127.0.0.1:9200/local_mst_student_idx_2/mst_student/1

body参数:

{
    "id":"1",
    "stu_code":"1a0001",
    "stu_name":"张三 ",
    "stu_age":"18",
    "stu_date":"1528887157717",
    "stu_bool":"true"
}

新增完成后我们怎么查看到刚才我们新增的数据呢?下面我就写一个根据id查询的请求。

--post  http://www.lhsxpumps.com/_localhost:9200/local_mst_student_idx_2/_search?pretty

bosy参数:

{
    "query":{
        "match":{
            "id":"1"
        }
    }
}

 

我就简单给大家讲解一下上面返回的参数;其实我们只关注一下hits内部的参数值就行了。

took:是查询花费的时间,毫秒单位。

time_out:标识查询是否超时。

_shards:描述了查询分片的信息,查询了多少个分片、成功的分片数量、失败的分片数量等。

hits:搜索的结果,total是全部的满足的文档数目,hits是返回的实际数目(默认是10)。

_score是文档的分数信息,与排名相关度有关,参考各大搜索引擎的搜索结果,就容易理解。

total:1;(代表当前es里总数只有一条数据,不管你发送任何请求,es都会把总数返回)

_index:我们指定查询的索引(类似数据库的某个库)。

_type:我们指定查询的文档(类似数据库的某张表)

_id:查询指定的id。

_source:查询返回数据。

 

看完麻烦给个赞吧,我会继续努力的~

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网