ElasticSearch_(5)搜索的简单使用

准备工作

删除已有的nba索引

curl -X DELETE "localhost:9200/nba"

新增一个索引,并且指定mapping

curl -X PUT "localhost:9200/nba" -H 'Content-Type:application/json' -d '
{
	"mappings":{
		"properties":{
			"name":{
				"type":"text"
			},
			"team_name":{
				"type":"text"
			},
			"position":{
				"type":"text"
			},
			"play_year":{
				"type":"long"
			},
			"jerse_no":{
				"type":"keyword"
			}
		}
	}
}
'

新增文档

curl -X PUT "localhost:9200/nba/_doc/1" -H 'Content-Type:application/json' -d '
{
	"name":"哈登",
	"team_name":"火箭",
	"position":"得分后卫",
	"play_year":10,
	"jerse_no":"13"
}
'
curl -X PUT "localhost:9200/nba/_doc/2" -H 'Content-Type:application/json' -d '
{
	"name":"库里",
	"team_name":"勇士",
	"position":"控球后卫",
	"play_year":10,
	"jerse_no":"30"
}
'
curl -X PUT "localhost:9200/nba/_doc/3" -H 'Content-Type:application/json' -d '
{
	"name":"詹姆斯",
	"team_name":"湖人",
	"position":"小前锋",
	"play_year":15,
	"jerse_no":"23"
}
'

搜索

term(词条)查询

词条查询:词条查询不会分析查询条件,只有当词条和查询字符串完全匹配时,才匹配搜索

单条term查询 POST _search

curl -X POST "localhost:9200/nba/_search" -H 'Content-Type:application/json' -d '
{
	"query":{
		"term":{
			"jerse_no":"23"
		}
	}
}
'

took 表示查询的时间
_shards 分片的信息
hits 搜索结果的关键
在这里插入图片描述

多条terms查询 POST _search

curl -X POST "localhost:9200/nba/_search" -H 'Content-Type:application/json' -d '
{
	"query":{
		"terms":{
			"jerse_no":[
				"23",
				"13"
			]
		}
	}
}
'

在这里插入图片描述
在这里插入图片描述

full text(全文)查询

全文查询:ES引擎会先分析查询字符串,将其拆分成多个分词,只要已分析的字符中包含词条的任意一个,或全部包含、就匹配查询条件,返回文档;如果不包含任意一个分词,表示没有任何文档匹配查询条件
match 针对 text 类型

match_all 查询全部文档

默认显示10条记录

curl -X POST "localhost:9200/nba/_search" -H 'Content-Type:application/json' -d '
{
	"query":{
		"match_all":{}
	}
}
'

返回一个json

{
	"took": 23,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 3,
			"relation": "eq"
		},
		"max_score": 1.0,
		"hits": [
			{
				"_index": "nba",
				"_type": "_doc",
				"_id": "1",
				"_score": 1.0,
				"_source": {
					"name": "哈登",
					"team_name": "火箭",
					"position": "得分后卫",
					"play_year": 10,
					"jerse_no": "13"
				}
			},
			{
				"_index": "nba",
				"_type": "_doc",
				"_id": "2",
				"_score": 1.0,
				"_source": {
					"name": "库里",
					"team_name": "勇士",
					"position": "控球后卫",
					"play_year": 10,
					"jerse_no": "30"
				}
			},
			{
				"_index": "nba",
				"_type": "_doc",
				"_id": "3",
				"_score": 1.0,
				"_source": {
					"name": "詹姆斯",
					"team_name": "湖人",
					"position": "小前锋",
					"play_year": 15,
					"jerse_no": "23"
				}
			}
		]
	}
}

指定分页 查询所有

从0开始,数量为100

curl -X POST "localhost:9200/nba/_search" -H 'Content-Type:application/json' -d '
{
	"query":{
		"match_all":{}
	},
	"from":0,
	"size":100
}
'

match 查询字符中包含词条的任意一个 数据

curl -X POST "localhost:9200/nba/_search" -H 'Content-Type:application/json' -d '
{
	"query":{
		"match":{
			"name":"库小里"
		}
	},
	"from":0,
	"size":100
}
'

