庞玉栋个人博客、人生苦短-我用Python

ES 增删改查操作

发布时间:6年前热度: 1330 ℃评论数:


创建

自定义ID增加

PUT /{index}/{type}/{id}
{
  "field": "value",
  ...
}

自动增加ID

使用post方式

POST /website/blog/
{
  "title": "My second blog entry",
  "text":  "Still trying this out...",
  "date":  "2014/01/01"
}

更新

PUT /website/blog/123
{
  "title": "My first blog entry",
  "text":  "Just trying this out...",
  "date":  "2014/01/01"
}

我们能看到 Elasticsearch 已经增加了 _version 字段值,created 标志设置成 false ,是因为相同的索引、类型和 ID 的文档已经存在。

删除

DELETE /website/blog/123

如果找到该文档,Elasticsearch 将要返回一个 200 ok 的 HTTP 响应码

查询

search查询

  • http://ip/_search 在所有索引中搜索全部类型 
    http://ip/索引/_search 在指定索引中搜索全部类型
    http://ip/索引,索引/_search 搜索两个索引的全部文档
    http://ip/索引*,索引*/_search 搜索以指定字符串开头的两个索引中的全部文档
    http://ip/索引/索引类型/_search 搜索指定索引中指定索引类型的全部文档
    http://ip/索引,索引/索引类型,索引类型/_search 搜索两个索引中,指定的两个索引类型的文档
    http://ip/_all/索引类型,索引类型/_search 在所有索引中搜索两个指定的索引类型文档
    http://ip/索引/索引类型/_search?q=name:立信&sort=id:desc 在指定索引中搜索指定数据,并按照id倒序排列
    took:耗时毫秒,time_out:是否超时,hits.total:查询条数,hits.max_score:匹配分数

dsl查询

  • 查询所有文档,并按照id倒序排列
GET /ecommerce/product/_search
{
  "query": { "match_all": {} },
  "sort": [
        { "id": "desc" }
    ],
  "from": 0,
  "size": 3
}
query:包装查询条件
sort:包装排序条件
form:第几页
size:每页几条
  • 根据名称查询,并按照id排序
GET /ecommerce/product/_search
{
  "query":{"macth":{"name":"测试"}},
  "sort":[{"id":"desc"}]
}
  • 返回结果的指定列
GET /ecommerce/product/_search
{
  "query":{"match_all":{}},
  "_source":["title","time"]
}

filter

  • 搜索指定名称的数据,而且id大于指定值
GET /ecommerce/product/_search
{
    "query" : {
        "bool" : {
            "must" : {
                "match" : {
                    "title" : "立信" 
                }
            },
            "filter" : {
                "range" : {
                    "id" : { "gt" : 10000 } 
                }
            }
        }
    }
}

全文检索

全文检索会将输入的搜索串拆解开来,去倒排索引里面去一一匹配,只要能匹配上任意一个拆解后的单词,就可以作为结果返回。

  • 根据一段文字进行查找
GET /ecommerce/product/_search
{
    "query" : {
        "match" : {
            "title" : "yagao producer"
        }
    }
}

精确查找

输入的搜索串,必须在指定的字段文本中,完全包含一模一样的,才可以算匹配,作为结果返回。

{
    "query" : {
        "match_phrase" : {
            "title" : "yagao producer"
        }
    }
}

高亮显示

{
    "query" : {
        "match_phrase" : {
            "title" : "立信"
        }
    },
    "highlight": {
    	"fields":{
    		"title": {}
    	}
    }
}

操作

手机扫码访问