16
头图
Due to the length of this article, if you want to get the PDF, please follow the 'Public Account-Rookie Growth Study Notes' and reply to the "es manual" to get the document.

es generalization

Elaticsearch, referred to as ES, ES is an open source highly scalable distributed full-text search engine, Elasticsearch is a document-oriented database, a piece of data is a document here.

essential elements

ES is a document database, which is different from traditional relational databases. The following is a one-to-one correspondence between the elements involved in ES and the relational database.

ElasticSearch index type document field
MySQL database data table Data row (row) Data column (column)

index operation

create index

Send a PUT request to the ES server: http://127.0.0.1:9200/shopping . To create an index, only PUT requests can be used. PUT is idempotent, that is to say, it will be created when it does not exist, and it will not be recreated when it exists, but will return the information that the index already exists.

 {
    "acknowledged": true,//响应结果
    "shards_acknowledged": true,//分片结果
    "index": "shopping"//索引名称
}

query index

Send a GET request to the ES server: http://127.0.0.1:9200/shopping .

 {
    "shopping": {//索引名
        "aliases": {},//别名
        "mappings": {},//映射
        "settings": {//设置
            "index": {//设置 - 索引
                "creation_date": "1617861426847",//设置 - 索引 - 创建时间
                "number_of_shards": "1",//设置 - 索引 - 主分片数量
                "number_of_replicas": "1",//设置 - 索引 - 主分片数量
                "uuid": "J0WlEhh4R7aDrfIc3AkwWQ",//设置 - 索引 - 主分片数量
                "version": {//设置 - 索引 - 主分片数量
                    "created": "7080099"
                },
                "provided_name": "shopping"//设置 - 索引 - 主分片数量
            }
        }
    }
}

View all indexes

Send a GET request to the ES server: http://127.0.0.1:9200/_cat/indices?v .

The _cat in the request path here indicates the meaning of viewing, and the indices indicate the index, so the overall meaning is to view all the indexes in the current ES server, just like the feeling of show tables in MySQL, and the server response results are as follows:

 health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   shopping J0WlEhh4R7aDrfIc3AkwWQ   1   1          0            0       208b           208b

drop index

Send a DELETE request to the ES server: http://127.0.0.1:9200/shopping .

The returned results are as follows:

 {
    "acknowledged": true
}

document manipulation

document creation

Assuming that the index has been created, let's create a document and add data. The document here can be compared to the table data in the relational database, and the added data format is JSON format

In Postman, send a POST request to the ES server: http://127.0.0.1:9200/shopping/_doc , the JSON content of the request body is:

 {
    "title":"小米手机",
    "category":"小米",
    "images":"http://www.gulixueyuan.com/xm.jpg",
    "price":3999.00
}

return result

 {
    "_index": "shopping",//索引
    "_type": "_doc",//类型-文档
    "_id": "ANQqsHgBaKNfVnMbhZYU",//唯一标识,可以类比为 MySQL 中的主键,随机生成
    "_version": 1,//版本
    "result": "created",//结果,这里的 create 表示创建成功
    "_shards": {//
        "total": 2,//分片 - 总数
        "successful": 1,//分片 - 总数
        "failed": 0//分片 - 总数
    },
    "_seq_no": 0,
    "_primary_term": 1
}
Note that the method of sending the document creation request here must be POST, not PUT, otherwise an error will occur.

After the above data is created, since no data unique identifier (ID) is specified, the ES server will randomly generate one by default.

If you want to customize the unique identifier, you need to specify: http://127.0.0.1:9200/shopping/_doc/1 , and the JSON content of the request body is:

 {
    "title":"小米手机",
    "category":"小米",
    "images":"http://www.gulixueyuan.com/xm.jpg",
    "price":3999.00
}

The returned results are as follows:

 {
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1",//<-----自定义唯一性标识
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 1,
    "_primary_term": 1
}

document query

When viewing a document, you need to specify the unique identifier of the document, similar to the primary key query of data in MySQL. In Postman, send a GET request to the ES server: http://127.0.0.1:9200/shopping/_doc/1 .

The returned results are as follows:

 {
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1",
    "_version": 1,
    "_seq_no": 1,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "title": "小米手机",
        "category": "小米",
        "images": "http://www.gulixueyuan.com/xm.jpg",
        "price": 3999
    }
}

Find non-existent content and send a GET request to the ES server: http://127.0.0.1:9200/shopping/_doc/1001 . The returned results are as follows:

 {
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1001",
    "found": false
}

View all data under the index and send a GET request to the ES server: http://127.0.0.1:9200/shopping/_search .

The returned results are as follows:

 {
    "took": 133,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 1,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "ANQqsHgBaKNfVnMbhZYU",
                "_score": 1,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            }
        ]
    }
}

Document deletion

Deleting a document does not immediately remove it from disk, it is simply marked as deleted (tombstone).

In Postman, send a DELETE request to the ES server: http://127.0.0.1:9200/shopping/_doc/1
Return result:

 {
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1",
    "_version": 4,
    "result": "deleted",//<---删除成功
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 4,
    "_primary_term": 1
}

document modification

Full modification

Just like adding a new document, input the same URL address to request, if the request body changes, the original data content will be overwritten

In Postman, send a POST request to the ES server: http://127.0.0.1:9200/shopping/_doc/1
The JSON content of the request body is:

 {
    "title":"华为手机",
    "category":"华为",
    "images":"http://www.gulixueyuan.com/hw.jpg",
    "price":1999.00
}

After the modification is successful, the server responds with the result:

 {
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1",
    "_version": 2,
    "result": "updated",//<---updated 表示数据被更新
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 2,
    "_primary_term": 1
}

local update

When modifying data, you can also modify only the local information of a given piece of data

In Postman, send a POST request to the ES server: http://127.0.0.1:9200/shopping/_update/1 .

The JSON content of the request body is:

 {
    "doc": {
        "title":"小米手机",
        "category":"小米"
    }
}

The returned results are as follows:

 {
    "_index": "shopping",
    "_type": "_doc",
    "_id": "1",
    "_version": 3,
    "result": "updated",//<----updated 表示数据被更新
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 3,
    "_primary_term": 1
}

URL pending conditional query

Find the document whose category is Xiaomi. In Postman, send a GET request to the ES server: http://127.0.0.1:9200/shopping/_search?q=category:小米 , the returned result is as follows:

 {
    "took": 94,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 1.3862942,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "ANQqsHgBaKNfVnMbhZYU",
                "_score": 1.3862942,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            ......
        ]
    }
}

The above is a query in the form of URL with parameters, which can easily make unscrupulous people malicious, or the parameter value will appear garbled in Chinese. To avoid these situations, we can use the request with JSON request body for query.

Request body with parameter query

Next, with the JSON request body, look for the document whose category is Xiaomi. In Postman, send a GET request to the ES server: http://127.0.0.1:9200/shopping/_search , with the JSON body as follows:

 {
    "query":{
        "match":{
            "category":"小米"
        }
    }
}

The returned results are as follows:

 {
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 1.3862942,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "ANQqsHgBaKNfVnMbhZYU",
                "_score": 1.3862942,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            ......
        ]
    }
}

Find all content with request body

Find all document content, you can also do this, in Postman, send a GET request to the ES server: http://127.0.0.1:9200/shopping/_search , with the following JSON body

 {
    "query":{
        "match_all":{}
    }
}

then returns all document content:

 {
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 6,
            "relation": "eq"
        },
        "max_score": 1,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "ANQqsHgBaKNfVnMbhZYU",
                "_score": 1,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            ......
        ]
    }
}

Query the specified field

If you want to query the specified field, in Postman, send a GET request to the ES server: http://127.0.0.1:9200/shopping/_search , with the following JSON body:

 {
    "query":{
        "match_all":{}
    },
    "_source":["title"]
}

The returned results are as follows:

 {
    "took": 5,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 6,
            "relation": "eq"
        },
        "max_score": 1,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "ANQqsHgBaKNfVnMbhZYU",
                "_score": 1,
                "_source": {
                    "title": "小米手机"
                }
            },
            ......
        ]
    }
}

Paging query

In Postman, send a GET request to the ES server: http://127.0.0.1:9200/shopping/_search with the following JSON body:

 {
    "query":{
        "match_all":{}
    },
    "from":0,
    "size":2
}

The returned results are as follows:

 {
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 6,
            "relation": "eq"
        },
        "max_score": 1,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "ANQqsHgBaKNfVnMbhZYU",
                "_score": 1,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
        ]
    }
}

query sort

If you want to find out the mobile phone with the highest price by sorting, in Postman, send a GET request to the ES server: http://127.0.0.1:9200/shopping/_search , with the following JSON body:

 {
    "query":{
        "match_all":{}
    },
    "sort":{
        "price":{
            "order":"desc"
        }
    }
}

The returned results are as follows:

 {
    "took": 96,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 6,
            "relation": "eq"
        },
        "max_score": null,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "ANQqsHgBaKNfVnMbhZYU",
                "_score": null,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                },
                "sort": [
                    3999
                ]
            },
            ......
        ]
    }
}

Multi-condition query

Suppose you want to find out the Xiaomi brand, the price is 3999 yuan. (must is equivalent to && in the database), in Postman, send a GET request to the ES server: http://127.0.0.1:9200/shopping/_search , with the following JSON body:

 {
    "query":{
        "bool":{
            "must":[{
                "match":{
                    "category":"小米"
                }
            },{
                "match":{
                    "price":3999.00
                }
            }]
        }
    }
}

The returned results are as follows:

 {
    "took": 134,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 2.3862944,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "ANQqsHgBaKNfVnMbhZYU",
                "_score": 2.3862944,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            }
        ]
    }
}

Suppose you want to find out the brands of Xiaomi and Huawei. (should is equivalent to || of the database) In Postman, send a GET request to the ES server: http://127.0.0.1:9200/shopping/_search , with the following JSON body:

 {
    "query":{
        "bool":{
            "should":[{
                "match":{
                    "category":"小米"
                }
            },{
                "match":{
                    "category":"华为"
                }
            }]
        },
        "filter":{
            "range":{
                "price":{
                    "gt":2000
                }
            }
        }
    }
}

The returned results are as follows:

 {
    "took": 8,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 6,
            "relation": "eq"
        },
        "max_score": 1.3862942,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "ANQqsHgBaKNfVnMbhZYU",
                "_score": 1.3862942,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            .....
        ]
    }
}

range query

Suppose you want to find out the brands of Xiaomi and Huawei, and the price of mobile phones is more than 2,000 yuan. In Postman, send a GET request to the ES server: http://127.0.0.1:9200/shopping/_search with the following JSON body:

 {
    "query":{
        "bool":{
            "should":[{
                "match":{
                    "category":"小米"
                }
            },{
                "match":{
                    "category":"华为"
                }
            }],
            "filter":{
                "range":{
                    "price":{
                        "gt":2000
                    }
                }
            }
        }
    }
}

The returned results are as follows:

 {
    "took": 72,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.3862942,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "ANQqsHgBaKNfVnMbhZYU",
                "_score": 1.3862942,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            }
        ]
    }
}

Full Text Search

This function is like a search engine, such as entering the brand "Xiaohua", the returned results bring back brands with "Xiaomi" and Huawei. In Postman, send a GET request to the ES server: http://127.0.0.1:9200/shopping/_search with the following JSON body:

 {
    "query":{
        "match":{
            "category" : "小华"
        }
    }
}

The returned results are as follows:

 {
    "took": 7,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 6,
            "relation": "eq"
        },
        "max_score": 0.6931471,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "ANQqsHgBaKNfVnMbhZYU",
                "_score": 0.6931471,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            ......
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "BtR6sHgBaKNfVnMbX5Y5",
                "_score": 0.6931471,
                "_source": {
                    "title": "华为手机",
                    "category": "华为",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 1999
                }
            },
            ......
        ]
    }
}

exact match

