博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Elasticsearch常用操作api
阅读量:5143 次
发布时间:2019-06-13

本文共 3008 字,大约阅读时间需要 10 分钟。

1、查看所有Index

curl -X GET 'http://localhost:9200/_cat/indices?v'

2、查看每个Index下的type

curl 'localhost:9200/_mapping?pretty=true'

3、创建索引,且指定分词器ik

curl -H "Content-Type: application/json" -XPUT "http://localhost:9200/cmsdb_test" -d '
{
"settings": {
"index": {
"analysis.analyzer.default.type": "ik_smart" //智能分词
"analysis.analyzer.default.type": "ik_max_word" //最细分词
}
}
}'

4、新增一条数据 (put需要制定id,用post不需要制定id)

curl -X PUT 'localhost:9200/accounts/person/1' -d '
{
"user": "张三",
"title": "工程师",
"desc": "数据库管理"
}'
curl -X POST 'localhost:9200/accounts/person' -d '
{
"user": "李四",
"title": "工程师",
"desc": "系统管理"
}'

5、查看某一条记录

curl 'localhost:9200/accounts/person/1?pretty=true'

6、删除某一条记录

curl -X DELETE 'localhost:9200/accounts/person/1'

7、更新一条记录

curl -X PUT 'localhost:9200/accounts/person/1' -d '
{
"user" : "张三",
"title" : "工程师",
"desc" : "数据库管理,软件开发"
}'

8、查询某type下所有数据

curl 'localhost:9200/accounts/person/_search'

9、match查询并设置高亮字段

curl 'localhost:9200/accounts/person/_search' -d '
{
"query" : { "match" : { "desc" : "软件" }}
"highlight": {
"pre_tags": [
"<font color='red'>"
],
"post_tags": [
"</font>"
],
"fields": {
"desc" {}
}
}
}'

10、查询所有字段包含某个词

curl -XGET 'localhost:9200/accounts/person/_search' -d '
{
"query": {
"multi_match": {
"query": "习xxx",
"fields": [
"title"
]
}
}
}'

11、设置某个字段的权重,并设置返回的字段

curl -XGET 'localhost:9200/accounts/person/_search' -d '
{
"query": {
"multi_match" : {
"query" : "elasticsearch guide",
"fields": ["name", "title^3"]
}
},
"_source": ["name", "summary", "title"]
}'

12、布尔查询 must=and,should=or,must_not=not

curl -XGET 'localhost:9200/accounts/person/_search' -d '
{
"query": {
"bool": {
"must": {
"bool" : { "should": [
{ "match": { "title": "Elasticsearch" }},
{ "match": { "title": "Solr" }} ] }
},
"must": { "match": { "authors": "clinton gormely" }},
"must_not": { "match": {"authors": "radu gheorge" }}
}
}
}'

13、模糊度查询(Fuzzy Queries)

curl -XGET 'localhost:9200/accounts/person/_search' -d '
{
"query": {
"multi_match" : {
"query" : "comprihensiv guide",
"fields": ["title", "summary"],
"fuzziness": "AUTO"
}
},
"_source": ["title", "summary", "publish_date"],
"size": 1
}'

14、通配符查询(Wildcard Query)

curl -XGET 'localhost:9200/accounts/person/_search' -d '
{
"query": {
"wildcard" : {
"authors" : "t*"
}
},
"_source": ["title", "authors"],
"highlight": {
"fields" : {
"authors" : {}
}
}
}'

15、正则表达式查询(Regexp Query)

curl -XGET 'localhost:9200/accounts/person/_search' -d '
{
"query": {
"regexp" : {
"authors" : "t[a-z]*y"
}
},
"_source": ["title", "authors"],
"highlight": {
"fields" : {
"authors" : {}
}
}
}'

16、匹配短语查询(Match Phrase Query) (key,"title","sub_title","lead_title","summary","keyword","article_tag","source","author","editor","liability","content")

curl -XGET 'localhost:9200/accounts/person/_search' -d '
{
"query": {
"multi_match": {
"query": "search engine",
"fields": [
"title",
"summary"
],
"type": "phrase",
"slop": 3 //控制输入的trems之间有多少个单词仍然能够搜索到
}
},
"_source": [
"title",
"summary",
"publish_date"
]
}'

转载于:https://www.cnblogs.com/helloemk/p/9771457.html

你可能感兴趣的文章
短信编码总结
查看>>
了解HTML和Css样式
查看>>
关于settimer的一些新认识
查看>>
[转]ExtJs4 笔记(13) Ext.menu.Menu 菜单、Ext.draw.Component 绘图、Ext.resizer.Resizer 大小变更...
查看>>
1-5-06:奥运奖牌计数
查看>>
Windows下Python连接sqlite3数据库
查看>>
Javascript 类与静态类的实现(续)
查看>>
shim和polyfill有什么区别
查看>>
Failed to load the JNI shared library “E:/2000/Java/JDK6/bin/..jre/bin/client/jvm.dll
查看>>
Zabbix3.4服务器的搭建--CentOS7
查看>>
〖Python〗-- IO多路复用
查看>>
栈(括号匹配)
查看>>
夜太美---酒不醉--人自醉
查看>>
Java学习 · 初识 面向对象深入一
查看>>
源代码如何管理
查看>>
vue怎么将一个组件引入另一个组件?
查看>>
多线程学习笔记三之ReentrantLock与AQS实现分析
查看>>
【转】进程与线程的一个简单解释
查看>>
getopt,getoptlong学习
查看>>
数据的传递 变量与参数的使用
查看>>