Installation Elasticsearch search Docker middleware environment and the use of basic operations (CRUD, search conditions, full text search, the search phrase, the search highlighted)

I. Introduction

Search function is a very common application functionality and open source Elasticsearch choice for full-text search engine

Elasticsearch is a bottom distributed search service based on Lucene Lucene open source software toolkit is not available directly
but Elasticsearch equivalent thereof is encapsulated and provided Restful style API multi Shard (fragments) also provides a way to ensure data security automatic resharding function
github and other large sites also used Elasticsearch as its search engine service

Elasticsearch can quickly search and analyze massive amounts of data storage

Second, the installation

Installation Docker way to
first download the image Elasticsearch

# 从阿里云的仓库拉取镜像
docker pull registry.cn-hangzhou.aliyuncs.com/elasticsearch/elasticsearch:6.7.0

Then the container is to start
Elasticsearch default 9200port for communication between the respective nodes Elasticsearch the web communication the case of a distributed 9300ports

# 启动容器
docker run -d -p 9200:9200 -p 9300:9300 --name ES01 02982be5777d

Then access the browser ip: 9200
If there is data follows a successful start:
Here Insert Picture Description
If the case can not access port 9200 appears
see another blog I: perfect solution for virtual machine installation Elasticsearch error: [1]: max virtual memory areas vm .max_map_count [65530] is too low, increase

Third, the use

Elasticsearch is [ document] for the
meaning that it is stored in
the entire object or document

Elasticsearch not only store documents and indexes the content of each document so that it can be retrieved
be in Elasticsearch of the document is indexed retrieval sorting and filtering - - to the ranks of the data rather than just
the way it is a completely different way of thinking is also Elasticsearch data to support the cause of complex full-text search

Elasticsearch using JSON (JavaScript Object Notation) serialization format of the document as
JSON serialized as most programming languages supported and has become the standard format in the field of NoSQL it simple and easy to read simple advantage of lightweight and cross-platform

basic concept:

Stored data to conduct Elasticsearch called [ index ] but before indexing a document you need to determine where the document is stored
(index here is the verb
index a document is stored in a document to an index in order to be retrieved and the query
is similar to SQL statements the INSERT keyword)

Clusters may comprise a plurality of Elasticsearch [index] corresponding to each index may comprise a plurality of [Type]

These different types of storage with a plurality of [Document] each have a plurality of document [Properties]

(Indexed here is a noun like a traditional relational database in the database is a place to store relational document)
Here Insert Picture Description
If using MySQL for comparison is:

  • Elasticsearch of the index corresponds to the MySQL database
  • Elasticsearch the type equivalent of MySQL table
  • Elasticsearch the document equivalent to MySQL's record
  • Elasticsearch of property equivalent to MySQL's field

1, CRUD

①, add

To achieve Add actually very simple
just need to send a PUTrequest to the specified index to specify the type of deposit in a document

grammar:

PUT请求 ----> ip:端口/索引名称/类型名称/编号

content:

{
	要添加的JSON数据
}

Example:

PUT请求 ----> ip:端口/zjitc/student/1
{
    "id" : "1",
    "name" :  "陈涛"
}

Here Insert Picture Description


②, get / query

The same need only send a GETrequest to obtain a specified document:
Syntax:

GET请求 ----> ip:端口/索引名称/类型名称/编号

Example:

GET请求 ----> ip:端口/zjitc/student/1

Here Insert Picture Description


③, delete

Send a DELETErequest to delete a specified document
syntax:

DELETE请求 ----> ip:端口/索引名称/类型名称/编号

Example:

DELETE请求 ----> ip:端口/zjitc/student/3

Here Insert Picture Description


④, to determine whether there

Send a HEADrequest to determine whether the presence of the specified document:
Syntax:

HEAD请求 ----> ip:端口/索引名称/类型名称/编号

Example:

HEAD请求 ----> ip:端口/zjitc/student/3

Here Insert Picture Description
Here Insert Picture Description
No data is returned if the presence of the data is returned 200
if there is no return404


