全文检索查询:
1.match_all:搜索所有
2.match搜索(用得最多的)
GET /hello/_search _search固定写法
{
"query": { query固定写法
"match": { match表示全文检索查询
"FIELD": "TEXT" field字段:text内容
}
}
}例子:
GET /hello/_search { "query": { "match": { "title": "手机" } } }
会进行分词然后查询,查询出来的会根据计算的分数排序,相关度越高,分数越高越靠前
上面是根据分词出来的结果然后对多个分词进行或(or)查询
下面是对分词字段进行与(and)查询,表示要包含所有分词
GET /hello/_search
{
"query": {
"match": {
"title": {
"query": "小米手机","operator": "and"
}
}
}
}
3.multi_match:多字段查询
可以匹配多个字段,如下
GET /hello/_search
{
"query": {
"multi_match": {
"query": "小米手机",
"fields": ["title","subtitle"] 多个字段
}
}
}
4.词条匹配:不进行分词,去索引库查询。一般查询不分词的字段(除了text以外的类型都用term)
GET /hello/_search
{
"query": {
"term": {
"title": {"value": "小米手机"}
}
}
}看下面,没有查询出小米手机词条相关的数据,因为title进行分词了把小米手机分词为小米和手机存储索引,索引库没有小米手机这个词语,所以查询不到。
5.结果过滤:可以指定哪些字段的数据
GET /hello/_search
{
"_source": ["title","price"], 要返回的字段列表
"query": {
"match": {
"title": "小米手机"
}
}
}还可以通过下面派出某些字段或者包含某些字段
"excludes": "{field}", "includes": "{field}"这俩个选择一个写,有时候需要的字段太多,就可以用偶爱出某些字段方法过滤
GET /hello/_search
{
"_source":{"excludes": "{field}",
"includes": "{field}"
},
"query": {
"match": {
"title": "小米手机"
}
}
}
6.模糊查询
有时候输错了一点也能查询出来,比如百度查询appla就可以匹配出apple,下面使用es尝试以下
GET /hello/_search
{
"query": {
"fuzzy": {
"title":"appla"
}
}
}
7.范围查询
GET /hello/_search
{
"query": {
"range": { 表示范围查询
"price": { 字段
"gte": 10, 大于
"lte": 1000 小于
}
}
}
}
布尔查询
GET /hello/_search
{
"query": {
"bool": {
"must": [ 还有should,must_not
{"match": {
"title": "小米"
}},
{
"range": {
"price": {
"gte": 10,
"lte": 1000
}
}
}
]
}
}
}bool查询本身没有查询条件,他通过组合其他的查询条件来完成对应查询,must表示全都查询条件匹配,should表示匹配一部分查询条件,must_not表示查询条件都不匹配
布尔查询其他功能:过滤:
当bool查询以价格范围和搜索条件进行查询时,价格只是我们的过滤条件,我们不希望价格范围查询影响记录的得分排序,所以需要过滤掉价格不进行计算分数
GET /hello/_search
{
"query": {
"bool": {
"must": [
{"match": {
"title": "小米"
}}
],
"filter": { 希望过滤条件不影响得分就这样写。
"range": {
"price": {
"gte": 10,
"lte": 1000
}
}
}
}
}
}
排序和分页
GET /hello/_search
{
"query": {
"bool": {
"must": [
{"match": {
"title": "小米"
}}
],
"filter": {
"range": {
"price": {
"gte": 10,
"lte": 1000
}
}
}
}
}
, "sort": [ 排序 和query同级
{
"price": {
"order": "desc"
}
}
],
"from": 0, 分页开始条数0开始 和query同级
"size": 2 每页多少条
}