在这里插入图片描述

curl -X POST "localhost:9200/nba/_search" -H 'Content-Type:application/json' -d '
{
	"query":{
		"match":{
			"position":"后卫"
		}
	}
}
'

返回一个json

{
	"took": 6,
	"timed_out": false,
	"_shards": {
		"total": 1,
		"successful": 1,
		"skipped": 0,
		"failed": 0
	},
	"hits": {
		"total": {
			"value": 2,
			"relation": "eq"
		},
		"max_score": 0.90630186,
		"hits": [
			{
				"_index": "nba",
				"_type": "_doc",
				"_id": "1",
				"_score": 0.90630186,
				"_source": {
					"name": "哈登",
					"team_name": "火箭",
					"position": "得分后卫",
					"play_year": 10,
					"jerse_no": "13"
				}
			},
			{
				"_index": "nba",
				"_type": "_doc",
				"_id": "2",
				"_score": 0.90630186,
				"_source": {
					"name": "库里",
					"team_name": "勇士",
					"position": "控球后卫",
					"play_year": 10,
					"jerse_no": "30"
				}
			}
		]
	}
}

multi_match 多个查询

更新一下id=2的数据

curl -X POST "localhost:9200/nba/_update/2" -H 'Content-Type:application/json' -d '
{
	"doc":{
		"name":"库里",
		"team_name":"勇士",
		"position":"控球后卫",
		"play_year":10,
		"jerse_no":"30",
		"title":"the best shooter"
	}
}
'

title 和 name 字段里包含 shooter 的数据

curl -X POST "localhost:9200/nba/_search" -H 'Content-Type:application/json' -d '
{
	"query":{
		"multi_match":{
			"query":"shooter",
			"fields":["title","name"]
		}
	}
}
'

match_phrase 类似与词条查询(精确查询)

curl -X POST "localhost:9200/nba/_search" -H 'Content-Type:application/json' -d '
{
	"query":{
		"match_phrase":{
			"position":"得分后卫"
		}
	}
}
'

在这里插入图片描述

match_phrase_prefix 满足单词前缀的数据(模糊查询)

更新一下id=3数据

curl -X POST "localhost:9200/nba/_update/3" -H 'Content-Type:application/json' -d '
{
	"doc":{
		"name":"詹姆斯",
		"team_name":"湖人",
		"position":"小前锋",
		"play_year":15,
		"jerse_no":"23",
		"title":"the best small forward"
	}
}
'
curl -X POST "localhost:9200/nba/_search" -H 'Content-Type:application/json' -d '
{
	"query":{
		"multi_match":{
			"query":"shooter",
			"fields":["title","name"]
		}
	}
}
'

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Elasticsearch是一个开源的分布式搜索和分析引擎,基于Apache Lucene搜索库构建。它被广泛用于日志分析、全文检索、业务监控等场景。 在Java中使用Elasticsearch可以通过Java API来操作。以下是一个简单的示例: 1. 在pom.xml文件中添加Elasticsearch依赖: ``` <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.9.2</version> </dependency> ``` 2. 创建Elasticsearch客户端: ``` RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost("localhost", 9200, "http"))); ``` 3. 创建索引: ``` CreateIndexRequest request = new CreateIndexRequest("my_index"); client.indices().create(request, RequestOptions.DEFAULT); ``` 4. 添加文档: ``` IndexRequest request = new IndexRequest("my_index"); request.id("1"); String jsonString = "{" + "\"name\":\"John\"," + "\"age\":30," + "\"city\":\"New York\"" + "}"; request.source(jsonString, XContentType.JSON); IndexResponse response = client.index(request, RequestOptions.DEFAULT); ``` 5. 搜索文档: ``` SearchRequest request = new SearchRequest("my_index"); SearchSourceBuilder builder = new SearchSourceBuilder(); builder.query(QueryBuilders.matchQuery("name", "John")); request.source(builder); SearchResponse response = client.search(request, RequestOptions.DEFAULT); ``` 这是一个简单的示例,实际使用中还可以使用更多的API来操作Elasticsearch
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值