Home >Backend Development >PHP Tutorial >Summarize the basic operations of ElasticSearch! Very detailed!

Summarize the basic operations of ElasticSearch! Very detailed!

藏色散人
藏色散人forward
2022-12-07 16:37:226436browse

es download address
IK word segmenter download address

Index

  • Create index
    Compared with relational databases, creating an index is equivalent to creating a database
      PUT请求
      http://127.0.0.1:9200/shopping
  • Query index
      GET请求
      http://127.0.0.1:9200/shopping
  • Query all indexes
      GET请求
      http://127.0.0.1:9200/_cat/indices?v
  • Delete index
      DELETE请求
      http://127.0.0.1:9200/shopping

Document

The index has been created. Next, we create the document and add data. The document here can be compared to table data in a relational database, and the added data format is JSON format

  • Create document

      POST请求
      http://127.0.0.1:9200/shopping/_doc #写法一
      http://127.0.0.1:9200/shopping/_create # 写法二  {"name":"商品"}
      PUT请求,主键必须幂等性
      http://127.0.0.1:9200/shopping/_doc/1001 #写法一
      http://127.0.0.1:9200/shopping/_create/1002 # 写法二  {"name":"商品"}
      POST请求 ,创建自定义id
      http://127.0.0.1:9200/shopping/_doc/1001
  • Primary key query

      GET请求
      http://127.0.0.1:9200/shopping/_doc/1001
  • Full query

      GET请求
      http://127.0.0.1:9200/shopping/_search
  • Full modification

      PUT请求
      http://127.0.0.1:9200/shopping/_doc/1001
      {"name":"商品"}
  • Partial modification

      POST请求
      http://127.0.0.1:9200/shopping/_update/1001
      {"doc":{"name":"局部修改商品"}}
  • Delete

      DELETE请求
      http://127.0.0.1:9200/shopping/_doc/1001

    Query

  • Conditional query

      GET请求,方法一
      http://127.0.0.1:9200/shopping/_search?q=category:小米
      http://127.0.0.1:9200/shopping/_search?q=name:商品
      GET请求,方法二(推荐)
      http://127.0.0.1:9200/shopping/_search  {
          "query":{
              "match":{
                  "category":"小米"
              }
          }
      }
  • Full query

      GET请求
      http://127.0.0.1:9200/shopping/_search  {
          "query":{
              "match_all":{
              }
          }
      }
  • Paging query (from, size)

      GET请求
      http://127.0.0.1:9200/shopping/_search  {
          "query":{
              "match_all":{
              }
          },
          "from":0,#起始位置/偏移量 ,公式:(页码-1)* 每页数据条数      "size":10,#每页查询10条  }
  • Specify field paging query (_source)

      GET请求
      http://127.0.0.1:9200/shopping/_search  {
          "query":{
              "match_all":{
              }
          },
          "from":0,#起始位置/偏移量 ,公式:(页码-1)* 每页数据条数      "size":10,#每页查询10条      "_source":["title"]
      }

    Query sorting (sort)

      GET请求
      http://127.0.0.1:9200/shopping/_search  {
          "query":{
              "match_all":{
              }
          },
          "from":0,#起始位置/偏移量 ,公式:(页码-1)* 每页数据条数      "size":10,#每页查询10条      "_source":["title"],
          "sort":{
              "price":{
                  "order":"desc"
              }
          }
      }

    Multi-condition query

  • and query (must)

      GET请求
      http://127.0.0.1:9200/shopping/_search  {
          "query":{
              "bool":{
                  "must":[ 
                      {
                          "match":{
                              "category":"小米"
                          }
                      },
                      {
                          "match":{
                              "price":1999.00
                          }
                      }
                  ]
              }
          }
      }
  • or query (should)

      GET请求
      http://127.0.0.1:9200/shopping/_search  {
          "query":{
              "bool":{
                  "should":[ 
                      {
                          "match":{
                              "category":"小米"
                          }
                      },
                      {
                          "match":{
                              "price":1999.00
                          }
                      }
                  ]
              }
          }
      }
  • Range query (filter, range)

      GET请求
      http://127.0.0.1:9200/shopping/_search  {
          "query":{
              "bool":{
                  "should":[
                      {
                          "match":{
                              "category":"小米"
                          }
                      },
                      {
                          "match":{
                              "price":1999.00
                          }
                      }
                  ],
                  "filter":{
                      "range":{
                          "price":{
                              "gt":5000
                          }
                      }
                  }
              }
          }
      }
  • Full text search matching (word segmentation) (match)

      GET请求
      http://127.0.0.1:9200/shopping/_search  {
          "query":{
              "match":{
                  "category": "小华"
              }
          }
      }
  • Exact match (match_phrase)

      GET请求
      http://127.0.0.1:9200/shopping/_search  {
          "query":{
              "match_phrase":{
                  "category": "小华"
              }
          }
      }
  • Highlight query (hightlight, add html tags to the results)

      GET请求
      http://127.0.0.1:9200/shopping/_search  {
          "query":{
              "match_phrase":{
                  "category": "小华"
              }
          },
          "hightlight":{
              "fields":{
                  "category":{}
              }
          }
      }

    Aggregation query

  • Return statistics and raw data

      GET请求
      http://127.0.0.1:9200/shopping/_search  { 
          "aggs":{ #聚合操作          "price_group":{ #名称,随意起名              "terms":{ #分组                  "field":"price" #分组字段              }
              }
          },  }
  • Close raw data (size)

      GET请求
      http://127.0.0.1:9200/shopping/_search      { 
          "aggs":{ #聚合操作          "price_group":{ #名称,随意起名              "terms":{ #分组                  "field":"price" #分组字段              }
              }
          },      "size":0
      }
  • Average

      GET请求
      http://127.0.0.1:9200/shopping/_search      { 
          "aggs":{ #聚合操作          "price_avg":{ #名称,随意起名              "age":{ #平均值                  "field":"price" #分组字段              }
              }
          },      "size":0
      }

    Mapping relationship

  • Create mapping

      PUT请求
      http://127.0.0.1:9200/user/_mapping  { 
          "properties":{
              "name":{
                  "type":"text", #全文检索分词查询              "index":true
              },
              "sex":{
                  "type":"keyword",#完全查询              "index":true
              },
              "tel":{
                  "type":"keyword",#不能查询              "index":false
              }
          }
      }
  • Query mapping

      GET请求
      http://127.0.0.1:9200/user/_mapping
  • Add data

      PUT请求
      http://127.0.0.1:9200/user/_create/1001
      {
          name:"小米",
          sex:"男的",
          tel:"10010"
      }
  • Query data

      GET请求
      http://127.0.0.1:9200/user/_search  {
          "query":{
              "match": {
                  name:"小"
              }
          }
      }
      GET请求
      http://127.0.0.1:9200/user/_search  {
          "query":{
              "match": {
                  sex:"男" #查询不到,必须输入男的          }
          }
      }
      #不支持查询  GET请求
      http://127.0.0.1:9200/user/_search  {
          "query":{
              "match": {
                  tel:"10010" 
              }
          }
      }

The above is the detailed content of Summarize the basic operations of ElasticSearch! Very detailed!. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete