elasticsearch基本操作

1. 创建索引。

创建book索引

 put  192.168.0.132:9200/book

{
    "settings":{
        "number_of_shards":3,
        "number_of_replicas": 1

    },
    
    "mappings":{
        "novel":{
           "properties":{
                "title":{
                   "type":"text",
                    "analyzer": "ik_max_word",
                    "search_analyzer": "ik_max_word",
                    "store": false
                },
                "word_count":{
                   "type":"integer"
                },
                "author":{
                   "type": "text",
                                       "analyzer": "ik_max_word",
                    "search_analyzer": "ik_max_word",
                    "store": false
                },
                "publish_date":{
                  "type": "date",
                  "format": "yyyy-MM-dd HH:mm:ss || yyyy-MM-dd||epoch_millis"
                }
                
           
           }
          
        },
        "literature":{
        }
       
    }

}

number_of_shards 是分片数,

number_of_replicas是备份数。

novel  和  literature 是类型

删除book索引

DELETE    192.168.0.131:9200/book

2.插入数据

post  192.168.0.131:9200/book/novel

{

"author":"欧阳花瓶",

"title": "China",

"word_count":31,

"date":"1994-09-01"

}i

3 .修改索引数据

192.168.0.131:9200/book/novel/6/_update

{
    "doc":{
        "author": "赵六5"
    }
}

192.168.0.131:9200/索引/类型/id/_update

4.删除索引数据

DELTE     192 .168.0.131:9200/book/novel/6

5. 查询

根据ID查询索引

get 192.168.0.131:9200 /索引/类型/id

查询所有数据

GET          192.168.0.131:9200/book/_search

查询数据分页

GET          192.168.0.131:9200/book/_search

{
    "query":{
        "match_all":{}
    
    },
    "from":0,
    "size": 10
}

match 查询

GET          192.168.0.131:9200/book/_search

{
    "query":{
        "match":{
        "author": "张三"
        }
    }
}

排序

GET          192.168.0.131:9200/book/_search

{
    "query":{
        "match":{
        "author": "张三"
        }
    
    },"sort":[
       {"date":{"order":"desc"}} 
    ]
}

聚合查询

GET   192.168.0.131:9200/book/_search

{
   "aggs":{
    "group_by_word_count": {
        "terms":{ "field":"word_count" }

    }

   }
}

GET   192.168.0.131:9200/book/_search

{
   "aggs":{
    "group_by_word_count": {
        "stats":{ "field":"word_count" }

    }

   }
}

短语匹配(Phrase Matching)

GET  192.168.0.131:9200/book/_search

{
    "query":{
        "match_phrase":{
        "author": "玛丽"
        }
    }
}

multi_match 查询

GET  192.168.0.131:9200/book/_search

{
    "query":{
        "multi_match":{
          "query":"玛丽",
            "fields":["author","title"]

        }
    
    }
}

query_string  查询

GET  192.168.0.131:9200/book/_search

{
    "query":{
        "query_string":{
          "query":"西游记 or 水浒传"      
        }
    
    }
}

GET  192.168.0.131:9200/book/_search

{
    "query":{
        "query_string":{
          "query":"玛丽 or 水浒传"      ,
          "fields":["author","title"]
        }
    
    }
}

term 查询

GET  192.168.0.131:9200/book/_search

{
    "query":{
        "term":{
          "title":"水浒传"
        }
    
    }
}

range 查询

GET  192.168.0.131:9200/book/_search

{
    "query":{
        "range":{
          "date":{

             "gte":"1994-09-01"

          }
        }
    
    }
}

query filter 查询

GET  192.168.0.131:9200/book/_search

{
    "query":{
        "bool":{
          "filter":{
            "term":{"word_count":51}

          }
        }
    
    }
}

固定分数查询

GET  192.168.0.131:9200/book/_search

{
    "query":{
        
    "constant_score":{
        "filter":{
            "match":{
                    "title":"西游记"

            }
        }
    }
    }
    
}

bool查询

should

GET  192.168.0.131:9200/book/_search

{
    "query":{
    "bool":{
        "should":[{
            "match":{
                    "title":"西游记"
            }
            
        },
        {
            "match":{
                   "author": "超级玛丽"
            }
        }
        ]
    }
    }
}

must

GET  192.168.0.131:9200/book/_search

{
    "query":{
    "bool":{
        "must":[{
            "match":{
                    "title":"西游记"
            }
            
        },
        {
            "match":{
                   "author": "超级玛丽"
            }
        }
        ]
    }
    }
}

