Elasticsearch常用操作api

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": "***x",
"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"
]
}'

posted @ 2018-10-11 11:26  helloemk  阅读(132)  评论(0编辑  收藏  举报