Elasticsearch文档操作

一、Elasticsearch的CURD

1、CURD之Create

PUT lqz/doc/1
{
   
  "name":"顾老二",
  "age":30,
  "from": "gu",
  "desc": "皮肤黑、武器长、性格直",
  "tags": ["黑", "长", "直"]
}

他明处貌似还有俩老婆:

PUT lqz/doc/2
{
   
  "name":"大娘子",
  "age":18,
  "from":"sheng",
  "desc":"肤白貌美,娇憨可爱",
  "tags":["白", "富","美"]
}

PUT lqz/doc/3
{
   
  "name":"龙套偏房",
  "age":22,
  "from":"gu",
  "desc":"mmp,没怎么看,不知道怎么形容",
  "tags":["造数据", "真","难"]
}

家里红旗不倒,家外彩旗飘摇:

PUT lqz/doc/4
{
   
  "name":"石头",
  "age":29,
  "from":"gu",
  "desc":"粗中有细,狐假虎威",
  "tags":["粗", "大","猛"]
}

PUT lqz/doc/5
{
   
  "name":"魏行首",
  "age":25,
  "from":"广云台",
  "desc":"仿佛兮若轻云之蔽月,飘飘兮若流风之回雪,mmp,最后竟然没有嫁给顾老二!",
  "tags":["闭月","羞花"]
}

注意:当执行PUT命令时,如果数据不存在,则新增该条数据,如果数据存在则修改该条数据。

咱们通过GET命令查询一下:

GET lqz/doc/1

结果如下:

{
   
  "_index" : "lqz",
  "_type" : "doc",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source" : {
   
    "name" : "顾老二",
    "age" : 30,
    "from" : "gu",
    "desc" : "皮肤黑、武器长、性格直",
    "tags" : [
      "黑",
      "长",
      "直"
    ]
  }
}

查询也没啥问题,但是你可能说了,人家老二是黄种人,怎么是黑的呢?好吧咱改改desctags

PUT lqz/doc/1
{
   
  "desc":"皮肤很黄,武器很长,性格很直",
  "tags":["很黄","很长", "很直"]
}

上例,我们仅修改了desctags两处,而nameagefrom三个属性没有变化,我们可以忽略不写吗?查查看:

GET lqz/doc/1

结果如下:

{
   
  "_index" : "lqz",
  "_type" : "doc",
  "_id" : "1",
  "_version" : 3,
  "found" : true,
  "_source" : {
   
    "desc" : "皮肤很黄,武器很长,性格很直",
    "tags" : [
      "很黄",
      "很长",
      "很直"
    ]
  }
}

哎呀,出事故了!修改是修改了,但结果不太理想啊,因为nameagefrom属性都没啦!
注意:**PUT命令,在做修改操作时,如果未指定其他的属性,则按照指定的属性进行修改操作。**也就是如上例所示的那样,我们修改时只修改了desctags两个属性,其他的属性并没有一起添加进去。

很明显,这是病!dai治!怎么治?上车,咱们继续往下走!

2、CURD之Update

让我们首先恢复一下事故现场:

PUT lqz/doc/1
{
   
  "name":"顾老二",
  "age":30,
  "from": "gu",
  "desc": "皮肤黑、武器长、性格直",
  "tags": ["黑", "长", "直"]
}

我们要将黑修改成黄:

POST lqz/doc/1/_update
{
   
  "doc": {
   
    "desc": "皮肤很黄,武器很长,性格很直",
    "tags": ["很黄","很长", "很直"]
  }
}

上例中,我们使用POST命令,在id后面跟_update,要修改的内容放到doc文档(属性)中即可。

我们再来查询一次:

GET lqz/doc/1

结果如下:

{
   
  "_index" : "lqz",
  "_type" : "doc",
  "_id" : "1",
  "_version" : 5,
  "found" : true,
  "_source" : {
   
    "name" : "顾老二",
    "age" : 30,
    "from" : "gu",
    "desc" : "皮肤很黄,武器很长,性格很直",
    "tags" : [
      "很黄",
      "很长",
      "很直"
    ]
  }
}

结果如上例所示,现在其他的属性没有变化,只有desctags属性被修改。

注意:POST命令,这里可用来执行修改操作(还有其他的功能),POST命令配合_update完成修改操作,指定修改的内容放到doc

