preface

ElasticSearch is the ElasticSearch cluster and Kinaba. In this article, you will learn how to use ElasticSearch DSL statements.

ElasticSearch DSL is introduced

Elasticsearch provides a full query DSL (domain-specific language) based on JSON to define queries. Think of a query DSL as an AST (abstract syntax tree) for a query, which consists of two clauses:

  • Leaf query clause: The leaf query clause looks for a specific value in a specific domain, such as match, term or range queries. These queries can be used by themselves.
  • Compound query clause Compound query clauses wrap other leaf or compound queries and are used to logically combine multiple queries (such as bool or DIS_max queries) or change their behavior (such as constant_score queries). Query clauses behave differently depending on whether they are used in a query context or a filter context.

You can’t avoid using DSL statements for ElasticSearch, just like you need to learn SQL syntax for relational databases. If we learn how to use the DSL syntax, it will be very easy to use and use Java Client calls later.

ElasticSearch DSL statement

Here we will first introduce the simple use of DSL statements, from the most commonly used to add, delete, change, check!

1. New data

ElasticSearch can add data directly, as long as you specify index and type. You can specify the primary key ID when adding it, or you can not specify the primary key ID, which is generated by ElasticSearch.

Example of adding data command:

POST test1/_doc/1 { "uid" : "1234", "phone" : "12345678909", "message" : "qq", "msgcode" : "1", "sendtime" : "The 2019-03-14 01:57:04}"Copy the code

Kinaba example diagram:

Note: POST test1/_doc/1 specifies primary key ID 1. If POST test1/_doc/1 specifies primary key ID 1, then es generates es.

You can also view the status of the index using GET test1/ or GET test1/_settings and GET test1/_mapping, namely setting and mapping.

Create an index library

In the example above, we create data through directly through the library, thus creating the index but not create indexes library generated by ES itself this is not friendly, because it will use the default configuration, the field structure is the text (text data segmentation, at the time of storage will be additional footprint), copy of fragmentation and index using the default, The default values are 5 and 1. The number of ES fragments cannot be changed after creation, except for reindex(discussed below), so we will specify the data template for the creation.

Here is a brief introduction to the data structure of ES. The following data structure is the 6.x version of ES.

  • The core data types text and keyword

  • Numeric data types long, INTEGER, short, byte, double, float, halF_float, SCALed_float

  • Date Data type date

  • Boolean Data type Boolean

  • Binary data type binary

  • Range data types integer_range, FLOAT_range, long_range, doubLE_range, date_range

  • Complex data type editing

  • Object Data type Object Is used for a single JSON object

  • Nested data types are used for JSON object arrays

  • Geographic data type editing

  • Geographic location data type GEO_point latitude/longitude integration

  • The geoshape data type geo_shape is used for complex shapes such as polygons

  • Professional data type editing

  • IP Data type IP Used for IPv4 and IPv6 addresses

  • Completion Data type Completion provides suggestions for automatic completion

  • Token count data type token_count Computes the number of tokens in a string mapper-murmur3 murmur3 computes the hash of the value and stores it in the index mapper-annotated-text annotated-text Index text containing a special tag (usually used to identify named entities)

  • Leachate types accept queries from Query-DSL

  • The JOIN data type defines parent/child relationships for documents within the same index

  • Alias data types define aliases for existing fields.

  • Multi-field editing is often useful to index the same field in different ways for different purposes. For example, a string field can be mapped to text for full-text search or keyword for sorting or aggregation. Alternatively, you can index text fields using the Standard analyzer, English analyzer, and French analyzer. This is the purpose of multiple domains. Most data types support multiple fields with the fields parameter.

Text, keyword, byte, short, INTEGER, long, float, double, Boolean, date Text and keyword are both string types. It is easy to distinguish between them. Text is used for word segmentation, and keyword can be used for sorting or aggregating.

ES data structure is here, let’s create index library!

Example of the command for adding an index library:

PUT test1
{
    "settings" : {
        "number_of_shards" : 10,
        "number_of_replicas" : 1,
         "refresh_interval" : "1s"
    },
    "mappings" : {
        "_doc" : {
            "properties" : {
                "uid" : { "type" : "long" },
                "phone" : { "type" : "long" },
                "message" : { "type" : "keyword" },
                "msgcode" : { "type" : "long" },
                 "sendtime" : {  
                  "type" : "date",
                  "format" : "yyyy-MM-dd HH:mm:ss" 
  }
                
            }
        }
    }
}
Copy the code

Figure:

Note:

  • Number_of_shards: specifies the number of shards to be set, which cannot be changed.
  • Refresh_interval: specifies the refresh time of ES cache. If the write is frequent but the query does not require real-time performance, the value can be set to a higher value to improve performance. You can change the
  • Number_of_replicas: yes Specifies the number of replicas of the index database. You are advised to set the value to more than 1.

There are also several important parameters here, by the way:

  • Store: true/false Indicates whether the field is stored. The default value is stored.
  • Doc_values: true/false indicates whether the field participates in aggregation and sorting.
  • Index: true/false Indicates whether the field is indexed. The default value is.

For the values of these fields, see the following example diagram:

3. Modify data

In fact, the addition and modification of ES can be seen as the same, there is modification, there is no new, but here is a brief introduction. There are two main ways to modify the data. One is to modify the data through the primary key ID. This is relatively simple, which is the same as new data. The other is modified through conditions, which are equivalent to where conditions in SQL update statements.

Example of the command modified by primary key:

POST test1/_doc/1 { "uid" : "1234", "phone" : "12345678909", "message" : "qq", "msgcode" : "1", "sendtime" : "The 2019-03-14 01:57:04}"Copy the code

Examples of commands modified based on conditions:

POST test1/_update_by_query
{
  "query": {
    "term": {
      "phone": "12345678909"
    }
  } ,
  "script": {
    "source": "ctx._source['message'] = 'xuwujing'"
  }
}
Copy the code

Original data:

Modified data:

Note: in addition to using DSl statements, ES can also operate with some officially defined scripting languages and SQL statements. The operation of scripting languages and SQL statements will be discussed in the future.

Delete data, fields, and index libraries

Mysql > DELETE index library /id; mysql > DELETE index library /id;

Example command for deleting data by primary key:

DELETE test1/1
Copy the code

Examples of commands to delete data based on conditions:

POST test/_delete_by_query
{
  "query": {
      "term": {
        "phone": "12345678909"
      }
  }
}
Copy the code

Of course, ES can also delete the data of a certain field according to the condition, such as deleting the data of msgcode field.

Example command for deleting field data:

POST test/_doc/_update_by_query
{
"script":{
"lang":"painless",
"inline":"ctx._source.remove(\"msgcode\")"
}
}
Copy the code

Figure:

The query

Query all

Match_all can query information about all index libraries in a cluster, including some hidden libraries. Command example:

GET _search
{   
  "query": {
    "match_all": {}
  }
}
Copy the code

Figure:

To query all data of an index database, run the GET index database name/index database type /_search command.

Command example:

GET  test1/_doc/_search
Copy the code

It is also easier to query data by ID, simply replace the _search with the primary key ID. Command example:

GET  test1/_doc/2
Copy the code

Term query

Term is primarily used to match exactly what values, such as numbers, dates, Boolean values, or not_analyzed strings (unanalyzed text data types)

For example, search by phone number. Command example:

GET  test1/_doc/_search
{
  "query": {
    "term": {
      "phone": "12345678909"
    }
  }
}
Copy the code

Of course, if you want to match multiple values in a single field, you can use terms, which is the equivalent of SQL’s in syntax.

Command example:

GET  test1/_doc/_search
{
  "query": {
    "terms": {
       "uid": [ 
        1234, 
        12345, 
        123456
      ] 
    }
  }
}
Copy the code

Figure:

Note: the above data is not 123456, so it is just for a simple test.

Range query

Range can be interpreted as the symbol >< in SQL, where GT is greater than, LT is less than, GTE is greater than or equal to, and LTE is less than or equal to.

Command example:

GET  test1/_doc/_search
{
  "query": {
   "range": { 
      "uid": { 
        "gt": 1234,
        "lte": 12345
      } 
    } 
  }
}
Copy the code

The query exists

Exists can be understood as the EXISTS function in SQL, which determines whether the field exists.

Here we add a field without MSgcode and use exists to query this field.

POST test1/_doc/3
{
    "uid" : "123456",
    "phone" : "12345678909",
    "message" : "qq",
    "sendtime" : "2019-03-14 01:57:04"
}
Copy the code

An example query command exists:

GET  test1/_doc/_search
{
  "query": {
   "exists": { 
       "field":"msgcode" 
    } 
  }
}
Copy the code

Figure:

Combination (bool) Query

Bool can be used to merge Boolean logic for multiple filter query results. It contains the following operators:

  • Must: A complete match of multiple query conditions, equivalent to and.
  • Must_not :: Reverse match of multiple query conditions, equivalent to NOT.
  • Should: at least one query condition matches, which is equivalent to or.

Examples of query commands are as follows:

GET /test1/_search
{
  "query": {
    "bool": {
      "must": {
        "term": {
          "phone": "12345678909"
        }
      },
      "must_not": {
        "term": {
          "uid": 12345
        }
      },
      "should": [
        {
          "term": {
            "uid": 1234
          }
        },
        {
          "term": {
            "uid": 123456
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  }
}
Copy the code

Figure:

Wildcard query

A wildcard query is the equivalent of the like syntax in an SQL statement, except that the data it queries is marked with an *.

Fuzzy query command example:

GET /test1/_search
{
  "query": {
   "wildcard": { 
       "message":"*wu*" 
    } 
  }
}
Copy the code

Re (regexp) queries

Regexp supports regular queries, such as verification codes in SMS content.

The following example queries data for content that begins with xu followed by the numbers 0-9.

Example of a regular query command:

GET /test1/_search
{
  "query": {
   "regexp": { 
       "message":"xu[0-9]" 
    } 
  }
}
Copy the code

Figure:

That’s about the end of the query example, and here are some tips for kinaba queries, as shown in the figure below:

other

Reference: www.elastic.co/guide/en/el…

I’ve been using ElasticSearch for over a year now, and I’ve accumulated some info along the way, but I’ve been so busy this year that I haven’t had time to blog about it. Ashamed to say, the number of this year to write blog some less, the second half of the year from a week more bloggers into a month more bloggers, but behind if enough time will be more some, again busy every month at least will write a ヾ(◍°∇°◍) Blue

ElasticSearch: Kinaba for ElasticSearch

Music to recommend

Original is not easy, if you feel good, I hope to give a recommendation! Your support is the biggest motivation for my writing! Copyright: www.cnblogs.com/xuwujing CSDN blog.csdn.net/qazwsxpcm Personal blog: www.panchengming.com