1 Data preparation
1.1 Create an index library
Create a new test index library search_demo
1.2 set index mapping
structure
POST /search_demo/_mapping
{
"properties":{
"id": {
"type": "long"
},
"age": {
"type": "integer"
},
"username": {
"type": "keyword"
},
"nickname": {
"type": "text",
"analyzer": "ik_max_word",
},
"money": {
"type": "float"
},
"desc": {
"type": "text",
"analyzer": "ik_max_word"
},
"sex": {
"type": "byte"
},
"birthday": {
"type": "date"
},
"face": {
"type": "text",
"index": false
}
}
}
1.3 Document data
CSDN download address: https://download.csdn.net/download/qq_15769939/15577363
1.4 Customize Chinese word segmentation
For details, please view the document https://blog.csdn.net/qq_15769939/article/details/114435973
配置分词器
[root@localhost config]# vi /usr/local/elasticsearch-7.4.2/plugins/ik/config/custom.dic
好的
好
的
重启es服务
[root@localhost config]# /usr/local/elasticsearch-7.4.2/bin/elasticsearch -d
2 Search
2.1 General parameter query
Common query may be referred to QueryString
query, as a parameter in the request url parameter.
Queries 字段
containing 内容
documents
GET /search_demo/_doc/_search?q=desc:test
2.2 DSL query
QueryString
It is rarely used, and it is difficult to construct once the parameters are complex, so most queries will use DSL to query.
- Domain Specific Language
- Domain-specific language
- Data query based on JSON format
- The query is more flexible, which is conducive to complex queries
查询统一请求路径
POST /search_demo/_doc/_search
2.2.1 Query the specified field
传递JSON数据
{
"query": {
"match": {
"desc": "项目"
}
}
}
请求结果
{
"took": 16,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.4514983,
"hits": [
{
"_index": "search_demo",
"_type": "_doc",
"_id": "1001",
"_score": 1.4514983,
"_source": {
"id": 1001,
"age": 18,
"username": "Tic",
"nickname": "飞翔的荷兰号",
"money": 88.8,
"desc": "我在p2pi网站解决项目中遇到的问题,学习到了很多知识",
"sex": 0,
"birthday": "1992-12-24",
"face": "http://www.p2pi.cn/static/img/1001_face.png"
}
}
]
}
}
2.2.2 Determine whether a field exists
传递JSON数据
{
"query": {
"exists": {
"field": "test"
}
}
}
请求结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 0,
"relation": "eq"
},
"max_score": null,
"hits": []
}
}
- The grammatical format is a json Object, the content is key-value key-value pairs, json can be looped and nested
- key can be some es keywords or a field
2.2.3 Problem location
During the DSL query process, there will be some error queries. Most of these problems are that json cannot be parsed by es, and es will report abnormal information and return. You can judge the problem based on the abnormal information, for example, the json format is incorrect and the keyword does not exist. , Unregistered, etc.
例子:
{
"query2": {
"exists": {
"field": "test"
}
}
}
请求结果
{
"error": {
"root_cause": [
{
"type": "parsing_exception",
"reason": "Unknown key for a START_OBJECT in [query2].",
"line": 2,
"col": 15
}
],
"type": "parsing_exception",
"reason": "Unknown key for a START_OBJECT in [query2].",
"line": 2,
"col": 15
},
"status": 400
}
3 Related information
- The blog post is not easy, everyone who has worked so hard to pay attention and praise, thank you