# 1 ik基本分词器
GET _analyze
{
"analyzer": "ik_smart",
"text": "中国人民我爱你"
}
GET _analyze
{
"analyzer": "ik_max_word",
"text": "中国人民我爱你"
}
# 2 简单查询title字段,并设置起始页和总共页数
# article代表索引名称
GET article/_search
{
"from": 0,
"size": 100,
"query": {
"match": {
"title": "中国"
}
}
}
# 3 bool查询、filter过滤器和排序
# must类似and(参与排序计算),should类似于or,must_not类似于no,filter过滤器(不参与排序分数计算)
# match模糊匹配,multi_match多字段模糊匹配,match_phrase短语匹配,term精确匹配(type是keyword或text),range排序
GET article/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "中国"
}
}
],
"should": [
{
"match": {
"content": "民族"
}
}
],
"must_not": [
{
"match": {
"title": "绿色"
}
}
],
"filter": [
{
"range": {
"date": {
"gte": "2016-01-02",
"lte": "2020-01-02"
}
}
}
]
}
}
}
# 4 日期排序
# 日期格式必须是“yyyy-MM-dd HH:mm:ss”或“yyyy-MM-dd”,字段的type是date,format是yyyy-MM-dd,如果格式错误搜索出的数据有问题
GET _search
{
"from": 0,
"size": 100,
"query": {
"bool": {
"must": [
{
"match": {
"content": "中国"
}
}
]
}
},
"sort": [
{
"date": {
"order": "desc"
}
}
]
}
# 5 多字段匹配
GET article/_search
{
"query": {
"multi_match": {
"query": "中国",
"fields": ["content", "title"]
}
}
}
# 6 短语匹配,匹配短语“绿色增长”
GET article/_search
{
"query": {
"match_phrase": {
"title":{
"query": "绿色增长",
"slop": 5
}
}
}
}
# 7 搜索推荐
# (1) 使用term
GET _search
{
"suggest": {
"my_suggest": {
"text": "中国人民",
"term": {
"field": "content"
}
}
}
}
# (2) 使用completion
# 注意,此处的title字段必须是"completion"类型
GET _search
{
"suggest": {
"my_suggest": {
"prefix":"中国",
"completion":{
"field": "title"
}
}
}
}
# 8 纠错查询,fuzziness表示纠错次数
GET article/_search
{
"query": {
"match": {
"title": {
"query": "中国人闵",
"fuzziness": 2
}
}
}
}
# 9 前缀查询
GET article/_search
{
"query": {
"prefix": {
"title": {
"value": "中国"
}
}
}
}