elasticsearch 7.5.0 学习笔记

温馨提示:电脑端看不到右侧目录的话请减小缩放比例。

API操作—— 新建或删除查询索引库

新建索引库

新建index,要向服务器发送一个PUT请求,下面是使用curl命令新建了一个名为test的index的例子

curl -XPUT 'localhost:9200/test'

Response:

{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "test"
}

删除索引库

删除index,要向服务器发送一个delete请求,下面是删除上面的test的index的例子

curl -XDELETE 'http://localhost:9200/customer'

Response:

{
    "acknowledged": true
}

查询索引库

查询index,要发送一个HEAD请求,例子如下

curl -IHEAD 'localhost:9200/test'

Reponse:

HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 359

API操作—— 创建映射关系

接下来的操作用curl比较不方便,复杂的内容使用PostMan进行操作,创建映射关系要使用PUT请求,如下json代码代表我定义了三个字段,分别是title,url和doc,他们的type属性即为数据类型,关于elasticsearch的数据类型这里不过多赘述,analyzer为ik_max_word代表使用了ik分词器的最粗粒度分词

PUT test/_mapping

{
    "properties": {
        "title": {
            "type": "text",
            "analyzer": "ik_max_word"
        },
        "url": {
            "type": "text"
        },
        "doc": {
            "type": "text",
            "analyzer": "ik_max_word"
        }
    }
}

Reponse:

{
    "acknowledged": true
}

API操作—— 查询映射关系

查询索引库的映射关系要使用GET请求,如下我查询了上面创建的映射关系

curl -XGET 'localhost:9200/test/_mapping'

Response:

{
  "test" : {
    "mappings" : {
      "properties" : {
        "doc" : {
          "type" : "text",
          "analyzer" : "ik_max_word"
        },
        "title" : {
          "type" : "text",
          "analyzer" : "ik_max_word"
        },
        "url" : {
          "type" : "text"
        }
      }
    }
  }
}

API操作—— 添加更新数据

添加数据

添加数据使用GET请求发送json数据,以上面的索引库为例,我添加以下数据

GET test/_doc

{
    "title": "PlumK's blog",
    "url": "plumk.site",
    "doc": "this is the content,这里是正文"
}

更新数据

更新数据需要知道数据的id,可以通过查找获得,其他的跟添加数据差不多,GET请求http://localhost:9200/test/_doc/bm87BW8BcAj1cF19_7Sfjson如下:(这里的bm87BW8BcAj1cF19_7Sf就是我上面插入数据的id)

GET test/_doc/bm87BW8BcAj1cF19_7Sf

{
    "title": "PlumK's blog",
    "url": "plumk.site",
    "doc": "this is the content,这里是修改后的正文"
}

Response:

{
    "_index": "test",
    "_type": "_doc",
    "_id": "bm87BW8BcAj1cF19_7Sf",
    "_version": 2,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 1,
    "_primary_term": 1
}

API操作—— 删除数据

删除数据同样需要知道id,以删除上面的数据为例,对下面url发送DELETE请求:

localhost:9200/test/_doc/bm87BW8BcAj1cF19_7Sf

API操作—— 查询数据

match all查询

最基本的查询,他将返回所有结果,每个对象的得分为1

POST /test/_search

{
   "query":{
      "match_all":{}
   }
}

Response:

{
    "took": 9,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "test",
                "_type": "_doc",
                "_id": "bm87BW8BcAj1cF19_7Sf",
                "_score": 1.0,
                "_source": {
                    "title": "PlumK's blog",
                    "url": "plumk.site",
                    "doc": "this is the content,这里是正文"
                }
            }
        ]
    }
}

match查询

match查询返回match中属性相同的条目,下面如果url为plumk就查询不到了,也就是说必须属性完全相同

POST test/_search

{
    "query": {
        "match": {
            "url": "plumk.site"
        }
    }
}

Reponse:

{
    "took": 11,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0.2876821,
        "hits": [
            {
                "_index": "test",
                "_type": "_doc",
                "_id": "bm87BW8BcAj1cF19_7Sf",
                "_score": 0.2876821,
                "_source": {
                    "title": "PlumK's blog",
                    "url": "plumk.site",
                    "doc": "this is the content,这里是修改后的正文"
                }
            }
        ]
    }
}

