4.4.查询.过滤.排序.分页

1.查询

1.1.单字段查询

GET /索引库名/_search
{
  "query": {"match": {
    "FIELD": "TEXT"
  }}
}

需要指定字段名FIELD和要查询的内容TEXT

demo1 查询title字段为“大米手机”的数据

GET /heima/_search
{
  "query": {
      "match": {
      "title": "大米手机"
  	}
  }
}

响应:

{
  "took": 372,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0.74487394,
    "hits": [
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "2",
        "_score": 0.74487394,
        "_source": {
          "title": "2号超大米手机",
          "images": "http://image.leyou.com",
          "price": 2899,
          "stock": 200,
          "saleable": true,
          "subTitle": "呵呵"
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "1",
        "_score": 0.5753642,
        "_source": {
          "title": "大米手机",
          "images": "http://image.leyou.com/123.jpg",
          "price": 2899
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "jNO--HEBf78mIy0fcrZT",
        "_score": 0.22108285,
        "_source": {
          "title": "小米手机",
          "images": "http://image.leyou.com/123.jpg",
          "price": 2699
        }
      }
    ]
  }
}

因为查询条件,和索引库中的数据都做了分词,所以与“大米手机”有关的数据都查询出来了,这种查询,词条之间的关系为or

在查询时词条之间采用and的方式,则使用如下的方式进行查询

GET /heima/_search
{
  "query": {
    "match": {
      "title": {
          "query":"大米手机",
          "operator": "and"
      }
    }
  }
}

返回结果:

{
  "took": 70,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.74487394,
    "hits": [
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "2",
        "_score": 0.74487394,
        "_source": {
          "title": "2号超大米手机",
          "images": "http://image.leyou.com",
          "price": 2899,
          "stock": 200,
          "saleable": true,
          "subTitle": "呵呵"
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "1",
        "_score": 0.5753642,
        "_source": {
          "title": "大米手机",
          "images": "http://image.leyou.com/123.jpg",
          "price": 2899
        }
      }
    ]
  }
}

1.2.查询所有

GET /heima/_search
{
  "query": {
    "match_all": {}
    }
}
  • query代表查询对象
  • match_all代表查询所有

返回结果:

{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "jNO--HEBf78mIy0fcrZT",
        "_score": 1,
        "_source": {
          "title": "小米手机",
          "images": "http://image.leyou.com/123.jpg",
          "price": 2699
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "2",
        "_score": 1,
        "_source": {
          "title": "2号超大米手机",
          "images": "http://image.leyou.com",
          "price": 2899,
          "stock": 200,
          "saleable": true,
          "subTitle": "呵呵"
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "1",
        "_score": 1,
        "_source": {
          "title": "大米手机",
          "images": "http://image.leyou.com/123.jpg",
          "price": 2899
        }
      }
    ]
  }
}
  • took:查询花费时间,单位是毫秒
  • time_out:是否超时
  • _shards:分片信息
  • hits:搜索结果总览对象
    • total:搜索到的总条数
    • max_score:所有结果中文档得分的最高分
    • hits:搜索结果的文档对象数组,每个元素是一条搜索到的文档信息
      • _index:索引库
      • _type:文档类型
      • _id:文档id
      • _score:文档得分
      • _source:文档的源数据

1.3.多字段搜索

GET /heima/_search
{
  "query": {
    "multi_match": {
      "query": "小米",
      "fields": ["title","subTitle"]
    }
  }
}

1.4.词条搜索

GET /heima/_search
{
  "query": {
    "term": {
      "title": {
        "value": "大米手机"
      }
    }
  }
}

将要查询的内容视为一个词条,不再对查询内容进行分词

查询结果:

{
  "took": 13,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

因此查询结果为0个,因为没有这个词条

词条查询适用于不分词的字段,除了text类型的字段外,其他类型的字段均不能分词

2.结果过滤

2.1.要求单字段

设置_source:“title”,表示结果中只要求title字段

查询请求:

GET /heima/_search
{
  "_source": "title", 
  "query": {
    "match": {
      "title":"大米手机"
       
    }
  }
}

查询结果:

{
  "took": 95,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0.74487394,
    "hits": [
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "2",
        "_score": 0.74487394,
        "_source": {
          "title": "2号超大米手机"
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "1",
        "_score": 0.5753642,
        "_source": {
          "title": "大米手机"
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "jNO--HEBf78mIy0fcrZT",
        "_score": 0.22108285,
        "_source": {
          "title": "小米手机"
        }
      }
    ]
  }
}

可以看到_source中只包含title字段,这样做能提高搜索效率

2.2.多字段

如果保留多个字段

查询请求:

GET /heima/_search
{
  "_source": ["title","price"], 
  "query": {
    "match": {
      "title":"大米手机"
    }
  }
}

查询结果:

{
  "took": 15,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0.74487394,
    "hits": [
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "2",
        "_score": 0.74487394,
        "_source": {
          "price": 2899,
          "title": "2号超大米手机"
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "1",
        "_score": 0.5753642,
        "_source": {
          "price": 2899,
          "title": "大米手机"
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "jNO--HEBf78mIy0fcrZT",
        "_score": 0.22108285,
        "_source": {
          "price": 2699,
          "title": "小米手机"
        }
      }
    ]
  }
}

2.3排除字段

如果排除多个字段

查询请求:

GET /heima/_search
{
  "_source": {
    "excludes": "images"
  },
  "query": {
    "match": {
      "title":"大米手机"
       
    }
  }
}

查询结果:

{
  "took": 12,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0.74487394,
    "hits": [
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "2",
        "_score": 0.74487394,
        "_source": {
          "subTitle": "呵呵",
          "price": 2899,
          "saleable": true,
          "title": "2号超大米手机",
          "stock": 200
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "1",
        "_score": 0.5753642,
        "_source": {
          "price": 2899,
          "title": "大米手机"
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "jNO--HEBf78mIy0fcrZT",
        "_score": 0.22108285,
        "_source": {
          "price": 2699,
          "title": "小米手机"
        }
      }
    ]
  }
}

3.高级搜索

3.1.模糊查询

先增加一条数据

POST /heima/_doc/4
{
  "title":"apple",
  "images":"http://image.leyou.com",
  "price":6899.00
}

查询请求:

GET /heima/_search
{
  "query": {
    "fuzzy": {
      "title": "appla"
    }
  }
}

查询结果:

{
  "took": 434,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1.0542042,
    "hits": [
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "4",
        "_score": 1.0542042,
        "_source": {
          "title": "apple",
          "images": "http://image.leyou.com",
          "price": 6899
        }
      }
    ]
  }
}

3.2.范围查询

查询价格大于1000,小于3000的数据

查询请求:

GET /heima/_search
{
  "query": {
    "range": {
      "price": {
        "gte": 1000,
        "lte": 3000
      }
    }
  }
}

查询结果:

{
  "took": 78,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "jNO--HEBf78mIy0fcrZT",
        "_score": 1,
        "_source": {
          "title": "小米手机",
          "images": "http://image.leyou.com/123.jpg",
          "price": 2699
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "2",
        "_score": 1,
        "_source": {
          "title": "2号超大米手机",
          "images": "http://image.leyou.com",
          "price": 2899,
          "stock": 200,
          "saleable": true,
          "subTitle": "呵呵"
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "1",
        "_score": 1,
        "_source": {
          "title": "大米手机",
          "images": "http://image.leyou.com/123.jpg",
          "price": 2899
        }
      }
    ]
  }
}

3.3.布尔查询

查询请求:

GET /heima/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "title": "apple"
        }},
        {
          "range": {
            "price": {
              "gte": 3000
            }
          }
        }
      ]
    }
  }
}

查询title既能匹配apple并且price大于3000的数据

  • must:条件必须同时成立
  • should:至少有一个条件成立
  • must_not:条件必须同时都不成立

must的条件会影响的分,有range条件和没有range条件时,查询结果的得分不同

查询结果:

{
  "took": 320,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1.7222843,
    "hits": [
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "2",
        "_score": 1.7222843,
        "_source": {
          "title": "2号超大米手机",
          "images": "http://image.leyou.com",
          "price": 2899,
          "stock": 200,
          "saleable": true,
          "subTitle": "呵呵"
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "1",
        "_score": 1.287682,
        "_source": {
          "title": "大米手机",
          "images": "http://image.leyou.com/123.jpg",
          "price": 2899
        }
      }
    ]
  }
}

如果不想must条件影响得分可以使用filter

GET /heima/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "title": "大米"
        }}
      ],
      "filter": [
        {"range": {
            "price": {
              "gte": 1000
            }
          }}
      ]
    }
  }

特别说明:

过滤的条件不要放到搜索的条件里面去,过滤条件是检索出结果之后,在进行过滤条件(filter)

4.排序

根据price字段倒序排序

GET /heima/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "title": "大米"
        }}
      ],
      "filter": [
        {"range": {
            "price": {
              "gte": 1000
            }
          }}
      ]
    }
  },
  "sort": [
    {
      "price": {
        "order": "desc"  //降序
      }
    }
  ]
}

5.分页

GET /heima/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "title": "大米"
        }}
      ],
      "filter": [
        {"range": {
            "price": {
              "gte": 1000
            }
          }}
      ]
    }
  },
  "sort": [
    {
      "price": {
        "order": "desc"
      }
    }
  ],
  "from": 0,
  "size": 2
}

说明:

  • from:起始页
  • size:页大小
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值