elasticsearch文档相关命令(版本6.5.4)
创建
创建文档
req 发送请求(在索引school中创建一个文档):
curl --location --request POST '127.0.0.1:9200/school/_doc' \
--header 'Content-Type: application/json' \
--data '{
"name":"哈佛大学",
"address":"美国",
"age":50
}'
resp 响应结果:
{
"_index": "school",
"_type": "_doc",
"_id": "-O6UL4IBGLqpicsmDvYF",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
查询
查询索引中文档总数
http://127.0.0.1:9200/[索引库名称]/_count
查询所有的信息
http://127.0.0.1:9200/[索引库名称]/_search
查询结果美化
http://127.0.0.1:9200/[索引库名称]/_search?pretty
查询某个标题
http://127.0.0.1:9200/[索引库名称]/_search?q=title:标题
范围查询
大于某个值的文档(如:检索出所有年龄age大于99的大学)
curl --location --request GET '127.0.0.1:9200/school/_search' \
--header 'Content-Type: application/json' \
--data '{
"query": {
"range": {
"age": {
"gte": 99
}
}
}
}'
gt: greater than 大于
gte: greater than or equal 大于等于
lt: less than 小于
lte: less than or equal 小于等于
分页查询
from+size
http://127.0.0.1:9200/[索引库名称]/_search?from=开始编号&size=数量
分词
测试分词
curl --location --request GET 'http://127.0.0.1:9200/_analyze' \
--header 'Content-Type: application/json' \
--data '{
"tokenizer": "[分词器]",
"text": "[内容]"
}'
如:
curl --location --request GET 'http://127.0.0.1:9200/_analyze' \
--header 'Content-Type: application/json' \
--data-raw '{
"tokenizer": "query_ansj",
"text": "测试elsearch的使用query_ansj分词器分词的效果"
}'