Elasticsearch个人心得笔记(三)
目录
一.文档管理操作
1.新增文档数据
curl -XPUT -d '{"id":"uuid1","title":"es简介","content":"es好用好用真好用"}' http://10.9.151.60:9200/index01/article/1
- index01:文档存储在哪个索引中
- article:index01中新建一个当前文档所属的type
{
"_index": "index01",
"_type": "article",
"_id": "2",
"_version": 1,// es集群中,主从分片判断数据的更新状态使用version
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
_version 用来判断数据是不是最新的,如果主宕机,选举一个从顶替,如果_version不是最新版本则会被pass掉,查询选举
2.获取文档
1.直接获取文档,直到文档id
#curl -XGET http://10.9.151.60:9200/index01/article/1
返回数据结构内容
{
"_index": "index01",
"_type": "article",
"_id": "1",
"_version": 1,
"found": true,
"_source": { //搜索还是直接获取,source中永远存储的是文档数据新增时的内容
"id": "uuid1",
"title": "es简介",
"content": "es好用好用真好用"
}
}
"_source": { //搜索还是直接获取,source中永远存储的是文档数据新增时的内容
"id": "uuid1",
"title": "es简介",
"content": "es好用好用真好用"
}
2.获取多个文档
curl -XGET -d '{"docs":[{"_index":"index01","_type":"article","_id":"1"},{"_index":"index01","_type":"article","_id":"2"}]}' http://10.42.60.249:9200/_mget
{
"docs": [{
"_index": "index01",
"_type": "article",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"id": "uuid1",
"title": "es简介",
"content": "es好用好用真好用"
}
}, {
"_index": "index01",
"_type": "article",
"_id": "2",
"_version": 1,
"found": true,
"_source": {
"id": "uuid1",
"title": "简介",
"content": "大大棒棒糖"
}
}]
}[
3.删除文档
curl -XDELETE http://10.42.60.249:9200/index01/article/1
4.搜索
新增文档时,文档的所有数据是什么类型(所有字符串底层lucene中都是TextField),分词器是什么(standard标准分词器)
1.准备多一些数据
- curl -XPUT -d '{"id":1,"title":"es简介","content":"es好用好用真好用"}' http://10.42.60.249:9200/index01/article/1
- curl -XPUT -d '{"id":1,"title":"java编程思想","content":"这就是个工具书"}' http://10.42.60.249:9200/index01/article/2
- curl -XPUT -d '{"id":1,"title":"大数据简介","content":"你知道什么是大数据吗,就是大数据"}' http://10.42.60.249:9200/index01/article/3
指令就不一一展示了
2.match_all
curl -XGET http://10.42.60.249:9200/index01/_search -d '{"query": {"match_all": {}}}'
3.term query
标准分词器
- curl -XGET http://10.42.60.249:9200/index01/_search -d '{"query":{"term":{"title":"java"}}}'
- curl -XGET http://10.42.60.249:9200/index01/_search -d '{"query":{"term":{"title":"java编程思想"}}}'
- curl -XGET http://10.42.60.249:9200/jtdb_item/_search -d '{"query":{"term":{"title":"双卡双"}}}'
4.match query
分域查询
curl -XGET http://10.42.60.249:9200/index01/_search -d '{"query":{"match":{"title":"java编程思想"}}}'
将查询的字符串 "java编程思想" 按照默认,指定的分词器先进行分词计算 {java},{编},{程},{思},{想},只要域属性中有其中一个词项,查询结果就会返回