In Postman, send a GET request to the ES server: http://127.0.0.1:9200/shopping/_search with the following JSON body:

 {
    "query":{
        "match_phrase":{
            "category" : "为"
        }
    }
}

The returned results are as follows:

 {
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 0.6931471,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "BtR6sHgBaKNfVnMbX5Y5",
                "_score": 0.6931471,
                "_source": {
                    "title": "华为手机",
                    "category": "华为",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 1999
                }
            },
            ......
        ]
    }
}

Highlight query

In Postman, send a GET request to the ES server: http://127.0.0.1:9200/shopping/_search with the following JSON body:

 {
    "query":{
        "match_phrase":{
            "category" : "为"
        }
    },
    "highlight":{
        "fields":{
            "category":{}//<----高亮这字段
        }
    }
}

The returned results are as follows:

 {
    "took": 100,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 0.6931471,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "BtR6sHgBaKNfVnMbX5Y5",
                "_score": 0.6931471,
                "_source": {
                    "title": "华为手机",
                    "category": "华为",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 1999
                },
                "highlight": {
                    "category": [
                        "华<em>为</em>"//<------高亮一个为字。
                    ]
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "B9R6sHgBaKNfVnMbZpZ6",
                "_score": 0.6931471,
                "_source": {
                    "title": "华为手机",
                    "category": "华为",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 1999
                },
                "highlight": {
                    "category": [
                        "华<em>为</em>"
                    ]
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "CdR7sHgBaKNfVnMbsJb9",
                "_score": 0.6931471,
                "_source": {
                    "title": "华为手机",
                    "category": "华为",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 1999
                },
                "highlight": {
                    "category": [
                        "华<em>为</em>"
                    ]
                }
            }
        ]
    }
}

Group query

In Postman, send a GET request to the ES server: http://127.0.0.1:9200/shopping/_search with the following JSON body:

 {
    "aggs":{//聚合操作
        "price_group":{//名称,随意起名
            "terms":{//分组
                "field":"price"//分组字段
            }
        }
    }
}

The returned results are as follows:

 {
    "took": 63,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 6,
            "relation": "eq"
        },
        "max_score": 1,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "ANQqsHgBaKNfVnMbhZYU",
                "_score": 1,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 3999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "A9R5sHgBaKNfVnMb25Ya",
                "_score": 1,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 1999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "BNR5sHgBaKNfVnMb7pal",
                "_score": 1,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 1999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "BtR6sHgBaKNfVnMbX5Y5",
                "_score": 1,
                "_source": {
                    "title": "华为手机",
                    "category": "华为",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 1999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "B9R6sHgBaKNfVnMbZpZ6",
                "_score": 1,
                "_source": {
                    "title": "华为手机",
                    "category": "华为",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 1999
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "CdR7sHgBaKNfVnMbsJb9",
                "_score": 1,
                "_source": {
                    "title": "华为手机",
                    "category": "华为",
                    "images": "http://www.gulixueyuan.com/xm.jpg",
                    "price": 1999
                }
            }
        ]
    },
    "aggregations": {
        "price_group": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": 1999,
                    "doc_count": 5
                },
                {
                    "key": 3999,
                    "doc_count": 1
                }
            ]
        }
    }
}

The above return results will be accompanied by the original data. If you don't want the result without the original data, in Postman, send a GET request to the ES server: http://127.0.0.1:9200/shopping/_search , with the JSON body as follows:

 {
    "aggs":{
        "price_group":{
            "terms":{
                "field":"price"
            }
        }
    },
    "size":0
}

The returned results are as follows:

 {
    "took": 60,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 6,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "price_group": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": 1999,
                    "doc_count": 5
                },
                {
                    "key": 3999,
                    "doc_count": 1
                }
            ]
        }
    }
}

query average

In Postman, send a GET request to the ES server: http://127.0.0.1:9200/shopping/_search with the following JSON body:

 {
    "aggs":{
        "price_avg":{//名称,随意起名
            "avg":{//求平均
                "field":"price"
            }
        }
    },
    "size":0
}