Query String查询

该查询使用查询解析器和query_string关键字。例如下:

POST test/_search

{
    "query": {
        "query_string": {
            "query": "plumk"
        }
    }
}

Response:

{
    "took": 25,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0.2876821,
        "hits": [
            {
                "_index": "test",
                "_type": "_doc",
                "_id": "bm87BW8BcAj1cF19_7Sf",
                "_score": 0.2876821,
                "_source": {
                    "title": "PlumK's blog",
                    "url": "plumk.site",
                    "doc": "this is the content,这里是修改后的正文"
                }
            }
        ]
    }
}

设置最小匹配度查询

设置minimum_should_match字段,如果满足这个匹配度就会被查询出来,不多废话,看例子

GET test/_search

{
  "query": {
    "match": {
      "title":{"query": "plumk的博客","minimum_should_match": "50%"}
    }
  }
}

多字段查询

这里尝试在url和title中搜索plumk

GET test/_search

{
  "query": {
    "multi_match": {
        "query": "plumk",
        "fields": ["url", "title"]  
    }
  }
}

接下来的查询我接下来可能使用不到,但是考虑到会有同样刚入门的小白看到这篇博客,所以这里我还是从网上摘抄以下

Term查询

这些查询主要处理数字、日期等结构化数据。如下:

POST /schools/_search

{
   "query":{
      "term":{"zip":"176115"}
   }
}

Reponse:

{
   "took":1, "timed_out":false, "_shards":{"total":10, "successful":10, "failed":0},
   "hits":{
      "total":1, "max_score":0.30685282, "hits":[{
         "_index":"schools", "_type":"school", "_id":"1", "_score":0.30685282,
         "_source":{
            "name":"Central School", "description":"CBSE Affiliation",
            "street":"Nagan", "city":"paprola", "state":"HP", "zip":"176115",
            "location":[31.8955385, 76.8380405], "fees":2200, 
            "tags":["Senior Secondary", "beautiful campus"], "rating":"3.3"
         }
      }]
   }
}

Range查询

该查询用于查找值在值范围之间的对象。为此,我们需要使用如下运算符

  • gte:大于等于
  • gt:大于
  • lte:小于等于
  • lt:小于

POST /schools*/_search

{
   "query":{
      "range":{
         "rating":{
            "gte":3.5
         }
      }
   }
}

Response:

{
   "took":31, "timed_out":false, "_shards":{"total":10, "successful":10, "failed":0},
   "hits":{
      "total":3, "max_score":1.0, "hits":[
         {
            "_index":"schools", "_type":"school", "_id":"2", "_score":1.0,
            "_source":{
               "name":"Saint Paul School", "description":"ICSE Affiliation",
               "street":"Dawarka", "city":"Delhi", "state":"Delhi", 
               "zip":"110075", "location":[28.5733056, 77.0122136], "fees":5000, 
               "tags":["Good Faculty", "Great Sports"], "rating":"4.5"
            }
         }, 
         {
            "_index":"schools_gov", "_type":"school", "_id":"2", "_score":1.0, 
            "_source":{
               "name":"Government School", "description":"State Board Affiliation",
               "street":"Hinjewadi", "city":"Pune", "state":"MH", "zip":"411057",
               "location":[18.599752, 73.6821995] "fees":500, 
               "tags":["Great Sports"], "rating":"4"
            }
         },
         {
            "_index":"schools", "_type":"school", "_id":"3", "_score":1.0,
            "_source":{
               "name":"Crescent School", "description":"State Board Affiliation",
               "street":"Tonk Road", "city":"Jaipur", "state":"RJ", "zip":"176114", 
               "location":[26.8535922, 75.7923988], "fees":2500,
               "tags":["Well equipped labs"], "rating":"4.5"
            }
         }
      ]
   }
}

复合查询

这个比较复杂,这里也不过多赘述,可以去看这个博客

此外还有Joining查询,这个涉及到嵌套映射,就不展开讲了,还有地理查询,暂时用不到,可以去看官方文档

未完待续。。。

猜你喜欢

转载自www.cnblogs.com/Rasang/p/12041729.html