About the basic operation of the document

About the basic operation of the document

Basic operation

adding data

PUT /latte2/user/1
{
    
    
  "name":"latte",
  "age": 23,
  "desc": "干",
  "tags":["技术","海贼王"]
}
PUT /latte2/user/2
{
    
    
  "name": "张三",
  "age": 28,
  "desc": "法外狂徒",
  "tags": ["旅游", "渣男", "交友"]
}

PUT /latte2/user/3
{
    
    
  "name": "李四",
  "age": 30,
  "desc": "不知道怎么描述",
  "tags": ["旅游", "男", "唱歌"]
}

GET latte2/user/1


GET latte2/user/_search?q=name:latte

Insert picture description here

Get data GET

Insert picture description here
Insert picture description here

Update data PUT

Insert picture description here
If the fields of put are incomplete, the original fields will be discarded, and put is an overwrite operation.

Post _update, this update method is recommended!

Use POST without adding _update

Insert picture description here

Other fields that have not been submitted will be left blank, and then a new doc.name field will be added

Insert picture description here

Use POST followed by _update

Insert picture description here

The submitted field value has been updated, and the unsubmitted field still has the original value

Insert picture description here

Simply search

GET latte2/user/1

The conditional query of short answer can generate a basic query according to the default mapping rules!

Insert picture description here
Weights

Complex operation search

select (sort, paging, highlight, fuzzy query, precise query!)

# 模糊查询
GET latte2/user/_search
{
  "query": {
    "match": {
      "name": "latte2"
    }
  }
}

# 对查询结果进行字段过滤
GET latte2/user/_search
{
  "query": {
    "match": {
      "name": "latte2"
    }
  },
  "_source": ["name", "desc"]
}

# 排序
GET latte2/user/_search
{
  "query": {
    "match": {
      "name": "latte2"
    }
  },
  "sort":[{
    "age": "asc"
  }]
}

第二种写法
GET latte2/user/_search
{
  "query": {
    "match": {
      "name": "latte2"
    }
  },
  "sort": [
    {
      "age": {
        "order": "asc"
      }
    }
  ]
}

# 分页
GET latte2/user/_search
{
  "query": {
    "match": {
      "name": "latte2"
    }
  },
  "sort":[{
    "age": "asc"
  }], 
  "from": 0,
  "size": 2
}

Insert picture description here
json structure
hit: index and document information. The total number of queries, and then the specific documents that are queried. Everything in the data can be traversed. Score: We can judge who is more in line with the result.

Output results, don't want so much!
Insert picture description here

After we use Java to operate es, all methods and objects are the keys inside!

Sort!

Insert picture description here

Paging query!

Insert picture description here

The data subscript still starts from 0, which is the same as all the data structures learned!

/search/{current}/{pagesize}

Boolean query

# 多条件查询 must 相当于and
GET latte2/user/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "name": "latte2"
        }},
        {"match": {
          "age": 23
        }}
      ]
    }
  }
}

# 多条件查询 should 相当于or
GET latte2/user/_search
{
  "query": {
    "bool": {
      "should": [
        {"match": {
          "name": "latte2"
        }},
        {"match": {
          "age": 25
        }}
      ]
    }
  }
}

# 多条件查询 must_not 相当于 not
GET latte2/user/_search
{
  "query": {
    "bool": {
      "must_not": [
        {"match": {
          "age": 25
        }}
      ]
    }
  }
}


# 过滤查询1 age > 27
GET latte2/user/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "name": "latte2"
        }}
      ],
      "filter": [
        {"range": {
          "age": {
            "gt": 27
          }
        }}
      ]
    }
  }
}

# 过滤器2  22<age<30 
GET latte2/user/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "name": "latte2"
        }}
      ],
      "filter": [
        {"range": {
          "age": {
            "lt": 30,
            "gt": 22
          }
        }}
      ]
    }
  }
}
GET latte2/user/_search
{
  "query": {
    "match": {
      "tags": "技术 男"
    }
  }
}

must (and) , all conditions must meet where id = 1 and name = xxx

Insert picture description here

should (or) , all conditions must meet where id = 1 or name = xxx

Insert picture description here

must_not (not)

Insert picture description here

Filter

Insert picture description here

  • gt is greater than
  • gte is greater than or equal to
  • lt is less than
  • lte is less than or equal to!

Insert picture description here

Insert picture description here

Match multiple conditions!

Insert picture description here

Accurate query!

# 定义类型term: 精确匹配
PUT latte3
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "desc": {
        "type": "keyword"
      }
    }
  }
}

PUT /latte3/_doc/1
{
  "name":"latte3",
  "desc":"latte3 desc"
}

PUT /latte3/_doc/2
{
  "name":"latte3",
  "desc":"desc 2"
}

# 按照keyword类型精准匹配
GET latte3/_search
{
  "query": {
    "term": {
      "desc": {
        "value": "desc 2"
      }
    }
  }
}
# 结果:
{
  "took" : 174,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.6931471,
    "hits" : [
      {
        "_index" : "latte3",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.6931471,
        "_source" : {
          "name" : "latte3",
          "desc" : "desc 2"
        }
      }
    ]
  }
}


# 按照text类型匹配
GET latte3/_search
{
  "query": {
    "term": {
      "name":"latte3"
    }
  }
# 结果:
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.18232156,
    "hits" : [
      {
        "_index" : "latte3",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.18232156,
        "_source" : {
          "name" : "latte3",
          "desc" : "latte3 desc"
        }
      },
      {
        "_index" : "latte3",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.18232156,
        "_source" : {
          "name" : "latte3",
          "desc" : "desc 2"
        }
      }
    ]
  }
}
多个值匹配精确查询
PUT /latte3/_doc/3
{
  "t1":"22",
  "t2":"2021-03-13"
}

PUT /latte3/_doc/4
{
  "t1": "33",
  "t2": "2021-03-13"
}

GET /latte3/_search
{
  "query": {
   "bool": {
     "should": [
       {
         "term": {
           "t1": "22"
         }
       },{
         "term": {
           "t1": "33"
         }
       }
     ]
   }
  }
}
高亮
GET latte2/user/_search
{
  "query": {
    "match": {
      "name": "latte2"
    }
  },
  "highlight": {
    "pre_tags": "<p class='key' style='color:red'>",
    "post_tags": "</p>", 
    "fields": {
      "name": {}
    }
  }
}

# 结果显示:
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 148,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.8405091,
    "hits" : [
      {
        "_index" : "latte2",
        "_type" : "user",
        "_id" : "1",
        "_score" : 0.8405091,
        "_source" : {
          "doc" : {
            "name" : "latte2"
          },
          "name" : "latte2"
        },
        "highlight" : {
          "name" : [
            "<p class='key' style='color:red'>latte2</p>"
          ]
        }
      },
      {
        "_index" : "latte2",
        "_type" : "user",
        "_id" : "2",
        "_score" : 0.8405091,
        "_source" : {
          "name" : "latte2",
          "age" : 28,
          "desc" : "法外狂徒",
          "tags" : [
            "旅游",
            "渣男",
            "交友"
          ]
        },
        "highlight" : {
          "name" : [
            "<p class='key' style='color:red'>latte2</p>"
          ]
        }
      }
    ]
  }
}

The term query is directly searched for precisely through the term process specified by the inverted index!

About participle:

  • term, directly query the exact
  • match, will use the tokenizer to parse! (Analyze the document first, and then query through the analyzed document!)

Two types text keyword(text type will be segmented, keyword type will not be segmented)

Insert picture description here

Insert picture description here

Multiple values ​​match exact query

Insert picture description here

Highlight query!

Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_43803285/article/details/114803961