写了这么多,我也发现我上面有讲的不对的地方——石头不是跟顾老二不清不楚,石头是跟小桃不清不楚!好吧,刚才那个数据是一个错误示范!我们这就把它干掉!

3、CURD之Delete

DELETE lqz/doc/4

很简单,通过DELETE命令,就可以删除掉那个错误示范了!

删除效果如下:

{
   
  "_index" : "lqz",
  "_type" : "doc",
  "_id" : "4",
  "_version" : 4,
  "result" : "deleted",
  "_shards" : {
   
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 4,
  "_primary_term" : 1
}

我们再来查询一遍:

GET lqz/doc/4

结果如下:

{
   
  "_index" : "lqz",
  "_type" : "doc",
  "_id" : "4",
  "found" : false
}

上例中,found:false表示查询数据不存在。

4、CURD之Retrieve

我们上面已经不知不觉的使用熟悉这种简单查询方式,通过 GET命令查询指定文档:

GET lqz/doc/1

结果如下:

{
   
  "_index" : "lqz",
  "_type" : "doc",
  "_id" : "1",
  "_version" : 5,
  "found" : true,
  "_source" : {
   
    "name" : "顾老二",
    "age" : 30,
    "from" : "gu",
    "desc" : "皮肤很黄,武器很长,性格很直",
    "tags" : [
      "很黄",
      "很长",
      "很直"
    ]
  }
}

二、Elasticsearch之查询的两种方式

1、前言

简单的没挑战,来点复杂的,elasticsearch提供两种查询方式:

  • 查询字符串(query string),简单查询,就像是像传递URL参数一样去传递查询语句,被称为简单搜索或查询字符串(query string)搜索。
  • 另外一种是通过DSL语句来进行查询,被称为DSL查询(Query DSL),DSL是Elasticsearch提供的一种丰富且灵活的查询语言,该语言以json请求体的形式出现,通过restful请求与Elasticsearch进行交互。

2、准备数据

PUT lqz/doc/1
{
   
  "name":"顾老二",
  "age":30,
  "from": "gu",
  "desc": "皮肤黑、武器长、性格直",
  "tags": ["黑", "长", "直"]
}

PUT lqz/doc/2
{
   
  "name":"大娘子",
  "age":18,
  "from":"sheng",
  "desc":"肤白貌美,娇憨可爱",
  "tags":["白", "富","美"]
}

PUT lqz/doc/3
{
   
  "name":"龙套偏房",
  "age":22,
  "from":"gu",
  "desc":"mmp,没怎么看,不知道怎么形容",
  "tags":["造数据", "真","难"]
}


PUT lqz/doc/4
{
   
  "name":"石头",
  "age":29,
  "from":"gu",
  "desc":"粗中有细,狐假虎威",
  "tags":["粗", "大","猛"]
}

PUT lqz/doc/5
{
   
  "name":"魏行首",
  "age":25,
  "from":"广云台",
  "desc":"仿佛兮若轻云之蔽月,飘飘兮若流风之回雪,mmp,最后竟然没有嫁给顾老二!",
  "tags":["闭月","羞花"]
}

3、查询字符串

GET lqz/doc/_search?q=from:gu

还是使用GET命令,通过_serarch查询,查询条件是什么呢?条件是from属性是gu家的人都有哪些。最后,别忘了_searchfrom属性中间的英文分隔符?

结果如下:

{
   
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
   
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
   
    "total" : 3,
    "max_score" : 0.6931472,
    "hits" : [
      {
   
        "_index" : "lqz",
        "_type" : "doc",
        "_id" : "4",
        "_score" : 0.6931472,
        "_source" : {
   
          "name" : "石头",
          "age" : 29,
          "from" : "gu",
          "desc" : "粗中有细,狐假虎威",
          "tags" : [
            "粗",
            "大",
            "猛"
          ]
        }
      },
      {
   
        "_index" : "lqz",
        "_type" : "doc",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
   
          "name" : "顾老二",
          "age" : 30,
          "from" : "gu",
          "desc" : "皮肤黑、武器长、性格直",
          "tags" : [
            "黑",
            "长",
            "直"
          ]
        }
      },
      {
   
        "_index" : "lqz",
        "_type" : "doc",
        "_id" : "3",
        "_score" : 0.2876821,
        "_source" : {
   
          "name" : "龙套偏房",
          "age" : 22,
          "from" : "gu",
          "desc" : "mmp,没怎么看,不知道怎么形容",
          "tags" : [
            "造数据",
            "真",
            "难"
          ]
        }
      }
    ]
  }
}

