一、restful接口规则
curl -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' -d '<BODY>'
被 < >
标记的部件:
| 适当的 HTTP 方法 或 谓词 : |
|
|
| Elasticsearch 集群中任意节点的主机名,或者用 |
| 运行 Elasticsearch HTTP 服务的端口号,默认是 |
| API 的终端路径(例如 |
| 任意可选的查询字符串参数 (例如 |
| 一个 JSON 格式的请求体 (如果请求需要的话) |
二、使用postman操作elastic search
1.查询集群中的文档数量
http://localhost:9200/_count?pretty
2.操作索引
2.1创建索引
http://localhost:9200/article
新建索引时,可以直接配置好mapping,一起创建。修改mapping如下
2.2修改索引(mapping)
mapping中:
properties:关系映射的属性
properties.content等等:映射的字段
properties.content.type: 字段类型
properties.content.analyzer:分词器配置
properties.content.search_analyzer:查询分词器
注:ik是一个中文分词器,不指定的话,默认使用标准分词器
http://localhost:9200/article/_mapping
2.3删除索引
http://localhost:9200/article
二、文档操作
1.新增文档
http://localhost:9200/article/_doc/1
2.根据id查询文档
http://localhost:9200/article/_doc/1
3.修改文档
http://localhost:9200/article/_doc/4
4.查询文档
4.1 match
match会对搜索的句子,进行分词,至少匹配到一个词语,再查询。下图的例子中,title为文档中字段名称
localhost:9200/article/_doc/_search
{
"query":{
"match":{
"title": "中国"
}
}
}
4.2 match_phrase 短语查询
短语查询,需要匹配所有分词。即文档字段中需要包含所有短语的分词。如果需要修改匹配个数,可通过调节可调节因子配置;slop为配置可调节因子配置项。
http://localhost:9200/article/_doc/_search
{
"query":{
"match_phrase":{
"title":"美国不幸身亡"
}
}
}
配置可调节因子
4.3 muti_match
匹配多个字段。
结果涉及评分问题:
(1)完全匹配的文档占的评分比较高,则需要使用best_fields
(2)越多字段匹配的文档评分越高,就要使用most_fields
http://localhost:9200/article/_doc/_search
{
"query":{
"multi_match":{
"query":"中国的疫情防控",
"type":"best_fields",
"fields":[
"title",
"content"
]
}
}
}