must_not

GET  192.168.0.131:9200/book/_search

{
    "query":{
    "bool":{
        "must_not":{
                "match":{
                     "title": "西游记"
                }
        }
    }
    }
}

嵌套查询

{

    "settings":{

        "number_of_shards":3,

        "number_of_replicas": 1

    },

    

    "mappings": {

        "earthblog": {

            "properties": {

                "orderId": {

                    "type": "keyword"

                },

                "totalAmount": {

                    "type": "long"

                },

                "discountAmount":{

                    "type": "long"

                },

                "status":{

                   "type": "integer"

                },

                "createTime":{

                  "type": "date",

                  "format": "yyyy-MM-dd HH:mm:ss || yyyy-MM-dd||epoch_millis"

                },

                "orderDetails": {

                    "type": "nested",

                    "properties": {

                        "productId": {

                            "type": "long"

                        },

                        "productName": {

                            "analyzer": "ik_max_word",

                            "type": "text"

                        },

    

                        "productType": {

                            "type": "short"

                        },

                        "status": {

                            "type": "short"

                        },

                        "createTime":{

                          "type": "date",

                          "format": "yyyy-MM-dd HH:mm:ss || yyyy-MM-dd||epoch_millis"

                        }

                    }

                }

            }

        }

    }

}

嵌套查询

192.168.0.132:9200/order/order/_search

{

    "query": {

     "bool": {

      "must": [

        {

                    "nested": {

                        "path": "orderDetails",

                        "query": {

                            "bool": {

                                "must": [{

                                        "match": {

                                            "orderDetails.productName": "AOC显示器"

                                        }

                                    },

                                    {

                                        "match": {

                                            "orderDetails.productType": 1

                                        }

                                    }

                                ]

                            }

                        }

                    }

                }

      ]

    }

    }

}

二、Kibana基本操作

1、点击这个小扳手是命令界面

创建索引

put  index_icoding  (创建一个index_icoding的索引)

 

查看索引信息,get index_icoding

删除索引

delete index_icoding

关闭索引 

post index_icoding/_close

打开索引

post index_icoding/_open

 操作映射

添加映射
put index_icoding/_mapping/class
{

"properties":{
"name":{
"type": "text"

},
"grade":{
"type": "integer"
 }
}

}

 

查询映射

 添加字段

put index_icoding/_mapping/class
{

"properties":{
"address":{
"type": "text"

}
}

}

操作文档

插入文档

 post index_icoding/class
{
  "name":"docter",
  "grade":19
}

查询文档

get /index_icoding/class/_search

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Elasticsearch是一个开源的分布式搜索和分析引擎,它可以用于存储、搜索和分析大量的数据。以下是一些Elasticsearch基本操作: 1. 安装和启动Elasticsearch:首先,你需要从Elasticsearch官方网站下载和安装Elasticsearch。安装完成后,你可以使用命令行或者图形界面来启动Elasticsearch。 2. 创建索引:在Elasticsearch中,数据存储在索引中。你可以使用PUT请求来创建一个新的索引。例如,使用curl命令可以发送以下请求来创建一个名为"my_index"的索引: ``` curl -XPUT 'localhost:9200/my_index' ``` 3. 添加文档:一旦索引创建好了,你可以使用POST请求来向索引中添加文档。文档是以JSON格式表示的数据。以下是向名为"my_index"的索引添加一个文档的示例请求: ``` curl -XPOST 'localhost:9200/my_index/_doc' -d ' { "title": "Elasticsearch Basics", "content": "This is a basic introduction to Elasticsearch" }' ``` 4. 搜索文档:你可以使用GET请求来搜索索引中的文档。以下是一个搜索名为"my_index"的索引中包含关键字"elasticsearch"的文档的示例请求: ``` curl -XGET 'localhost:9200/my_index/_search?q=elasticsearch' ``` 5. 更新文档:使用POST请求可以更新索引中的文档。以下是更新名为"my_index"的索引中ID为1的文档的示例请求: ``` curl -XPOST 'localhost:9200/my_index/_doc/1/_update' -d ' { "doc": { "content": "This is an updated content" } }' ``` 6. 删除文档:使用DELETE请求可以删除索引中的文档。以下是删除名为"my_index"的索引中ID为1的文档的示例请求: ``` curl -XDELETE 'localhost:9200/my_index/_doc/1' ``` 这些是Elasticsearch的一些基本操作。你可以根据需要进一步探索和学习更多高级功能和API。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值