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

Summarize the basic operations of ElasticSearch! Very detailed!

藏色散人
Release: 2023-04-11 09:04:02
forward
6384 people have browsed it

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
    Copy after login
  • Query index
      GET请求
      http://127.0.0.1:9200/shopping
    Copy after login
  • Query all indexes
      GET请求
      http://127.0.0.1:9200/_cat/indices?v
    Copy after login
  • Delete index
      DELETE请求
      http://127.0.0.1:9200/shopping
    Copy after login

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":"商品"}
    Copy after login
      PUT请求,主键必须幂等性
      http://127.0.0.1:9200/shopping/_doc/1001 #写法一
      http://127.0.0.1:9200/shopping/_create/1002 # 写法二  {"name":"商品"}
    Copy after login
      POST请求 ,创建自定义id
      http://127.0.0.1:9200/shopping/_doc/1001
    Copy after login
  • Primary key query

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

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

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

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

      DELETE请求
      http://127.0.0.1:9200/shopping/_doc/1001
    Copy after login

    Query

  • Conditional query

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

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

      GET请求
      http://127.0.0.1:9200/shopping/_search  {
          "query":{
              "match_all":{
              }
          },
          "from":0,#起始位置/偏移量 ,公式:(页码-1)* 每页数据条数      "size":10,#每页查询10条  }
    Copy after login
  • 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"]
      }
    Copy after login

    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"
              }
          }
      }
    Copy after login

    Multi-condition query

  • and query (must)

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

      GET请求
      http://127.0.0.1:9200/shopping/_search  {
          "query":{
              "bool":{
                  "should":[ 
                      {
                          "match":{
                              "category":"小米"
                          }
                      },
                      {
                          "match":{
                              "price":1999.00
                          }
                      }
                  ]
              }
          }
      }
    Copy after login
  • 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
                          }
                      }
                  }
              }
          }
      }
    Copy after login
  • Full text search matching (word segmentation) (match)

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

      GET请求
      http://127.0.0.1:9200/shopping/_search  {
          "query":{
              "match_phrase":{
                  "category": "小华"
              }
          }
      }
    Copy after login
  • 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":{}
              }
          }
      }
    Copy after login

    Aggregation query

  • Return statistics and raw data

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

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

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

    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
              }
          }
      }
    Copy after login
  • Query mapping

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

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

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

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!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template