day2:利用Elasticsearch来搜索数据

转自:https://juejin.im/post/6844904030037245959

 

【_settings】

 磁盘满了以后,es会自动置为 read_only为True,此时需要运维手动操作开启read_only为False

curl -H 'Content-Type:application/json' -XPUT 'http://localhost:10290/apollo/_settings' -d '{"index.blocks.read_only_allow_delete":"false"}'

【_search】

curl -H "Content-Type:application/json" -XGET "http://127.0.0.1:10290/apollo/_search?pretty" -d '{"ip":xx.xx.xx.xx, 'port':'8000'}'

or :should
and: must

curl -H "Content-Type:application/json" -XGET "http://127.0.0.1:10290/apollo/_search?pretty" -d '{"query":{"bool":{"must":[{"term":{"ip":"x.x.x.x"}},{"term":{"port":"xxx"}},{"bool":{"should":[{"term":{"age":"xxx"}},{"term":{"name":"xxx"}}]}}]}}}'

{
	"query": {
		"bool": {
			"must": [
				{
					"term": {
						"ip": "x.x.x.x"
					}
				},
				{
					"term": {
						"port": "xxx"
					}
				},
				{
					"bool": {
						"should": [
							{
								"term": {
									"age": "xxx"
								}
							},
							{
								"term": {
									"name": "xxx"
								}
							}
						]
					}
				}
			]
		}
	}
}

  

 

【_count】
curl -H "Content-Type:application/json" -XGET "http://127.0.0.1:10290/apollo/_count?pretty" -d '{"query":{"bool":{"must":[{"term":{"sex":"男"}}]}}}'

 

【_mapping】

curl -H 'Content-type: application/json' -XGET 'http://localhost:10290/apollo/_mapping?pretty'

 

查指定条数文档:

GET /_search?size=20&pretty


从第2页开始查:

GET twitter/_search?size=2&from=2


查设置:/_settings?pretty'

查当前index的字段属性映射: _mapping

查询数据条数:_count

关闭开启索引: _open/_close Index
关闭索引后会阻止读写操作,关闭索引会占用大量磁盘空间
可以通过将 cluster.indices.close.enable 的默认值从 true 更改为 false 来禁用关闭索引功能,以避免发生意外。

 

复合查询:

query--bool--must | filter | must_not | should -- term

POST _search

{
	"query": {
		"bool": {
			"must": {
				"term": {
					"user": "kimchy"
				}
			},
			"filter": {
				"term": {
					"tag": "tech"
				}
			},
			"must_not": {
				"range": {
					"age": {
						"gte": 10,
						"lte": 20
					}
				}
			},
			"should": [
				{
					"term": {
						"tag": "wow"
					}
				},
				{
					"term": {
						"tag": "elasticsearch"
					}
				}
			],
			"minimum_should_match": 1,
			"boost": 1.0
		}
	}
"explain": true
}

 

字段是否存在:
字段为空时也不显示
GET twitter/_search { "query": { "exists": { "field": "city" } } }


匹配短语:
匹配不分先后顺序
1)此处happy 和 birthday 是 或关系
GET twitter/_search { "query": { "match": { "message": "happy birthday" } } }
2)与关系:
GET twitter/_search { "query": { "match": { "message": { "query": "happy birthday", "operator": "and" } } } }
3)短语中的词至少有两个匹配:
GET twitter/_search { "query": { "match": { "message": { "query": "happy birthday", "minimum_should_match": 2 } } } }

4)匹配短句,要求 Happy 必须在 birthday 之前
GET twitter/_search { "query": { "match_phrase": { "message": "Happy birthday" } }, "highlight": { "fields": { "message": {} } } }


SQL 查询:
未成功,后期再试
GET /_sql/translate { "query": """ SELECT * FROM twitter WHERE age = 30 """ }

multi-search-api
通过_msearch终点来实现在一个API请求中做多个查询,对多个index进行同时操作
GET twitter/_msearch
{"index":"twitter"}
{"query":{"match_all":{}},"from":0,"size":1}

{"index":"twitter"}
{"query":{"bool":{"filter":{"term":{"city.keyword":"北京"}}}}, "size":1}

{"index":"twitter1"}
{"query":{"match_all":{}}}


可以使用通配符对多个索引同时操作:
GET twitter*/_search

GET /twitter,twitter1/_search
但是
GET /twitter, twitter1/_search 不行,因为两个索引之间多了一个空格

 

posted @ 2020-10-26 11:12  bhz  阅读(101)  评论(0编辑  收藏  举报