ElasticSearch基础操作

1.启动es后执行查询

curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/_count?pretty' -d ' { "query": { "match_all": {} } } '

 

2.创建雇员信息

curl  -H "Content-Type: application/json" -XPUT 'http://localhost:9200/megacorp/employee/1' -d '
		{
		    "first_name" : "John",
		    "last_name" :  "Smith",
		    "age" :        25,
		    "about" :      "I love to go rock climbing",
		    "interests": [ "sports", "music" ]
		}
		'


		curl  -H "Content-Type: application/json" -XPUT 'http://localhost:9200/megacorp/employee/2' -d '
		{
		    "first_name" :  "Jane",
		    "last_name" :   "Smith",
		    "age" :         32,
		    "about" :       "I like to collect rock albums",
		    "interests":  [ "music" ]
		}
		'

		curl  -H "Content-Type: application/json" -XPUT 'http://localhost:9200/megacorp/employee/3' -d '
		{
		    "first_name" :  "Douglas",
		    "last_name" :   "Fir",
		    "age" :         35,
		    "about":        "I like to build cabinets",
		    "interests":  [ "forestry" ]
		}

		'

 

 

3.检索指定文档

curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/megacorp/employee/1' curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/megacorp/employee/2' curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/megacorp/employee/3'

 

 

 

4.检索全部文档

curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/megacorp/employee/_search'

 

返回结果包括了所有三个文档,放在数组 hits 中。一个搜索默认返回十条结果。

 

5.添加参数搜索

curl -X GET "localhost:9200/megacorp/employee/_search?q=last_name:Smith"

 

q=后边为参数名 : value

6.使用表达式查询

curl -X GET "localhost:9200/megacorp/employee/_search" -H 'Content-Type: application/json' -d' { "query" : { "match" : { "last_name" : "Smith" } } } '

 

 

 

7.更复杂的查询:年龄>30且最后一个名字为smith的

filter作为一个范围查询

curl -X GET "localhost:9200/megacorp/employee/_search" -H 'Content-Type: application/json' -d'
		{
		    "query" : {
		        "bool": {
		            "must": {
		                "match" : {
		                    "last_name" : "smith" 
		                }
		            },
		            "filter": {
		                "range" : {
		                    "age" : { "gt" : 30 } 
		                }
		            }
		        }
		    }
		}
		'

 

 

8.全文搜索

搜索下所有喜欢攀岩(rock climbing)的雇员:

curl -X GET "localhost:9200/megacorp/employee/_search" -H 'Content-Type: application/json' -d'
		{
		    "query" : {
		        "match" : {
		            "about" : "rock climbing"
		        }
		    }
		}
		'

 

9.短语查询

仅匹配同时包含 “rock” 和 “climbing” ,并且以短语 “rock climbing” 的形式紧挨着的雇员记录。

使用一个叫做 match_phrase 的查询:

	curl -X GET "localhost:9200/megacorp/employee/_search" -H 'Content-Type: application/json' -d'
			{
			    "query" : {
			        "match_phrase" : {
			            "about" : "rock climbing"
			        }
			    }
			}
			'

 

 

10.聚合

 

聚合前需要先执行操作一下以下代码:

因为:5.x后对排序,聚合这些操作用单独的数据结构(fielddata)缓存到内存里了,text需要单独开启,

https://blog.csdn.net/u011403655/article/details/71107415

https://www.elastic.co/guide/en/elasticsearch/reference/current/fielddata.html

 curl -X PUT "localhost:9200/megacorp/_mapping/employee/" -H 'Content-Type: application/json' -d'
{
  "properties": {
    "interests": { 
      "type":     "text",
      "fielddata": true
    }
  }
}
'


curl -X GET "localhost:9200/megacorp/employee/_search" -H 'Content-Type: application/json' -d' { "aggs": { "all_interests": { "terms": { "field": "interests" } } } } '

 

11.聚合的分级汇总

 

查询特定兴趣爱好员工的平均年龄:

curl -X GET "localhost:9200/megacorp/employee/_search" -H 'Content-Type: application/json' -d'
{
   "aggs" : {
        "all_interests" : {
            "terms" : { "field" : "interests" },
            "aggs" : {
                "avg_age" : {
                    "avg" : { "field" : "age" }
                }
            }
        }
    }
}
'

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值