⑤, update

PUTIt can also be used to update the document
syntax:

PUT请求 ----> ip:端口/索引名称/类型名称/编号

content:

{
    JSON数据
}

Example:

PUT请求 ----> ip:端口/zjitc/student/1
{
    "id" : "1",
    "name" :  "王王涛"
}

Here Insert Picture Description


2, Advanced Search

①, Search All

Use _search
cases:

GET请求 ----> ip:端口/zjitc/student/_search

Here Insert Picture Description


②, the search condition

Command line syntax

Use _search?q=属性名:属性值
cases:

GET请求 ----> ip:端口/zjitc/student/_search?q=id:2

Similar results can be sorted according to relevance Redis score SortedSet

Here Insert Picture Description


Query Expression Syntax

Lightweight search (command-line syntax) can be carried out easily by temporary command of the search but it has its own limitations
Elasticsearch provides a rich and flexible query language called [ query expression ] which supports building a more robust and complex queries
single search terms:
syntax:

POST请求 ----> ip:端口/索引名称/类型名称/_search

content:

{
    "query" : {
        "match" : {
            "搜索条件key" : "搜索条件value"
        }
    }
}

Example:

POST请求 ----> /zjitc/student/_search
{
    "query" : {
        "match" : {
            "id" : "1"
        }
    }
}

Here Insert Picture Description


Complex search (multi-criteria search):
to filter query results by adding a filter
syntax:

POST请求 ----> ip:端口/索引名称/类型名称/_search

content:

{
    "query" : {
        "bool": {
            "must": {
                "match" : {
                    "首选搜索条件key" : "首选搜索条件value" 
                }
            },
            "filter": {
                "range" : {
                    "次选搜索条件key" : { "判断符" : 次选搜索条件value } 
                }
            }
        }
    }
}

Example:

POST请求 ----> ip:端口/zjitc/student/_search
{
    "query" : {
        "bool": {
            "must": {
                "match" : {
                    "name" : "涛" 
                }
            },
            "filter": {
                "range" : {
                    "num" : { "gt" : 1 } 
                }
            }
        }
    }
}

Here Insert Picture Description


③, full-text search

As long as the target field contains the value of the search condition can check out the
syntax:

POST请求 ----> ip:端口/索引名称/类型名称/_search

content:

{
    "query" : {
        "match" : {
            "搜索条件key" : "搜索条件value"
        }
    }
}

Example:

POST请求 ----> ip:端口/zjitc/student/_search
{
    "query" : {
        "match" : {
            "about" : "rock climbing"
        }
    }
}

Here Insert Picture Description


④, phrase search

Exact match series of words or phrases that
match only contains "word 1" and "word 2" and both next to the phrase "word 1 word 2" is recorded in the form of
the syntax:

POST请求 ----> ip:端口/索引名称/类型名称/_search

content:

{
    "query" : {
        "match_phrase" : {
            "搜索条件key" : "搜索条件value"
        }
    }
}

Example:

POST请求 ----> ip:端口/zjitc/student/_search
{
    "query" : {
        "match_phrase" : {
            "hobby" : "watch TV"
        }
    }
}

In the above example will match with the attribute values attached to the hobby watch TV document
if the watch with or will not match the TV


⑤, highlighting search

In each of the search results highlighted some of the text fragments in order to let the user know why the document match the query
syntax:

POST请求 ----> ip:端口/索引名称/类型名称/_search

content:

{
    "query" : {
        "match_phrase" : {
            "搜索条件key" : "搜索条件value"
        }
    },
    "highlight": {
        "fields" : {
            "要高亮的属性名" : {}
        }
    }
}

Example:

POST请求 ----> ip:端口/zjitc/student/_search
{
    "query" : {
        "match_phrase" : {
            "name" : "涛"
        }
    },
    "highlight": {
        "fields" : {
            "name" : {}
        }
    }
}

Here Insert Picture Description


Published 176 original articles · won praise 5 · Views 300,000 +

Guess you like

Origin blog.csdn.net/Piconjo/article/details/105279672