# 查看所有索引
curl 127.0.0.1:9200/_cat/indices
# 创建索引
curl -XPUT 139.224.100.27:9200/test
# 删除索引
curl -X DELETE 127.0.0.1:9200/help_document
# 创建映射
curl -XPOST 139.224.100.27:9200/test/_mapping -H 'Content-Type:application/json' -d'
{
"properties": {
"content": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
}
}
}'
# 查看映射
curl 139.224.100.27:9200/索引名
# 插入数据
curl -XPOST 139.224.100.27:9200/test/_doc/2 -H 'Content-Type:application/json' -d'
{"content":"公安部:各地校车将享最高路权"}
'
curl -XPOST 139.224.100.27:9200/test/_doc/3 -H 'Content-Type:application/json' -d'
{"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}a
'
# 查询
curl -X GET 139.224.100.27:9200/help_document/article/_search -H 'Content-Type:application/json' -d'
{
"query" : {
"match" : {
"content" : "公安"
}
}
}'
分词
elasticsearch-analysis-pinyin
# 查看分词 ik_smart
139.224.100.27:9200/_analyze
{
"analyzer":"ik_max_word",
"text":"我是中国人"
}
# 配置自己的分词字
修改ik包里的config的xml
自动补全
# 创建索引
curl -XPUT 139.224.100.27:9200/completions
# 创mapping
curl -X PUT 139.224.100.27:9200/completion/_mapping/_doc -H 'Content-Type: application/json' -d'
{
"_doc": {
"properties": {
"suggest": {
"type": "completion",
}
}
}
}
'
# 插入数据
curl -XPOST 139.224.100.27:9200/completions/_doc/2 -H 'Content-Type:application/json' -d'
{"suggest":"cci识别"}
'
# 查询自动补全
curl 139.224.100.27:9200/completions/_doc/_search -H 'Content-Type:application/json' -d '
{
"suggest":{
"title-suggest":{
"prefix":"文字",
"completion":{
"field":"suggest"
}
}
}
}
'
拼写纠错
curl 127.0.0.1:9200/articles/article/_search?pretty -d '
{
"_source": false,
"suggest": {
"text": "phtyon web", # 输入的内容
"word-phrase": { # 自定义字段名, 推荐结果会包含在该字段中
"phrase": { # 返回短语形式, 还可以使用term
"field": "_all", # 指定在哪些字段中获取推荐词
"size": 1 # 返回的推荐词数量
}
}
}
}'
配置
# max number of threads [2048] for user [es] is too low, increase to at least [4096]
新建(为方便删除)
/etc/security/limits.d/test-limits.conf
添加
* soft nofile 655360
* hard nofile 655360
* soft nproc 4096
* hard nproc 4096
# [1]: max file descriptors [65535] for elasticsearch process is too low, increase to at least [65536]
设置/etc/security/limits.conf
* soft nproc 5000
* hard nproc 5000
root soft nproc 5000
root hard nproc 5000
# [3]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
设置/etc/sysctl.conf
vm.max_map_count=655360
# [1]: the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured
在elasticsearch的config目录下,修改elasticsearch.yml配置文件,将下面的配置加入到该配置文件中:
cluster.initial_master_nodes: ["node-1"] #这里的node-1为node-name配置的值
# 配置跨域
http.cors.enabled:true
http.cors.allow-origin:"*"
CURD
PUT localhost:9200/索引名称/类型名称/文档id # 创建文档(指定文档id)
POST localhost:9200/索引名称/类型名称 # 创建文档(随机文档id)
POST localhost:9200/索引名称/类型名称/文档id/_update # 修改文档
PUT localhost:9200/索引名称/类型名称/文档id # 修改文档,会覆盖原来,少字段(曾经)
DELETE localhost:9200/索引名称/类型名称/文档id # 删除文档
GET localhost:9200/索引名称/类型名称/文档id # 删除文档
POST localhost:9200/索引名称/类型名称/_search # 查询所有数据
# 高级
1.指定字段
GET index/type/_search
{
"query":{
"match":"xxx"
},
"_source":["name","age"]
}
2.排序
{
"query":{},
"sort":[
"age":{
"order":"asc"
}
],
"from":0,
"size":1
}
3.bool (must,should,must_not)
{
"query":{
"bool":{
"must":[
{
"match":{
"name":"xxx"
}
},
{
"match":{
"age":123
}
}
]
}
}
}
4.过滤
{
"query":{
"bool":{
"must":[
{
"match":{
"name":"xx"
}
}
],
"filter":{
"range":{
"age":{
"lt":10
}
}
}
}
}
}
5.多条件查询
# 多条件空格隔开
{
"query":{
"match":{
"tags":"男 技术"
}
}
}
6.精确查询多个值
{
"query":{
"bool":{
"should":[
{
"term":{
"t1":"22"
}
},
{
"term":{
"t2":"33"
}
}
]
}
}
}
7.高亮
{
"query":{
"match":{
"content":"公安"
}
},
"highlight":{
"pre_tags":"<p class='key' style='color:red'>",
"post_tags":"</p>",
"fields":{
"content":{}
}
}
}
GET help_document/_doc/_search
{
"query":{
"bool":{
"should":[
{
"match":{
"content":"gdf"
}
},
{
"match":{
"brief":"识别"
}
},
{
"match":{
"title":"aaaa"
}
}
],
"must":[{
"term":{
"status":1
}}
]
}
},
"highlight":{
"pre_tags":"<p class='key' style='color:red'>",
"post_tags":"</p>",
"fields":{
"brief":{},
"title":{}
}
}
}
"minimum_should_match": 1
alias swagger-ui='echo "http://localhost:8000" && docker run --rm --name swagger-ui -p 8000:8080 -e SWAGGER_JSON=/app/openapi.yaml -v $PWD:/app swaggerapi/swagger-ui'
{
"query":{
"match":{
"title":"识别"
}
},
"highlight":{
"pre_tags":"<span class='key'>",
"post_tags":"</span>",
"fields":{
"title":{}
}
},
"_source":["title"]
}