【ElasticSearch】Part3 - 使用查询表达式搜索(query DSL)

在这里插入图片描述
不再使用拼接的query-string参数(GET query?xx=xx&xx=xx),而是一个json请求体替代。

查询

查询所有古诗

GET article/poems/_search
{
	"query": {
		"match_all": {}
	}
}

查询有"春"字的古诗

GET article/poems/_search
{
	"query":{
		"match":{
			"content":"春"
		}
	}
}

根据id倒序排序

GET article/poems/_search
{
	"query": {
		"match_all": {}
	},
	"sort": [
	  {
	    "_id": {
	      "order": "desc"
	    }
	  }
	]
}

根据诗的字数倒序排序

GET article/poems/_search
{
	"query": {
		"match_all": {}
	},
	"sort": [
	  {
	    "words": {
	      "order": "desc"
	    }
	  }
	]
}

分页

GET article/poems/_search
{
	"query": {
		"match_all": {}
	},
	"from": 0,
	"size": 2
}

指定要查那些字段

GET article/poems/_search
{
  "query": {
    "match_all": {}
  },
  "_source": ["title", "content"]
}

should和must

查找诗中有"春"和"雨"的,相当于and

GET article/poems/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "content": {
              "value": "春"
            }
          }
        },
        {
          "term": {
            "content": {
              "value": "雨"
            }
          }
        }
      ]
    }
  }
}

查找诗中有"春"或"雨"的,相当于or

GET article/poems/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "content": {
              "value": "春"
            }
          }
        },
        {
          "term": {
            "content": {
              "value": "雨"
            }
          }
        }
      ]
    }
  }
}

过滤条件(query filter)

查找含"春"字的诗并且字数在10-30之间的

GET article/poems/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "content": "春"
          }
        }
      ],
      "filter": {
        "range": {
          "words.keyword": {
            "gte": 10,
            "lte": 30
          }
        }
      }
    }
  }
}

全文检索(full-text search)

查找带诗里带"春"和"月"的

GET article/poems/_search
{
  "query": {
    "match": {
      "content": "春 月"
    }
  }
}

短语搜索(phrase search)

搜索content中包含"好雨"的诗

GET article/poems/_search
{
  "query": {
    "match_phrase": {
      "content": "好雨"
    }
  }
}

Q&A

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值