Elastic Search的简单操作

前言

启动一个节点和kibana

接下来的一切操作都在kianaDev Tools下的Console完成。

创建一篇文档。

现在,我们试图插入插入数据。

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

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

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

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

PUT zhifou/doc/5
{
  "name":"魏行者",
  "age":25,
  "from":"广云台",
  "desc":"后悔没能嫁给顾老二~",
  "tags":["闭月","羞花"]
}

PUT表示创建命令,虽然命令可以小写,但我们推荐大写。以RESTFul 风格返回结果。

返回结果的信息如下:

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

查询所有索引

现在学习一条命令

GET _cat/indices?v

展示当前集群中索引情况,包括索引的健康状态、UUID、主副分片个数、大小等信息。

查询指定的索引信息

单独查看zhifu索引:

GET zhifou  

返回的结果如下:

{
  "zhifou" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "age" : {
          "type" : "long"
        },
        "desc" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "from" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "tags" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "creation_date" : "1579267138792",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "thVUwigXSNu__npB0NM9Gg",
        "version" : {
          "created" : "7030099"
        },
        "provided_name" : "zhifou"
      }
    }
  }
}

返回了zhifou索引的创建信心。

查询文档信息

查看某个特定的文档信息

GET zhifou/doc/1

返回的文档信息如下:

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

查询所有文档信息

GET zhifou/doc/_search

返回的信息如下:

{
  "took" : 116,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "顾老二",
          "age" : 30,
          "from" : "gu",
          "desc" : "皮肤黑、武器长、性格直",
          "tags" : [
            "黑",
            "长",
            "直"
          ]
        }
      },
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "大娘子",
          "age" : 18,
          "from" : "sheng",
          "desc" : "貌美肤白、娇憨可爱",
          "tags" : [
            "白",
            "富",
            "美"
          ]
        }
      },
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "name" : "龙套偏房",
          "age" : 22,
          "from" : "gu",
          "desc" : "mmp,没怎么看清,不知道怎么形容",
          "tags" : [
            "造数据",
            "真",
            "难"
          ]
        }
      },
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "name" : "石头",
          "age" : 29,
          "from" : "gu",
          "desc" : "粗中有细、狐假虎威",
          "tags" : [
            "粗",
            "大",
            "猛"
          ]
        }
      },
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "5",
        "_score" : 1.0,
        "_source" : {
          "name" : "魏行者",
          "age" : 25,
          "from" : "广云台",
          "desc" : "后悔没能嫁给顾老二~",
          "tags" : [
            "闭月",
            "羞花"
          ]
        }
      }
    ]
  }
}
    

删除指定索引

DELETE zhifou
DELETE zhifou/doc/5 

删除指定的文档

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

此时在存储文档,会发现5已经不存在了。

{
  "took" : 297,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "顾老二",
          "age" : 30,
          "from" : "gu",
          "desc" : "皮肤黑、武器长、性格直",
          "tags" : [
            "黑",
            "长",
            "直"
          ]
        }
      },
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "大娘子",
          "age" : 18,
          "from" : "sheng",
          "desc" : "貌美肤白、娇憨可爱",
          "tags" : [
            "白",
            "富",
            "美"
          ]
        }
      },
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "name" : "龙套偏房",
          "age" : 22,
          "from" : "gu",
          "desc" : "mmp,没怎么看清,不知道怎么形容",
          "tags" : [
            "造数据",
            "真",
            "难"
          ]
        }
      },
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "name" : "石头",
          "age" : 29,
          "from" : "gu",
          "desc" : "粗中有细、狐假虎威",
          "tags" : [
            "粗",
            "大",
            "猛"
          ]
        }
      }
    ]
  }
}

match查询

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

查看来自顾家的都有那些人。

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

上例,查询条件是一步步构建出来的,将查询条件添加到match中即可。

返回结果如下:

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

match系列之match_all(查询全部)

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

match_all条件为空,表示没有查询条件,那就是查询全部。就像select * from table_name一样。

查询结果如下:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "name" : "顾老二",
          "age" : 30,
          "from" : "gu",
          "desc" : "皮肤黑、武器长、性格直",
          "tags" : [
            "黑",
            "长",
            "直"
          ]
        }
      },
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "大娘子",
          "age" : 18,
          "from" : "sheng",
          "desc" : "貌美肤白、娇憨可爱",
          "tags" : [
            "白",
            "富",
            "美"
          ]
        }
      },
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "name" : "龙套偏房",
          "age" : 22,
          "from" : "gu",
          "desc" : "mmp,没怎么看清,不知道怎么形容",
          "tags" : [
            "造数据",
            "真",
            "难"
          ]
        }
      },
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "name" : "石头",
          "age" : 29,
          "from" : "gu",
          "desc" : "粗中有细、狐假虎威",
          "tags" : [
            "粗",
            "大",
            "猛"
          ]
        }
      },
      {
        "_index" : "zhifou",
        "_type" : "doc",
        "_id" : "5",
        "_score" : 1.0,
        "_source" : {
          "name" : "魏行者",
          "age" : 25,
          "from" : "广云台",
          "desc" : "后悔没能嫁给顾老二~",
          "tags" : [
            "闭月",
            "羞花"
          ]
        }
      }
    ]
  }
}

match系列之match_phrase(短语查询)

我们现在已经对match有了基础的了解。match查询的是散列映射,包含了我们希望搜索的字段和字符串。但也带来了一些问题。

首先来创建一些数据

PUT t1/doc/1
{
  "title":"中国是世界上人口最多的国家"
}

PUT t1/doc/2
{
  "title":"美国是世界上军事实力最强大的国家"
}

PUT t1/doc/3
{
  "title":"北京市中国的首都"
}

现在,当我们以中国作为搜索条件,我们希望只返回和中国相关的文档。我们首先使用match进行查询:

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

结果如下:

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 0.7048458,
    "hits" : [
      {
        "_index" : "t1",
        "_type" : "doc",
        "_id" : "3",
        "_score" : 0.7048458,
        "_source" : {
          "title" : "北京市中国的首都"
        }
      },
      {
        "_index" : "t1",
        "_type" : "doc",
        "_id" : "1",
        "_score" : 0.6406914,
        "_source" : {
          "title" : "中国是世界上人口最多的国家"
        }
      },
      {
        "_index" : "t1",
        "_type" : "doc",
        "_id" : "2",
        "_score" : 0.16943811,
        "_source" : {
          "title" : "美国是世界上军事实力最强大的国家"
        }
      }
    ]
  }
}

虽然如期返回了中国的文档,但是把美国的文档也返回了。主要是由于Elastic Search在内部对文档进行分词的时候,对于中文来说,是一个字一个字进行切分的。

可以使用短语查询:

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

返回结果:

{
  "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.70484585,
    "hits" : [
      {
        "_index" : "t1",
        "_type" : "doc",
        "_id" : "3",
        "_score" : 0.70484585,
        "_source" : {
          "title" : "北京市中国的首都"
        }
      },
      {
        "_index" : "t1",
        "_type" : "doc",
        "_id" : "1",
        "_score" : 0.59047776,
        "_source" : {
          "title" : "中国是世界上人口最多的国家"
        }
      }
    ]
  }
}
        

这里match_pharse是在文档中搜索指定的词组,而中国正是一个词组,可以返回。

那么,现在想搜索中国世界相关的文档,但是有忘记其余部分内容,就可以使用slop来解决了。

GET t1/doc/_search
{
  "query":{
    "match_phrase": {
      "title": {
        "query": "中国世界",
        "slop": 2
      }
    }
  }
}

在两个词组之间有了2个词的间隔,这个时候,就可以查询到结果了:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.9709763,
    "hits" : [
      {
        "_index" : "t1",
        "_type" : "doc",
        "_id" : "1",
        "_score" : 0.9709763,
        "_source" : {
          "title" : "中国是世界上人口最多的国家"
        }
      }
    ]
  }
}

slop间隔可以根据需要进行适当的改动。

match系列之match_pharse_prefix(最左前缀查询)

如果遇到拼写beautiful拼到bea就不知道该往下怎么拼写了。这个时候,搜索引擎就看自己的词库有啥以bea开头的词。

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

PUT t3/doc/2
{
  "title":"sun and beach",
  "desc":"I like basking on the beach"
}

在这里用matchmatch_phrase都不太合适,因为输入的是不完整的词,可以用match_phrase_prefix

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

结果如下:

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.9530774,
    "hits" : [
      {
        "_index" : "t3",
        "_type" : "doc",
        "_id" : "1",
        "_score" : 0.9530774,
        "_source" : {
          "title" : "maggie",
          "desc" : "beautiful girl you are beautiful so "
        }
      },
      {
        "_index" : "t3",
        "_type" : "doc",
        "_id" : "2",
        "_score" : 0.6931472,
        "_source" : {
          "title" : "sun and beach",
          "desc" : "I like basking on the beach"
        }
      }
    ]
  }
}

前缀查询与短语查询类似,但前缀查询可以进一步的搜索词组,只不过它是和词组中的最后一个词进行匹配,当使用这种行为进行搜索的时候,最好通过max_expansions来设置最大前缀扩展数量,因为产生的结果可能是一个很大的集合,不加限制的话,影响查询性能。

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

结果返回:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.9530774,
    "hits" : [
      {
        "_index" : "t3",
        "_type" : "doc",
        "_id" : "1",
        "_score" : 0.9530774,
        "_source" : {
          "title" : "maggie",
          "desc" : "beautiful girl you are beautiful so "
        }
      }
    ]
  }
}

如果设置2,则两篇都返回。

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.9530774,
    "hits" : [
      {
        "_index" : "t3",
        "_type" : "doc",
        "_id" : "1",
        "_score" : 0.9530774,
        "_source" : {
          "title" : "maggie",
          "desc" : "beautiful girl you are beautiful so "
        }
      },
      {
        "_index" : "t3",
        "_type" : "doc",
        "_id" : "2",
        "_score" : 0.6931472,
        "_source" : {
          "title" : "sun and beach",
          "desc" : "I like basking on the beach"
        }
      }
    ]
  }
}

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

