索引(indices)--------------------------------Databases 数据库
类型(type)-----------------------------Table 数据表
文档(Document)----------------Row 行
字段(Field)-------------------Columns 列
测试中文分词器
POST _analyze
{
"analyzer": "ik_max_word",
"text": "我是中国人"
}
//查找所有
GET _search
{
"query": {
"match_all": {}
}
}
//映射配置
PUT jixiu/_mapping/searchs
{
"properties": {
"title":{
"type": "text",
"analyzer": "ik_max_word"
},
"images":{
"type": "keyword",
"index": "false"
},
"price":{
"type": "float"
}
}
}
//创建索引
PUT jixiu
{"settings":{
"number_of_shards": 1,
"number_of_replicas": 0
}
}
//查看索引
GET jixiu
GET *
//删除索引
DELETE jixiu
//插入数据
POST /jixiu/searchs/
{
"title":"小米手机",
"images":"http://image.leyou.com/12479122.jpg",
"price":2699.00
}
//新增指定id
POST /jixiu/searchs/2
{
"title":"大米手机",
"images":"http://image.leyou.com/12479122.jpg",
"price":2899.00
}
//不添加映射,只能判断添加映射
POST /jixiu/searchs/3
{
"title":"超米手机",
"images":"http://image.leyou.com/12479122.jpg",
"price":2899.00,
"stock": 200,
"saleable":true
}
//修改数据
PUT /jixiu/searchs/3
{
"title":"超大米手机",
"images":"http://image.leyou.com/12479122.jpg",
"price":3899.00,
"stock": 100,
"saleable":true
}
//删除数据
DELETE /索引库名/类型名/id值
//基本查询
GET /索引库名/_search
{
"query":{
"查询类型":{
"查询条件":"查询条件值"
}
}
}
//or关系查询:match`类型查询,会把查询条件进行分词,然后进行查询,多个词条之间是or的关系
GET /jixiu/_search
{
"query":{
"match":{
"title":"小米电视"
}
}
}
//and关系某些情况下,我们需要更精确查找,我们希望这个关系变成`and`,可以这样做:
GET /jixiu/_search
{
"query":{
"match": {
"title": {
"query": "小米电视",
"operator": "and"
}
}
}
}
//最小匹配参数查询
GET /jixiu/_search
{
"query":{
"match":{
"title":{
"query":"小米曲面电视",
"minimum_should_match": "75%"
}
}
}
}
//多字段查询
GET /jixiu/_search
{
"query":{
"multi_match": {
"query": "小米",
"fields": [ "title", "subTitle" ]
}
}
}
//结果过滤
GET /jixiu/_search
{
"_source": ["title","price"],
"query": {
"term": {
"price": 2699
}
}
}
//布尔组合
bool`把各种其它查询通过`must`(与)、`must_not`(非)、`should`(或)的方式进行组合
GET /jixiu/_search
{
"query":{
"bool":{
"must": { "match": { "title": "大米" }},
"must_not": { "match": { "title": "电视" }},
"should": { "match": { "title": "手机" }}
}
}
}
//范围查询:gt 大于,gte 大于等于,lt 小于,lte 小于等于
GET /jixiu/_search
{
"query":{
"range": {
"price": {
"gte": 1000.0,
"lt": 2800.00
}
}
}
}
//模糊查询:fuzzy` 查询是 `term` 查询的模糊等价。它允许用户搜索词条与实际词条的拼写出现偏差,但是偏差的编辑距离不得超过2:
GET /jixiu/_search
{
"query": {
"fuzzy": {
"title": "appla"
}
}
}
//过滤所有的查询都会影响到文档的评分及排名。如果我们需要在查询结果中进行过滤,并且不希望过滤条件影响评分,那么就不要把过滤条件作为查询条件来用。而是使用`filter`方式:
GET /jixiu/_search
{
"query":{
"bool":{
"must":{ "match": { "title": "小米手机" }},
"filter":{
"range":{"price":{"gt":2000.00,"lt":3800.00}}
}
}
}
}
//排序sort` 可以让我们按照不同的字段进行排序,并且通过`order`指定排序的方式
GET /jixiu/_search
{
"query": {
"match": {
"title": "小米手机"
}
},
"sort": [
{
"price": {
"order": "desc"
}
}
]
}
//多字段排序
假定我们想要结合使用 price和 _score(得分) 进行查询,并且匹配的结果首先按照价格排序,然后按照相关性得分排序:
GET /searchs/_search
{
"query":{
"bool":{
"must":{ "match": { "title": "小米手机" }},
"filter":{
"range":{"price":{"gt":200000,"lt":300000}}
}
}
},
"sort": [
{ "price": { "order": "desc" }},
{ "_score": { "order": "desc" }}
]
}