导入数据
1.创建索引——put 索引名字
author、title、id :text类型(字符串)paragraphs :数组类型
PUT song
{
"mappings": {
"properties": {
"author":{
"type": "text"
},
"title":{
"type": "text"
},
"paragraphs":{
"index": "true",
"type": "keyword"
},
"id":{
"type": "text"
}
}
}
}
执行结果
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-iYWtlCHK-1651932343192)(assets/image-20220409153537-wn4zg3e.png)]](https://img-blog.csdnimg.cn/187056eb3a4749559df08b3b7634919b.png)
2.插入数据——put 索引/_doc/数字
PUT song/_doc/1
{
"author": "宋太祖",
"paragraphs": [
"欲出未出光辣達,千山萬山如火發。",
"須臾走向天上來,逐却殘星趕却月。"
],
"title": "日詩",
"id": "08e41396-2809-423d-9bbc-1e6fb24c0ca1"
}
查询数据
1.全局查询——GET 索引/_search
GET song/_search
{
"query": {
"match_all": {}
}
}
查询结果
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jBIwunut-1651932343193)(assets/image-20220409153855-dntpe2r.png)]](https://img-blog.csdnimg.cn/5874ea55529047af8d9fd7dd93ab2f91.png)
2.判断索引是否存在——head
GET song/_count
查询成功
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pSAzx5eu-1651932343194)(assets/image-20220409164752-iwxpxhb.png)]](https://img-blog.csdnimg.cn/6070f9a17e014154905cde82b1f12816.png)
查询失败
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-N3vQSE3g-1651932343194)(assets/image-20220409170841-ro4w4ur.png)]](https://img-blog.csdnimg.cn/4ef52cc3320c48e1af55eb3cae7ae56b.png)
统计数据
1.分词统计
创建索引
创建索引时添加字段"fielddata": "true"
例如
PUT song
{
"mappings": {
"properties": {
"author":{
"type": "text"
},
"title":{
"type": "text"
},
"paragraphs":{
"analyzer": "ik_max_word",
"type": "text",
"fielddata": "true"
},
"id":{
"type": "text"
}
}
}
}
字段查询
在插入完数据后,进行分词查询
GET song/_search?pretty
{
"size" : 0,
"aggs" : {
"messages" : {
"terms" : {
"size" : 10,
"field" : "paragraphs"
}
}
}
}
查询结果
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ahSNZ3Ny-1651932343194)(assets/image-20220409172104-77g2646.png)]](https://img-blog.csdnimg.cn/4a0d41fc749d4e38bfd834caa866e8d7.png)
|