我们来重点说下hitshits是返回的结果集——所有from属性为gu的结果集。重点中的重点是_score得分,得分是什么呢?根据算法算出跟查询条件的匹配度,匹配度高得分就高。后面再说这个算法是怎么回事。

4、结构化查询

我们现在使用DSL方式,来完成刚才的查询,查看来自顾家的都有哪些人。

GET lqz/doc/_search
{
   
  "query": {
   
    "match": {
   
      "from": "gu"
    }
  }
}

上例,查询条件是一步步构建出来的,将查询条件添加到match中即可,而match则是查询所有from字段的值中含有gu的结果就会返回。
当然结果没啥变化:

{
   
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
   
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
   
    "total" : 3,
    "max_score" : 0.6931472,
    "hits" : [
      {
   
        "_index" : "lqz",
        "_type" : "doc",
        "_id" : "4",
        "_score" : 0.6931472,
        "_source" : {
   
          "name" : "石头",
          "age" : 29,
          "from" : "gu",
          "desc" : "粗中有细,狐假虎威",
          "tags" : [
            "粗",
            "大",
            "猛"
          ]
        }
      },
      {
   
        "_index" : "lqz",
        "_type" : "doc",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
   
          "name" : "顾老二",
          "age" : 30,
          "from" : "gu",
          "desc" : "皮肤黑、武器长、性格直",
          "tags" : [
            "黑",
            "长",
            "直"
          ]
        }
      },
      {
   
        "_index" : "lqz",
        "_type" : "doc",
        "_id" : "3",
        "_score" : 0.2876821,
        "_source" : {
   
          "name" : "龙套偏房",
          "age" : 22,
          "from" : "gu",
          "desc" : "mmp,没怎么看,不知道怎么形容",
          "tags" : [
            "造数据",
            "真",
            "难"
          ]
        }
      }
    ]
  }
}

三、term与match查询

1、match查询

1.1 准备数据

PUT lqz/doc/1
{
   
  "name":"顾老二",
  "age":30,
  "from": "gu",
  "desc": "皮肤黑、武器长、性格直",
  "tags": ["黑", "长", "直"]
}

PUT lqz/doc/2
{
   
  "name":"大娘子",
  "age":18,
  "from":"sheng",
  "desc":"肤白貌美,娇憨可爱",
  "tags":["白", "富","美"]
}

PUT lqz/doc/3
{
   
  "name":"龙套偏房",
  "age":22,
  "from":"gu",
  "desc":"mmp,没怎么看,不知道怎么形容",
  "tags":["造数据", "真","难"]
}


PUT lqz/doc/4
{
   
  "name":"石头",
  "age":29,
  "from":"gu",
  "desc":"粗中有细,狐假虎威",
  "tags":["粗", "大","猛"]
}

PUT lqz/doc/5
{
   
  "name":"魏行首",
  "age":25,
  "from":"广云台",
  "desc":"仿佛兮若轻云之蔽月,飘飘兮若流风之回雪,mmp,最后竟然没有嫁给顾老二!",
  "tags":["闭月","羞花"]
}

1.2 match系列之match(按条件查询)

我们查看来自顾家的都有哪些人。

GET lqz/doc/_search
{
   
  "query": {
   
    "match": {
   
      "from": "gu"
    }
  }
}

上例,查询条件是一步步构建出来的,将查询条件添加到match中即可,而match则是查询所有from字段的值中含有gu的结果就会返回。
结果如下:

{
   
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
   
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
   
    "total" : 3,
    "max_score" : 0.6931472,
    "hits" : [
      {
   
        "_index" : "lqz",
        "_type" : "doc",
        "_id" : "4",
        "_score" : 0.6931472,
        "_source" : {
   
          "name" : "石头",
          "age" : 29,
          "from" : "gu",
          "desc" : "粗中有细,狐假虎

你可能感兴趣的:(数据库,elasticsearch)