您的位置:首页 > 其它

elasticsearch数据建模

2018-07-07 15:18 162 查看

类似于mysql数据库主外键,三范式 ,先根据条件查询到主键,再根据主键查询到对应的数据

PUT /website/users/1
{
"name" : "小鱼儿",
"email" : "1690780260@QQ.com",
"birthday" : "1997-08-03"
}

PUT /website/blog/1
{
"title" : "小鱼儿的第一篇博客",
"content" : "这是我的第一篇博客,开通了",
"userId" : 1
}

GET /website/users/_search
{
"query": {
"constant_score": {
"filter": {
"terms": {
"name.keyword": [
"小鱼儿"
]
}
}
}
}
}

GET /website/blog/_search
{
"query": {
"constant_score": {
"filter": {
"term": {
"userId": 1
}
}
}
}
}

每个博主下的前五篇博客

GET /website/blogs/_search
{
"size": 0,
"aggs": {
"group_by_name": {
"terms": {
"field": "userInfo.userName.keyword"
},
"aggs": {
"top_title": {
"top_hits": {    //控制为前5篇博客
"_source": {   //控制搜索结果中显示那些内容,这里只包含title,也就是只显示title
"include": "title"
},
"size": 5
}
}
}
}
}
}

文件系统建模

//自定义分词器,并建立索引
PUT /fs
{
"settings": {
"analysis": {
"analyzer": {
"paths" : {
"tokenizer" : "path_hierarchy"
}
}
}
},
"_mapping" : {
"file" : {
"properties" : {
"name" : {
"type" : "keyword"
},
"path" : {
"type" : "keyword",
"fields" : {
"tree" : {
"type" : "text",
"analyzer" : "paths"
}
}
}
}
}
}
}
//填充数据
PUT /fs/file/1
{
"name":     "README.txt",
"path":     "/workspace/projects/helloworld",
"contents": "这是我的第一个elasticsearch程序"
}
//搜索指定目录下文件内容包含elasticserch的文件
GET /fs/file/_search
{
"query": {
"bool": {
"must": [
{"match": {
"contents": "elasticsearch"
}},
{"constant_score": {
"filter": {
"match": {
"path": "/workspace/projects/helloworld"
}
}
}}
]
}
}
}
//因为path的分词器是path_hierarchy  所以可以使用部分路径进行搜素
GET /fs/file/_search
{
"query": {
"bool": {
"must": [
{"match": {
"contents": "elasticsearch"
}},
{"constant_score": {
"filter": {
"match": {
"path": "/workspace"
}
}
}}
]
}
}
}

elasticsearch 中 index级别的锁

GET /fs/file/_search

PUT /fs/block/global/_create
{}       //加锁
//如果在此时间点,如果在另一个客户端进行put,或者是post操作,是会报错的

POST /fs/file/1/_update
{
"doc" : {
"name" : "README1.txt"
}
}

DELETE /fs/block/global  //解锁
阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: