ElasticSearch official website:

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

  • Elastic – > start
    • The installation
    • Start the
  • Elastic – >
    • The basic concept
    • The data type
  • Elastic -> Create databases and tables
    • Index mapping is established
    • The Index of the delete
  • Elastic -> Data query
    • A conditional query for a single record
    • Batch conditional query

The installation

Elastic requires a Java 8 environment. Please refer to this article to install Java and solve your environment configuration problems!

After installing Java 8, enter the following command on your terminal to download Elastic.

$ wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.5.1.zip
$ unzip elasticsearch-5.5.1.zip
$ cdElasticsearch 5.5.1 /Copy the code

Start the

Once everything is set up (Java is installed), go to the root of Elastic and type the following command to start Elastic.

$ ./bin/elasticsearch
Copy the code

After the startup is complete, open the browser and enter localhost:9200 in the address box. If information similar to the following is displayed, the startup is successful:

{
  "name" : "atntrTf"."cluster_name" : "elasticsearch"."cluster_uuid" : "tf9250XhQ6ee4h7YI11anA"."version" : {
    "number" : "5.5.1"."build_hash" : "19c13d0"."build_date" : "The 2017-07-18 T20:44:24. 823 z"."build_snapshot" : false."lucene_version" : "6.6.0"
  },
  "tagline" : "You Know, for Search"
}
Copy the code

Ps. PostMan is recommended as an API request tool. It can replace browsers and terminals to initiate API requests and convert returned data into JSON format, which is intuitive and concise.

The basic concept

Node and Cluster

Elastic is essentially a distributed database that allows multiple servers to work together and each server can run multiple Elastic instances at the same time. Each Elastic instance is called a node, and a group of nodes is called a cluster.


The Index (Index)

An Index is the Elastic equivalent of a dataBase in a relational dataBase, or of course a table. The name of each Index must be lowercase. The following command can view all indexes under the current Elastic instance, including the document size of each Index.

$ curl -X GET 'http://localhost:9200/_cat/indices? v'
Copy the code

Of course, you can also enter the routing address from the above command in the PostMan mentioned above.


Document (Document)

Each data record in Index is called a Document. Multiple documents form an Index. So Index can be used as a table if it’s smaller. The data structure in Document is represented in JSON format.

{
  "auth": "Assassin"."work": "NodeJS full stack"."email": "I'm sorry I told you @qq.com.",}Copy the code

Type (classification)

If Index is considered a database, Type can be considered a table in that database. Note that types under the same Index should have a similar schema, which is different from a relational database. The following command lists all types under the current Index.

$ curl 'localhost:9200/_mapping? pretty=true'
Copy the code

One more thing, ES versus the various elements of a relational database:

Relational DB Database Table Row Colum
ElasticSearch Index Type Document Field

Data type in Elastic

Only the commonly used data types are listed here, and the rest will come later

  1. String: it is a String of characters. By default, it is split into words. These include the keyword and text subtypes.

    Keyword: When storing data, it is not indexed by keyword and can be used for search filtering, sorting, and aggregation. For example, ‘Wall steel structure’, the keyword considers it as a whole.

    Text: When storing data, the data will be indexed by word segmentation. However, if the data of the field is not needed for word segmentation, using text will waste storage space. The text data type cannot be used for sorting and aggregation. For example: ‘wall steel structure’, text will split it into ‘wall’, ‘steel structure’.

    Tokenizer: for text only. The git address is github.com/medcl/elast… . Its function is to slice the text according to certain rules. Install: Unzip the downloaded word segmentation plug-in to the plugins in the root directory, and restart the Elastic instance.

  2. Number type:

    • Long: long integer, 64-bit storage
    • Integer: indicates a 32-bit integer
    • Short: short integer, 16-bit storage
    • Byte: byte type, 8-bit storage
    • Double: 64-bit storage
    • Float: single-precision, 32-bit storage
  3. Other simple types

    • Date: indicates the time type
    • Boolean: Boolean type
    • Binary: indicates the byte type
  4. The compound type

    • Array: indicates the array type
    • Object: Indicates the json object type
    • Array[Object] : Nested type of an Array of objects

Index mapping is established

The creation of Index includes the definition of Type for each table. Create it in JavaScript and terminal as follows:

  • JavaScript
import elasticsearch from 'elasticsearch';
const esClient = new elasticsearch.Client({
  host: 'localhost:9200'.log: 'error'}); const settings = { number_of_shards: 5, number_of_replicas: 1, }; const PromiseList = [ esClient.indices.create({ index:'student', body: {Settings, // Index some Settings: {//type
        person: {
          // typeProperties: {sno: {type: 'long',
            },
            name: {
              type: 'keyword',
            },
            sex: {
              type: 'keyword',
            },
            class: {
              type: 'text',
              analyzer: 'ik_max_word',
              search_analyzer: 'ik_max_word',
              fields: {
                raw: {
                  type: 'keyword',
                  ignore_above: 128,
                },
              },
            },
            register: {
              type: 'date',},},},..... },}}),... ] ; PromiseList.map(p => p.catch(e => console.log(e.message))); .Copy the code

Promises are used to create databases in batches. If you want to create a single library, you can use async await.

  • terminal
$ curl -X PUT -H 'Content-Type: application/json' 'localhost:9200/student' -d '{// type person: {// type person: {sno: {type:'long', }, name: { type: 'keyword', }, sex: { type: 'keyword', }, class: { type: 'text', analyzer: 'ik_max_word', search_analyzer: 'ik_max_word', fields: { raw: { type: 'keyword', ignore_above: 128, }, }, }, register: { type: 'date',},},},},},}'
Copy the code

In the above code, we create an Index named Student, which contains a table of person containing four fields: son, name, sex, register.

The Index of the delete

Now that we’re done creating, we’re done deleting. Deleting is simple:

  • JavaScript
import elasticsearch from 'elasticsearch';
const esClient = new elasticsearch.Client({
  host: 'localhost:9200'.log: 'error'}); Promise.all( ['student'. ] .map(index => esClient.indices .delete({ index }) .catch(() => console.error(`delete fail:${index}`))));Copy the code

Promises to delete databases in batches, delete single libraries with async await.

  • terminal
$ curl -X DELETE 'localhost:9200/student'
Copy the code

Elastic data query

Common search methods in Elastic

Search, get, count, mget, msearch, etc

The configuration file

import elasticSearch from 'elasticsearch';


let _client = null;

export default () => {
  if(! _client) { _client = new elasticSearch.Client('localhost:9200');
  }
  return _client;
};
Copy the code

A conditional query for a single record

The query structure of a conditional query for a single record is a JSON-like object

{
  index: database_name,
  type: table_name,
  body: {
    query: {
      bool: {
        filter: {
          term: {
            'field_name': value,
          },
        },
      },
    },
  },
}
Copy the code

This is the simplest query structure

The filter attribute value for the Object of | Array, the Array is an Array of objects. [{term: {key: value}},…] .

Of course, queries can’t be that simple, and there are other attributes that can be added.

Term & terms filter query (match query)

Term is mainly used to match exactly what values, such as numbers, dates, and Booleans, are matched. A term query structure:

{ term: { key: value } }
Copy the code

Terms, a similar term, is used to filter queries that match multiple criteria. Such as

{ terms: 'tag': [ 'react'.'vue'.'angular']}Copy the code

Range Filter query (range query)

Range filtering allows us to find a batch of data in a specified range

{
  range: {
    age: {
      gte: 18,
      lt: 28,
    }
  }
}
Copy the code

Range of keywords:

  • Gt: greater than

  • Gte: greater than or equal to

  • Lt: less than

  • Lte: less than or equal to

Reflected in the query structure:

{
  index: database_name,
  type: table_name,
  body: {
    query: {
      range: {
        age: {
          gte: 18,
          lt: 28,
        }
      }
  },
}
Copy the code

Range and term(s) are sibling properties, so they can be used as values for the Filter array property

Exists & Missing Filtering

Exists & missing Is used to find the presence or absence of a specified field in a record.

{
  exists: {
    field: 'field_value'}}Copy the code

Exists & Missing and term(s) are sibling properties and therefore can be used as values for filter array properties

Bool filter

Bool Boolean logical query containing keywords:

  • Must, must, must

  • Must_not: equivalent to not

  • Should: equivalent to or

The usage is simple, just replace filter, of course, with filter.

Agg aggregate query

For this part, there are some differences of understanding in the language description. After all, this piece of aggregation is still relatively huge, so in order to be more professional or recommend Baidu bar (and so I grow up to do a summary again!!)

ES of aggs

Batch conditional query

Batch query is a query process that processes a set of different query conditions in turn, just like a batch process.

Msearch Batch conditional query

Msearch simply puts the general conditional query structure into an array and uses that array as the query structure.

{
  body: [
    { index: database_1, type: table_1 },
    {
      query: {
        term: { field: value },
      },
    },
    { index: database_2, type: table_2 },
    {
      query: {
        bool: {
          must: {
            term: {
              field: value,
            },
          },
          filter: [
            { term: { field: value } },
            { term: { field: value } },
          ],
        },
      },
    },
}
Copy the code

As can be seen from the above structure, the query structure of msearch is an array structure with two adjacent elements starting from subscript 0 as a single query structure.

Mget Batch ID query

This approach is suitable for batch queries where multiple record ids are known

{
  index: database,
  type: table,
  body: {
    ids,
  },
}
Copy the code

Ids is an array.


Wen/Assassin is me

Welcome to pay attention to the author

Sound/fluorspar

The copyright of this article belongs to the author.

To subscribe for more sharing from the front line of KnownsecFED development, please search our wechat official account KnownsecFED. Welcome to leave a comment to discuss, we will reply as far as possible.

Thank you for reading.