ElasticSearch is similar to relational databases

ElasticSearch has a similar syntax to relational databases (Mysql, Sql Server).

Relational database ElasticSearch
Database Indices
Tables (tables) Types
Lines (rows) The document (the document)
Columns Fields

2. Basic data types

(1) String types: text (will be split by the word segmentation), keyword (will not be split by the word segmentation)

(2) Number types: long, INTEGER, short, byte, double, float, half_float, SCALed_float

(3) Date

(4) Date (nanosecond) : date_nanos

(5) Boolean: Boolean

(6) Binary type: binary

And so on…

Basic syntax of document operation

General usage:

(Request mode)/(index)/(type)/(ID)

{(request body)}

  1. Add data

    PUT /userinfo/user/1
    {
      "name":"weder"."age":"22"."desc":"Learning"."tags": ["Man"."JAVA"."666"]}Copy the code

  1. Query data
    GET /userinfo/user/1
    Copy the code

  1. Modify the data

Method one:

PUT /userinfo/user/1
{
"desc":"Game"
}
Copy the code

From the results, it can be seen that this method is modified by direct overwriting, which is not recommended

Method 2 (Common) :

POST /userinfo/user/1/_update
{
 	"doc": {"name":"weder"."desc":"Eat"}}Copy the code

  1. Simple conditional query
GET /userinfo/user/_search? q=name:wederCopy the code

  1. For queries that require additional operations, the _source field is a display-only field
GET /userinfo/user/_search
{
"query": {
 "match": {
     "desc":"Eat"}},"_source": ["name"]}Copy the code

6. Multi-condition query (similar to AND in SQL)

GET /userinfo/user/_search
{
"query": {
 "bool": {
   "must": [{"match": {
         "name":"weder"}}, {"match": {
          "desc":"Learning"}}]}}}Copy the code

7. Multi-condition query (similar to OR in SQL)

GET /userinfo/user/_search
{
"query": {
 "bool": {
   "should": [{"match": {
         "name":"weder"}}, {"match": {
          "desc":"Eat"}}]}}}Copy the code

  1. The query
GET /userinfo/user/_search
{
"query": {
  "bool": {
    "must_not": [{"match": {
          "name":"weder"}}]}}}Copy the code

  1. Filter, which can be used for range lookup

  2. Paging, form: start page, size: number of pages

GET /userinfo/user/_search
{
"query": {
  "match": {
      "desc":"Learning"}},"_source": ["name"."age"]."from": 0."size": 2
}
Copy the code

  1. There is also highlight, which concatenates HTML tags to highlight the results