Elasticsearch简单用法

基本命令

# 数据的顶层单位就是Index <=> 数据库
curl -XGET 'http://localhost:9200/_cat/indices?v'

# Index里面单条记录叫做文档Document,同一个Index当中的文档不要求有相同的结构,最好相同

# Type: Document分组,虚拟的逻辑分组

# 不同的Type应该有相似的schema

# 列出每个Index包含的Type
curl 'localhost:9200/_mapping?pretty=true'

新建和删除Index

curl -XPUT 'localhost:9200/weather'
curl -XDELETE 'localhost:9200/weather'

创建模板

curl -H "Content-Type: application/json" -XPUT 'localhost:9200/account' -d '
{
    "mappings": {
        "person": {
            "properties": {
                "user": {
                    "type": "text",
                    "analyzer": "ik_max_word"
                },
                "title": {
                    "type": "text",
                    "analyzer": "ik_max_word"
                },
                "desc": {
                    "type": "text",
                    "analyzer": "ik_max_word",
                    "search_analyzer": "ik_max_word"
                }
            }
        }
    }
}'

向指定的数据操作

  • 可以不指定Type
  • 可以不指定1
# 向指定的/Index/Type发送put请求
curl -H "Content-Type: application/json" -XPUT 'localhost:9200/account/person/1' -d '
{
    "user": "张三",
    "title": "工程师",
    "desc": "数据库guanli"
}' 

# 不指定id
curl -H "Content-Type: application/json" -XPOST 'localhost:9200/account/person' -d '
{
    "user": "李四",
    "title": "工程师",
    "desc": "数据库db"
}'

# 如果没有先创建Index,直接执行上面的命令会直接生成指定的Index

查看记录

# 查看某条记录
curl 'localhost:9200/accounts/person/1?pretty=true'

# 删除某条记录
curl -XDELETE 'localhost:9200/account/person/1'

# 更新数据
curl -H"Content-Type: application/json" -XPUT 'localhost:9200/account/person/1' -d '
{
    "user": "李四",
    "title": "工程师",
    "desc": "数据库管理员"
}'

数据查询

# 查询改 Index:account, Type:person 下的所有的值
curl 'localhost:9200/account/person/_search'

全文搜索

# 指定匹配条件是 desc 字段含有“软件”这个词
curl -H"Content-Type: application/json" 'localhost:9200/account/person/_search' -d '
{
    "query": {"match": {"desc": "软件"}}
}'

# 指定返回条目
curl -H"Content-Type: application/json" 'localhost:9200/account/person/_search' -d '
{
    "query": {"match": {"desc": "数据库管理"}},
    "size": 1
}'

# 类似sql limit 1, 1
curl -H "Content-Type: application/json" 'localhost:9200/account/person/_search' -d '
{
    "query": {"match": {"desc": "数据库管理"}},
    "from": 0,
    "size": 1
}'

# or 
curl -H "Content-Type: application/json" 'localhost:9200/account/person/_search' -d '
{
    "query": {"match": {"desc": "数据库 管理"}}
}'

# and
curl -H "Content-Type: application/json" 'localhost:9200/account/person/_search' -d '
{
    "query": {
        "bool":{
            "must": [
                {"match": {"desc": "数据库"}},
                {"match": {"desc": "管理"}}
            ]
        }
    }
}'

上面检索会自动分词,如果想硬匹配怎么办

{
	"query": {
    	"match_phrase": {
        	"content" : {
            	"query" : "我的宝马多少马力"
        	}
    	}
  	}
}

查看IK分词结果

curl -XGET "http://es-cn-v0h0qdjdl000acisw.elasticsearch.aliyuncs.com:9200/_analyze?pretty" -H 'Content-Type: application/json' -d'
{
  "analyzer": "ik_smart", 
  "text": "hello world, 万里长城永不倒,千里黄河水滔滔,君不见黄河之水天上来  "
}'

查看索引account下面type:person 的 mapping

curl -XGET -H "Content-Type: application/json" 'localhost:9200/accout/_mapping/person'

参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值