elasticsearch使用说明


  1. 安装(略)

  1. 使用

基础

创建索引
PUT /my_store 
{
    "mappings" : {
        "products" : {
            "properties" : {
                "productID" : {
                    "type" : "string",
                    "index" : "not_analyzed" 
                }
            }
        }
    }

}
查看索引结构
GET /city/_mapping
更新索引(更改字段类型)
DELETE /my_store (必段先删除原索引)

PUT /my_store 
{
    "mappings" : {
        "products" : {
            "properties" : {
                "productID" : {
                    "type" : "string",
                    "index" : "not_analyzed" 
                }
            }
        }
    }

}
更新索引(添加字段 不需要删除原有的类型)
PUT /my_store 
{
    "mappings" : {
        "products" : {
            "properties" : {
                "productID" : {
                    "type" : "string",
                    "index" : "not_analyzed" 
                }
            }
        }
    }

}
分词

es对text类型字段默认进行分词,查询ES索引中对某个字段如何分词

GET /my_store/_analyze
{
  "field": "productID",
  "text": "XHDK-A-1293-#fJ3"
}
强制不分词(使用关键词 index: not_analyzed)
PUT /my_store 
{
    "mappings" : {
        "products" : {
            "properties" : {
                "productID" : {
                    "type" : "string",
                    "index" : "not_analyzed" 
                }
            }
        }
    }

}

查询

第一种查询模式(直接查询文档)
根据ID查询(取回单个文档)
GET /website/blog/123?pretty
  1. Website为index名称,对应elasticsearch数据库的字段为”_index”
  2. Blog为类型,相对应传统关系型数据的“表table”概念,对应elasticsearch数据库的”_type”
  3. 123 即为 ID,对应elasticsearch数据库的字段为”_id”
查询一条记录的某个字段
GET /website/blog/123?_source=title,text

Title,text对应实际想返回的字段。

只返回字段,不返回元数据
GET /website/blog/123/_source
检查某条记录是否存在(根据ID)
HEAD http://localhost:9200/website/blog/123
第二种查询模式,查询QUERY语句(DSL语句)
match查询
POST /my_store/products/_search
{
 "query":{
   "term":{
     "price": 20
   }
 }
}
Term查询
POST /my_store/products/_search
{
 "query":{
   "term":{
     "price": 20
   }
 }
}
match与term的区别

match会对搜索关键字进行分词,比如当搜索“面包好吃”时,使用match会把搜索条件分成"面包"."好".“吃”,三个token,并对索引进行倒序查询 term只查"面包好吃"这四个字

ES对查询会默认进行评分,并按评分返回值,可通过特定参数设置不评分 使用 constant_score进行设置

curl -X GET "localhost:9200/my_store/products/_search" -H 'Content-Type: GET /my_store/products/_search
{
    "query" : {
        "constant_score" : { 
            "filter" : {
                "term" : { 
                    "price" : 20
                }
            }
        }
    }
}

过滤
GET /megacorp/employee/_search
{
    "query" : {
        "bool": {
            "must": {
                "match" : {
                    "last_name" : "smith" 
                }
            },
            "filter": {
                "range" : {
                    "age" : { "gt" : 30 } 
                }
            }
        }
    }
}
组合查询

当我们执行以下的SQL时

SELECT product
FROM   products
WHERE  (price = 20 OR productID = "XHDK-A-1293-#fJ3")
  AND  (price != 30)

对应es的操作语法

{
   "bool" : {
      "must" :     [],
      "should" :   [],
      "must_not" : [],
   }
}

本例子

GET /my_store/products/_search
{
   "query" : {
      "filtered" : { 
         "filter" : {
            "bool" : {
              "should" : [
                 { "term" : {"price" : 20}}, 
                 { "term" : {"productID" : "XHDK-A-1293-#fJ3"}} 
              ],
              "must_not" : {
                 "term" : {"price" : 30} 
              }
           }
         }
      }
   }
}


更新

PUT /website/blog/123
{
  "title": "My first blog entry",
  "text":  "I am starting to get the hang of this...",
  "date":  "2014/01/02"
}

删除文档

DELETE /website/blog/123

批量操作

BULK(待完善内容)
批量添加
POST /my_store/products/_bulk
{ "index": { "_id": 1 }}
{ "price" : 10, "productID" : "XHDK-A-1293-#fJ3" }
{ "index": { "_id": 2 }}
{ "price" : 20, "productID" : "KDKE-B-9947-#kL5" }
{ "index": { "_id": 3 }}
{ "price" : 30, "productID" : "JODL-X-1937-#pV7" }
{ "index": { "_id": 4 }}
{ "price" : 30, "productID" : "QQPX-R-3956-#aD8" }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值