elasticsearch7.x基本搜索(搜索)

match分词搜索

全文搜索

  • match_all 搜索全部
GET xxx/_search
{
	"query":{
		"match_all":{}
	}
}

匹配单字段搜索

  • field 字段名
GET xxx/_search
{
	"query":{
		"match":{
			"field":"内容"
		}
	}
}

匹配单字段多词组搜索

  • filed 字段名,词组使用空格隔开

注意:数据应该保存成为一个数组

GET xxx/_search
{
	"query":{
		"match":{
			"field":"内容1 内容2 内容3"
		}
	}
}

匹配多字段搜索

  • must相当于MySQL条件中的 AND
  • should相当于MySQL条件中的 OR

must

GET xxx/_search
{
	"query":{
		"bool":{
			"must":[
			{
				"match":{
					"field1":"content"
				}
			},
			{
				"match":{
					"field2":"content"
				}
			}
			]
		}
	}
}

should

GET xxx/_search
{
	"query":{
		"bool":{
			"should":[
				{
					"match":{
						"field1":"content"
					}
				},
				{
					"match":{
						"field2":"content"
					}
				}
			]
		}
	}
}

匹配多字段搜索相同内容

  • multi_match 多字段
  • query 搜索内容
  • fields 搜索的字段
  • operator 字段的匹配方式 ,属性有OR, AND
GET xxx/_search
{
	"query":{
		"multi_match": {
	        "query": "content",
	        "fields": ["field_name1", "field_name2"],
	        "operator": "OR"
		}
    }
}

搜索分页

  • from 起始数
  • size 获得的数量
GET xxx/_search
{
	"query":{
		"match_all":{}
	},
	"from":0,
	"size":10
}

搜索过滤

  • filter 过滤
  • field 要过滤的字段名
  • gte 大于等于 附加 gt 大于
  • lte 小于等于 附加 lt 小于
GET xxx/_search
{
	"query":{
		"bool":{
			"should":[
				{
					"match_all":{}
				}
			],
			"filter":{
				"range":{
					"field":{
						"gte":22,
						"lte":23
					}
				}
			}
		}
	}
}

term精确搜索

精确搜索

  • term 精确查找(单个)
  • terms 精确查找(多个)

注意:这里的字段类型应保证为非分词的类型,如:keyword

term

{
	"query":{
		"term": {
	        "_id":1
		}
    }
}

terms

{
	"query":{
		"terms": {
	        "_id":[1,2,3]
		}
    }
}

term高亮搜索

高亮搜索

  • highlight 高亮查找
  • pre_tags 标签前缀
  • post_tags 标签后缀
  • fields 规定的字段,支持多个

注意:如果不声明前缀和后缀,那边默认使用 <em></em>

{
	"query":{
		"match":{
			"field":"content"
		}
	},
	"highlight":{
		"pre_tags":"<p class = \"text_high_light\">",
		"post_tags":"</p>",
		"fields":{
			"field":{}
		}
	}
}

知识源于学习

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值