elasticsearch数据建模

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

PUT /website/users/1
{
  "name" : "小鱼儿",
  "email" : "[email protected]",
  "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  //解锁

猜你喜欢

转载自blog.csdn.net/zs18052449719/article/details/80949109