preface
Elastic Search relies on Restful apis to operate on indexes. It is mainly divided into two categories, one is index management API, including index creation, modification, deletion, query, document creation, modification, deletion and so on. There is also a query class, which includes the retrieval of documents under various conditions.
Here we have a scene. Here we assume that we have a set of authors, each with an id, name, gender, age, describing several fields. Each author writes a list of articles, including the article id, title, author id, content, and date of publication fields. We need to query the author by name, age, keywords in the description, and we need to query the article by publication time, title keywords, content keywords, and author.
So let’s analyze it here. We need to build two indexes, an author index and an article index. The description field of author index and the title and content field of article index need to be segmented.
All of the following actions are based on the previous Elastic Search Installation and Configuration. Most rest API requests are made in the Postman tool.
Install the word segmentation parser
To search by keyword, you need to segment the value of the corresponding field. For Chinese, you need to specify a special word segmentation plug-in. Here we use the IK participle plugin. This plugin has an installation package for Elastic Search. Be sure to install the corresponding version. The previously installed Elastic Search version is 6.1.1, so use the following command to install it:
elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.1.1/elasticsearch-analysis-ik-6.1.1.zip
Copy the code
Once installed, restart Elastic Search and run the command
Curl http://10.110.2.53:9200/_cat/plugins Master Analysis-IK 6.1.1Copy the code
As you can see, the word divider for IK 6.1.1 has been installed
The index operation
Create indexes
A typical rest API call for index creation would look like this:
curl -X PUT -H 'Content-Type:application/json' \
http://10.110.2.53:9200/author -d ' { "settings": { "index": { "number_of_shards": 6, "number_of_replicas": 0 } }, "mappings": { "person": { "properties": { "name": { "type": "text" }, "sex": { "type": "text" }, "age": { "type": "integer" }, "des": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word" } } } } } '
Copy the code
This contains two parts, the first part is index Setting, and the second part is index Mapping.
The index set
Index Settings Settings. Index is used to set the configuration of an index. In this example, the number of index fragments (number_of_shards) is set to 6 and the number of index replicas (number_of_replicas) is set to 0. Because our experimental environment is single-node, no replicas are created. Otherwise, the health status of the index will be yellow because the replica cannot be created on the second node.
The index map
Index mapping is used to specify the configuration of fields in an index when creating an index. The configuration includes
- Whether a field can be full-text indexed (word segmentation)
- The type of data stored in the field
- The format of the data displayed in the field
- Whether the field should be placed in the _all built-in field. This feature has been declared deprecated in version 6.0
Explicit index mapping
When creating indexes, explicitly setting the mapping of each field with the mappings parameter is called an explicit index mapping. As in the example above.
Dynamic index mapping
In addition to specifying the mapping of index fields when creating an index, a simple way to create an index is to not specify the mapping of index fields. Elastic Search will use dynamic mapping for indexes to automatically push for automatic storage types, storage formats, etc. when indexing a field for the first time.
For example, we can create an index of authors by using the following method
The curl -x PUT http://10.110.2.53:9200/authorCopy the code
We do not set any mapping information for this index. When we create the first document for this index with the following information, we automatically map name, sex, and DES to text and age to long.
{
"name":"Yang Chao"."age": 24,"sex":"Male"."des":"IT software engineer with expertise in Java and software architecture"
}
Copy the code
In addition to creating indexes, there are apis for modifying indexes, querying indexes, and dropping indexes. More detailed usage of the indexing API can be found in the official documentation
The document query
To query the author index details, run the following command:
The curl http://10.110.2.53:9200/author? pretty {"author" : {
"aliases": {},"mappings" : {
"doc" : {
"properties" : {
"age" : {
"type" : "text"."fields" : {
"keyword" : {
"type" : "keyword"."ignore_above": 256}}},"des" : {
"type" : "text"."analyzer" : "ik_max_word"
},
"name" : {
"type" : "text"
},
"age" : {
"type" : "integer"
},
"sex" : {
"type" : "text"}}}},"settings" : {
"index" : {
"creation_date" : "1515160270198"."number_of_shards" : "6"."number_of_replicas" : "0"."uuid" : "v1E0_mfAR5qRIroOV31HOA"."version" : {
"created" : "6010199"
},
"provided_name" : "author"}}}}Copy the code
Document indexing
The following command indexes an author document into an author index
curl -H 'Content-Type:application/json' http://10.110.2.53:9200/author -d '{" name ":" lee kau chau ", "age" : 23, "sex", "female", "des" : "the IT manager software, good at Java and development management"}'
Copy the code
The final index to the author index and article index is listed below
The document query
The full text indexing
curl -X GET 'http://10.110.2.53:9200/article/_search? Q = docker build gitlab&pretty '
{
"took" : 5,
"timed_out" : false."_shards" : {
"total" : 6,
"successful" : 6,
"skipped": 0."failed": 0}."hits" : {
"total" : 1,
"max_score": 1.3862944."hits": [{"_index" : "article"."_type" : "doc"."_id" : "rKOjxmABQGn3FeQBeqAg"."_score": 1.3862944."_source" : {
"title" : "Build your own GitLab service with Docker"."author" : "qaOXxmABQGn3FeQBMqCA"."created" : "The 2018-01-01 15:11:11"."context" : "Git is the version management system of the day. It's almost impossible to say hello to anyone unless you're working on a Git version management system. There are plenty of Internet Git services available, such as GitHub, a social network for programmers, and Bitbucket, which is quietly useful. There is no problem with these being used by individuals or companies for open source use. However, if the use is promoted within the department, the code can not be disclosed or additional costs will be involved. I used to manage the code warehouse on the Linux server manually in the department. Permissions can't be set, and it's very inconvenient. So I have been very upset."}}]}}Copy the code
Complex queries
curl -X POST -H 'Content-Type:application/json' http://10.110.2.53:9200/author/_search?pretty -d ' { "took" : 5, "timed_out" : false, "_shards" : { "total" : 6, "successful" : 6, "skipped" : 0, "failed" : , "hits" : {0} "total" : 2, "max_score" : 0.2876821, "hits" : [{" _index ":" the author ", "_type" : "doc", "_id" : "QaOXxmABQGn3FeQBMqCA", "_score" : 0.2876821, "_source" : {" name ":" Yang Gaochao ", "age" : "24", "sex", "male", "des" : {"_index" : "author", "_type" : "doc", "_id" : "qqOXxmABQGn3FeQBiKCD", "_score" : "qqOXxmABQGn3FeQBiKCD" 0.2876821, the "_source" : {" name ":" lee kau chau ", "age" : "23", "sex", "female", "des" : "the IT manager software, good at Java and development management"}}}}]Copy the code
Queries with Boolean calculations
curl -X POST -H 'Content-Type:application/json' http://10.110.2.53:9200/author/_search?pretty -d '{" query ": {" bool" : {" must ": [{" match" : {" des ":" manager "}}, {" match ": {" name" : "high"}}]}}}'
Copy the code
This query results in name containing “superb” and DES containing the author of “manager”
You can refer to the official documentation for more detailed query usage. You can perform more queries based on our samples and documentation.
Afterword.
For details on how to use the Elastic Search Api, see the official website. We actually use it in our program. They all operate using high-level apis. Later we will use other articles to illustrate.