/test 为索引名称
一、创建索引
PUT /test
PUT 创建请求
二、删除索引
DELETE /test
DELETE 删除请求
三、查询索引数据结构
GET /test
四、查询索引数据
GET /test/_search
GET 查询请求 /_search 执行搜索操作
#文本与结构化数据查询
#match为文本查询,当字段有设置分词器时会进行相应的分词操作
#term为结构化数据查询,term查询将按照储存再到排序引中的确切字词进行操作,这些查询通常用于数字,日期和枚举等结构化数据,而不是全文本字段
GET /test/_search
{
"query":{
"term": {
"id": 1
}
}
}
GET /test/_search
{
"query":{
"match": {
"name": "王"
}
}
}
#_source 指定查询的字段
GET /test/_search
{
"query":{
"match": {
"name": "王"
}
}
, "_source": ["name"]
}
#排序与分页
#form为查询的开始,size为查询的个数,sort为排序 age年龄字段 order:desc 为倒序排序
GET /test/_search
{
"query": {
"match": {
"name": "王"
}
},
"sort": [
{
"age": {
"order": "desc"
}
}
],
"from": 0,
"size": 20
}
#布尔查询
#must相当于 and
GET /test/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "王"
}
},
{
"term": {
"age": 21
}
}
]
}
}
}
#should相当于 or
GET /test/_search
{
"query": {
"bool": {
"should": [
{
"term": {
"age": 22
}
},
{
"term": {
"age": 21
}
}
]
}
}
}
#must_not相当于 not
GET /test/_search
{
"query": {
"bool": {
"must_not": [
{
"term": {
"age": 21
}
}
]
}
}
}
#过滤与多条件查询
#gt大于,gte大于等于,lt小于,lte小于等于
GET /test/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "王"
}
}
],
"filter": [
{
"range": {
"age": {
"gte": 10,
"lte": 21
}
}
}
]
}
}
}
#keyword和text
#keyword不会被分词器拆分,text类型会
GET _analyze
{
"analyzer": "keyword",
"text": "老王"
}
GET _analyze
{
"analyzer": "standard",
"text": "老王"
}
#高亮
#fields:高亮的字段,pre_tags/post_tags 前后标签,优选使用字段内的高亮设置
GET /test/_search
{
"query": {
"match": {
"name": "王"
}
},
"highlight": {
"pre_tags": "<p class='key' style='color:red'>",
"post_tags": "</p>",
"fields": {
"name": {
"pre_tags": "<p class='key' style='color:blue'>",
"post_tags": "</p>"
}
}
}
}