现在我们有一个50个字段的索引,我们需要在多个字段中查询同一个关键字。

GET t3/doc/_search
{
  "query":{
    "multi_match": {
      "query": "beach"
      , "fields": ["title","desc"]

    }
  }
}

我们可以将多个字段放到field列表中即可。以达到匹配多个字段的目的。

原文地址:https://www.cnblogs.com/shine-rainbow/p/12207768.html

时间: 2024-10-07 03:16:31

Elastic Search的简单操作的相关文章

Elastic Search | Head插件的安装与简单使用

Elastic Search | Head插件的安装与简单使用 Elastic Search Head插件是一个入门级的Elastic Search前端插件:使用它可以按照Restful API来操作Elastic Search. 安装Node JS 我们使用官网直接下载编译好的二进制文件进行安装. 到官网右击红圈的内容,复制下载地址: 到cd /usr/local目录,使用wget https://nodejs.org/dist/v10.16.3/node-v10.16.3-linux-x64

Elastic Search如何快速检索

Elastic Search如何快速检索 http://www.infoq.com/cn/articles/database-timestamp-02 Elasticsearch是通过Lucene的倒排索引技术实现比关系型数据库更快的过滤.特别是它对多条件的过滤支持非常好,比如年龄在18和30之间,性别为女性这样的组合查询.倒排索引很多地方都有介绍,但是其比关系型数据库的b-tree索引快在哪里?到底为什么快呢? 笼统的来说,b-tree索引是为写入优化的索引结构.当我们不需要支持快速的更新的时

elastic search查询命令集合

Technorati 标签: elastic search,query,commands 基本查询:最简单的查询方式 query:{"term":{"title":"crime"}} terms:目标列包含指定词 match:会把输入的"短语"进行分解成词 bool match:match分解成多个词,并且是and的关系,默认是or的关系 match phrase:分词,但是要指定分词之间词语的个数 match phrase

elastic search 学习笔记

Elastic search在数据分析的应用中相当于一个数据库的搜索引擎. 跟MySQL类似,它有自己的查询语言,只不过不是关系型数据库,属于NoSQL. 可以根据索引从分布式服务器文件系统中快速存取数据.mysql的数据库和表存放在某台服务器上,使用的时候连接到服务器对数据库进行操作,数据使用unicode编码的mysql raw data格式:而elasticsearch通过http使用json数据进行数据索引.

Elastic Search 相关度计算

为了搞清楚elastic search背后是如何计算文档搜索时候的相关度,我决定自己做实验去探索 这篇博客讲得还不错 http://blog.csdn.net/dm_vincent/article/details/42099063 而博客本身也只是翻译了官方文档 https://www.elastic.co/guide/en/elasticsearch/guide/current/scoring-theory.html 我准备验证一下 在进行文档的搜索时,应用了以下几个基础算法的组合.名字听起来

[elastic search][redis] 初试 ElasticSearch / redis

现有项目组,工作需要. http://www.cnblogs.com/xing901022/p/4704319.html Elastic Search权威指南(中文版) https://es.xiaoleilu.com/010_Intro/00_README.html https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html https://github.com/elastic/elasticsearch Ela

深入分析Elastic Search的写入过程

摘要 之前写过一篇ElasticSearch初识之吐槽,不知觉竟然过去了两年了.哎,时光催人老啊.最近又用到了ES,想找找过去的总结文档,居然只有一篇,搞了半年的ES,遇到那么多的问题,产出只有这么点,真是说不过去啊.只好又重新捡起ES,发现ES槽点依然很多,不兼容的更新太多了,各个版本之间的差异不小,感觉ES就是偏理论算法的人设计出来的,而不是工程学家写的.非常像公司里面,算法工程师吐槽后端应用开发算法能力弱,后端应用开发吐槽算法工程师工程能力太差.作为一个应用开发对ES差不多就是这种感觉.不

Elastic search中使用nested类型的内嵌对象

在大数据的应用环境中,往往使用反范式设计来提高读写性能. 假设我们有个类似简书的系统,系统里有文章,用户也可以对文章进行赞赏.在关系型数据库中,如果按照数据库范式设计,需要两张表:一张文章表和一张赞赏历史记录表,赞赏历史记录表包括了赞赏者姓名和赞赏金额. 在Elastic search中,由于都是json格式存储,则可以在一个index存储系统中的文章及其赞赏记录,这种情况下需要在elastic search中使用nested类型的内嵌对象.因为如果使用数组或者object对象的话,赞赏者姓名和

moloch1.8.0简单操作手册

moloch1.8.0简单操作手册 Sessions 页面:Sessions主要通过非常简单的查询语言来构建表达式追溯数据流量,以便分析. SPIView 页面: SPIGraph页面:SPIGraph 将以流量趋势图的方式展示指定字段的流量情况.除了直接在 SPIGraph 界面中选择字段,也可以在Sessions 及 SPIView 界面进行指定要查看的字段,通过 SPIGraph 选项跳转至本界面. Connections页面:Connections 用于直观的展示源节点与目的节点的关系.