The returned results are as follows:

 {
    "took": 14,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 6,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "price_avg": {
            "value": 2332.3333333333335
        }
    }
}

Mapping relations

With the index library, it is equivalent to having the database in the database. Next, the mapping in the index library (index) needs to be built, which is similar to the table structure (table) in the database (database). To create a database table, you need to set the field name, type, length, constraints, etc. The same is true for the index library. You need to know what fields are in this type and what constraint information each field has. This is called mapping.
First create an index:

 # PUT http://127.0.0.1:9200/user

Return result:

 {
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "user"
}

Create a map

 # PUT http://127.0.0.1:9200/user/_mapping

{
    "properties": {
        "name":{
            "type": "text",
            "index": true
        },
        "sex":{
            "type": "keyword",
            "index": true
        },
        "tel":{
            "type": "keyword",
            "index": false
        }
    }
}

The returned results are as follows:

 {
    "acknowledged": true
}

query mapping

 #GET http://127.0.0.1:9200/user/_mapping

The returned results are as follows:

 {
    "user": {
        "mappings": {
            "properties": {
                "name": {
                    "type": "text"
                },
                "sex": {
                    "type": "keyword"
                },
                "tel": {
                    "type": "keyword",
                    "index": false
                }
            }
        }
    }
}

add data

 #PUT http://127.0.0.1:9200/user/_create/1001
{
    "name":"小米",
    "sex":"男的",
    "tel":"1111"
}

The returned results are as follows:

 {
    "_index": "user",
    "_type": "_doc",
    "_id": "1001",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}

Find name with "small" data:

 #GET http://127.0.0.1:9200/user/_search
{
    "query":{
        "match":{
            "name":"小"
        }
    }
}

The returned results are as follows:

 {
    "took": 495,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0.2876821,
        "hits": [
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "1001",
                "_score": 0.2876821,
                "_source": {
                    "name": "小米",
                    "sex": "男的",
                    "tel": "1111"
                }
            }
        ]
    }
}

Find sex with "male" data:

 #GET http://127.0.0.1:9200/user/_search
{
    "query":{
        "match":{
            "sex":"男"
        }
    }
}

The returned results are as follows:

 {
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 0,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    }
}

Unwanted results are found because the type of "sex" is "keyword" when the mapping is created. "sex" can only be completely "male" to get the original data.

 #GET http://127.0.0.1:9200/user/_search
{
    "query":{
        "match":{
            "sex":"男的"
        }
    }
}

The returned results are as follows:

 {
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0.2876821,
        "hits": [
            {
                "_index": "user",
                "_type": "_doc",
                "_id": "1001",
                "_score": 0.2876821,
                "_source": {
                    "name": "小米",
                    "sex": "男的",
                    "tel": "1111"
                }
            }
        ]
    }
}

Enquiries

 # GET http://127.0.0.1:9200/user/_search
{
    "query":{
        "match":{
            "tel":"11"
        }
    }
}

The returned results are as follows:

 {
    "error": {
        "root_cause": [
            {
                "type": "query_shard_exception",
                "reason": "failed to create query: Cannot search on field [tel] since it is not indexed.",
                "index_uuid": "ivLnMfQKROS7Skb2MTFOew",
                "index": "user"
            }
        ],
        "type": "search_phase_execution_exception",
        "reason": "all shards failed",
        "phase": "query",
        "grouped": true,
        "failed_shards": [
            {
                "shard": 0,
                "index": "user",
                "node": "4P7dIRfXSbezE5JTiuylew",
                "reason": {
                    "type": "query_shard_exception",
                    "reason": "failed to create query: Cannot search on field [tel] since it is not indexed.",
                    "index_uuid": "ivLnMfQKROS7Skb2MTFOew",
                    "index": "user",
                    "caused_by": {
                        "type": "illegal_argument_exception",
                        "reason": "Cannot search on field [tel] since it is not indexed."
                    }
                }
            }
        ]
    },
    "status": 400
}

The error is only because the "index" of "tel" is false when the mapping is created.


Mandy
412 声望627 粉丝