ElasticSearch基本操作之kibana操作

一、ES简单的增删改查

    1、 创建一个test文档(类似于sql的表名),类型为doc的 id为2的数据(字段为:name,age,desc)

PUT test/doc/2
{
  "name":"wangfei",
  "age":"27",
  "desc":"天气打瞌睡"
}
PUT test/doc/1
{
  "name":"wangjifei",
  "age":27,
  "desc":"萨芬我反胃为范围额"
}

PUT test/doc/3
{
  "name":"wangyang",
  "age":30,
  "desc":"点在我心内的几首歌"
}

2、查询指定的索引信息

GET test

3、查询指定文档信息

GET test/doc/1
GET test/doc/2

4、查询集群下有哪些索引

GET /_cat/indices\?v

5.删除指定文档

DELETE test/doc/3

6、删除索引

DELETE test

7、查询对应索引下所有数据

GET test/doc/_search
或
GET test/doc/_search
{
  "query": {
    "match_all": {}
  }
}

8、修改指定文档方式

  • 使用POST命令,在id后面跟_update,要修改的内容放到doc文档(属性)中(正确的修改指定文档方式)
POST test/doc/1/_update
{
  "doc":{
    "desc":"生活就像 茫茫海上",
    "age":"19"
  }
}

二、ES查询的两种方式

1、查询字符串搜索

GET test/doc/_search?q=name:wangfei

2、结构化查询(单字段查询,不能多字段组合查询)

GET test/doc/_search
{
  "query":{
    "match":{
      "name":"wangfei"
    }
  }
}

三、match系列之操作

1、match系列之match_all(查询索引中所有数据)

GET test/doc/_search
{
  "query":{
    "match_all": {
    }
  }
}

2、match系列之match_phrase(短语查询)

     准备数据



PUT test1/doc/1
{
  "title": "中国是世界上人口最多的国家"
}
PUT test1/doc/2
{
  "title": "美国是世界上军事实力最强大的国家"
}
PUT test1/doc/3
{
  "title": "北京是中国的首都"
}

    查询语句

GET test1/doc/_search
{
  "query":{
    "match":{
      "title":"中国"
    }
  }
}

通过观察结果可以发现,虽然如期的返回了中国的文档。但是却把和美国的文档也返回了,这并不是我们想要的。是怎么回事呢?因为这是elasticsearch在内部对文档做分词的时候,对于中文来说,就是一个字一个字分的,所以,我们搜中国,中和国都符合条件,返回,而美国的国也符合。而我们认为中国是个短语,是一个有具体含义的词。所以elasticsearch在处理中文分词方面比较弱势。后面会讲针对中文的插件。但目前我们还有办法解决,那就是使用短语查询 用match_phrase

GET test1/doc/_search
{
  "query":{
    "match_phrase": {
      "title": "中国"
    }
  }
}

我们搜索中国和世界这两个指定词组时,但又不清楚两个词组之间有多少别的词间隔。那么在搜的时候就要留有一些余地。这时就要用到了slop了。相当于正则中的中国.*?世界。这个间隔默认为0

3、match系列之match_phrase_prefix(最左前缀查询)智能搜索--以什么开头

数据准备

数据准备

PUT test2/doc/1
{
  "title": "prefix1",
  "desc": "beautiful girl you are beautiful so"
}

PUT test2/doc/2
{
  "title": "beautiful",
  "desc": "I like basking on the beach"
}

搜索特定英文开头的数据

GET test5/doc/_search
{
  "query": {
    "match_phrase_prefix": {
      "desc": "bea"
    }
  }
}

基于短语查询

GET test5/doc/_search
{
  "query": {
    "match_phrase_prefix": {
      "desc": "you are bea"
    }
  }
}

 

max_expansions 参数理解 前缀查询会非常的影响性能,要对结果集进行限制,就加上这个参数。

GET test5/doc/_search
{
  "query": {
    "match_phrase_prefix": {
      "desc": {
        "query": "bea",
        "max_expansions":1
      }
    }
  }
}

 

4、match系列之multi_match(多字段查询)

  • multi_match是要在多个字段中查询同一个关键字 除此之外,mulit_match甚至可以当做match_phrase和match_phrase_prefix使用,只需要指定type类型即可

GET test2/doc/_search
{
  "query": {
    "multi_match": {
      "query": "beautiful",
      "fields": ["title","desc"]
    }
  }
}

>>查询结果
{
  "took" : 43,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 0.39556286,
    "hits" : [
      {
        "_index" : "test2",
        "_type" : "doc",
        "_id" : "1",
        "_score" : 0.39556286,
        "_source" : {
          "title" : "prefix1",
          "desc" : "beautiful girl you are beautiful so"
        }
      },
      {
        "_index" : "test2",
        "_type" : "doc",
        "_id" : "2",
        "_score" : 0.2876821,
        "_source" : {
          "title" : "beautiful",
          "desc" : "I like basking on the beach"
        }
      }
    ]
  }
}
  • 当设置属性 type:phrase 时 等同于 短语查询

GET test1/doc/_search
{
  "query": {
    "multi_match": {
      "query": "中国",
      "fields": ["title"],
      "type": "phrase"
    }
  }
}

>>>查询结果
{
  "took" : 47,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 0.5753642,
    "hits" : [
      {
        "_index" : "test1",
        "_type" : "doc",
        "_id" : "1",
        "_score" : 0.5753642,
        "_source" : {
          "title" : "中国是世界上人口最多的国家"
        }
      },
      {
        "_index" : "test1",
        "_type" : "doc",
        "_id" : "3",
        "_score" : 0.5753642,
        "_source" : {
          "title" : "北京是中国的首都"
        }
      }
    ]
  }
}
  • 当设置属性 type:phrase_prefix时 等同于 最左前缀查询

GET test2/doc/_search
{
  "query": {
    "multi_match": {
      "query": "bea",
      "fields": ["desc"],
      "type": "phrase_prefix"
    }
  }
}

>>查询结果
{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 0.5753642,
    "hits" : [
      {
        "_index" : "test1",
        "_type" : "doc",
        "_id" : "1",
        "_score" : 0.5753642,
        "_source" : {
          "title" : "中国是世界上人口最多的国家"
        }
      },
      {
        "_index" : "test1",
        "_type" : "doc",
        "_id" : "3",
        "_score" : 0.5753642,
        "_source" : {
          "title" : "北京是中国的首都"
        }
      }
    ]
  }
}

5、match查询相关总结

1、match:返回所有匹配的分词。

2、match_all:查询全部。

3、match_phrase:短语查询,在match的基础上进一步查询词组,可以指定slop分词间隔。

4、match_phrase_prefix:前缀查询,根据短语中最后一个词组做前缀匹配,可以应用于搜索提示,但注意和max_expanions搭配。其实默认是50.......

5、multi_match:多字段查询,使用相当的灵活,可以完成match_phrase和match_phrase_prefix的工作。

详细请看:https://www.jianshu.com/p/3873a6290c65

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值