es操作。

elasticsearch

一、索引

1、新增

# PUT http://localhost:9200/indexName
# 可以增加 requestBody 
{
  "mappings":{
    "properties":{
      "city":{
        "type":"keyword"
      },
      "name":{
        "type":"text",
        "analyzer":"ik_smart"
      },
      "price":{
        "type":"float"
      }
    }
  },
  "settings":{
  	"number_of_shards": "1", //主分片
  	number_of_replicas": "1" //副本数
  }
}

2、删除

DELETE http://localhost:9200/indexName

3、修改

POST http://localhost:9200/indexName/_mappings // 修改映射关系,注意已经存在的字段映射不可以修改

{
    properties:{
        "fieldName": {
         	"type": "text" 或者 "keyword",   // keyword 不分词  ,
            "analyzer": "standard" ,// 分词器,建立倒排索引
            "index":true 或者 false, // 是否可以被检索
            "search_analyzer":"ik_smart" // 检索这分词器
        }
    }
}

4、查询

GET http://localhost:9200/indexName

GET http://localhost:9200/_cat/indices?v // 查询所有

二、文档

1、新增

### 以下几种写法都满足

// POST http://localhost:9200/indexName/_doc
{
    "city":"北京",
    "name":"bmw",
    "price":450000
}


//PUT http://localhost:9200/indexName/_doc/1101
{
    "city":"北京",
    "name":"bmw",
    "price":450000
}

//PUT|POST http://localhost:9200/indexName/_create/1101
{
    "city":"北京",
    "name":"bmw",
    "price":450000
}


//PUT|POST http://localhost:9200/indexName/_create/1101
{	
    "doc":{
     	"city":"北京",
        "name":"bmw",
        "price":450000   
    }
    
}

2、删除

DELETE localhost:9200/indexName/_doc/6

3、修改

  • 全量修改
#等同于新增,只是存在时候会先删除
//PUT http://localhost:9200/indexName/_doc/1101
{
    "city":"北京",
    "name":"bmw",
    "price":450000
}
  • 局部修改
//post /hotel/_update/8
{ 
  "doc":{
    "city": "北京上海",
    "name": "北京自如"  
  }
  
}

4、查询

  • 全文检索查询
//GET localhost:9200/indexName/_search
{
    "query":{
        "match_all":{  // match_all 查询所有文档,请求体不需要给匹配条件
            
        }
    }
}

{
    "query":{
        // match 倒排索引查询 ,
        // match_phrase
        "match":{  
            "FIELD": "TEXT"
        }
    }
}

GET /car/_search
{
  "query":{
    "term": { term 精确查询,主要是keyword|Date|boolean|long
      "FIELD": {
        "value": "VALUE"
      }
    }
  }
}

{
  "query":{
    "multi_match": {
      "query": "北京",
      "fields": []
    }
  }
}

{
  "query":{
    "range": { // 范围查询
      "FIELD": {
        "gte": 10,
        "lte": 20
      }
    }
  }
}

{
  "query":{
    "geo_distance": { // 坐标查询
      "distance": 100,
      "FIELD": {
        "lat": 40.73,
        "lon": -74.1
      }
    }
  }
}
  • 分页排序
{
    "query":{},
    "from":0,
    "size":10,
    "sort":{
        "fieldName":{
            "order":"asc"
        }
    }
}

{
    "query":{},
    "from":0,
    "size":10,
    "sort":{
        "fieldName":"asc"
    }
}

5、高亮

{
    "highlight":{
        "fields":{
            "fieldName":{
                "pre_tags":"<em>",
                "post_tags":"</em>"
            }
        }
    }
}

6、条件查询

bool => must、should、must_not、filter **注意:**must、should 参与算分,must_not、filter不参与算分

{
    "query":{
        "bool":{
            "must":{
                match:{}
            },
            "should":{
                "term":{}
            },
            "filter":{
                "geo_distance":{}
            }
        }
    }
}

7、修改算分查询

GET /hotel/_search
{
  "query":{
    "function_score": {
      "query": {"match": {
        "city": "北京"
      }},
      "functions": [
        {
          "filter": {"term": {
            "city": "北京"
          }},
          "weight": 10
        }
      ],
      "boost_mode": "multiply"
    }
  }
}

三、聚合

平均值等

GET /hotel/_search
# 分组
{ 
  "size":0,  //不显示sourse字段信息
  "aggs":{
    "group_price":{
      "terms": {
        "field": "price",
        "size": 1
      }
    }
  }
}


{ 
  "size":0,  //不显示sourse字段
  "aggs":{
    "group_price":{
      "avg": {  // avg ,min,max,sum    stats==>获取所有
        "field": "price"
      }
    }
  }
}

四、自动补全

# 添加索引、指定字段sug为completion类型
PUT /sug
{
  "mappings":{
    "properties":{
      "sug":{
       "type":"completion" 
      }
    }
  }
}



POST /sug/_doc/1
{
  "sug":["你好","张","李"]
}

POST /sug/_doc/2
{
  "sug":["李晓明","张三","李"]
}


GET /sug/_search
{
  "suggest":{
    "my_sug":{ //自定义名字
      "text":"李", // 被匹配的数字
      "completion":
      {
        "field":"sug",
         "skip_duplicates":true
      }
     
    }
  }
}

五、分词器

1、概念

# localhost:9200/_analyze  GET
# 分词器 
// ik_smart、 ik_max_word 、standard 、 english 、py
{
	"analyzer":"standard",
	"text":"你好"
}

{
    "tokens": [
        {
            "token": "你",
            "start_offset": 0,
            "end_offset": 1,
            "type": "<IDEOGRAPHIC>",
            "position": 0
        },
        {
            "token": "好",
            "start_offset": 1,
            "end_offset": 2,
            "type": "<IDEOGRAPHIC>",
            "position": 1
        }
    ]
}

分词器分为三个部分

  • character filters 分词前对文本进行处理,比如:删除字符,替换字符
  • tokenizer 文本分词,IK分词器等
  • tokenizer filter 将tokenizer 输出的词进一步处理,比如拼音转换、大小写、同义词处理

2、自定义分词器

{
    "settings":{
        "analysis":{
            "analyzer":{
                "my_custom_ananlyzer":{
                    "tokenizer":"ik_smart",
                    "filter":"pinyin" //安装插件即可
                }
            }
        }
    }
}

六、总结

1、copy_tp

可以将其他字段属性copy到另一个字段上,这样的复合数据可以一起用来查询,避免两个字段查询影响性能。

eg:

PUT /phone
{
  "mappings":{
    "properties":{
      "name":{
        "type":"keyword",
        "index":true,
        "copy_to":"all"
      },
      "price":{
        "type":"long",
        "index":true
      },
      "addr":{
        "type":"text",
        "analyzer":"ik_max_word",
        "search_analyzer":"ik_smart",
        "copy_to":"all"
      },
      "all":{
        "type":"text",
        "analyzer":"ik_max_word",
        "search_analyzer":"ik_max_word"
      }
    }
  }
}

2、分词器

分为倒排索引以及查询分词器。

"all":{
    "type":"text",
    "analyzer":"ik_max_word", // 倒排索引
    "search_analyzer":"ik_max_word" // 查询分词器,但是term查询会失去作用
}
  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值