elasticsearch语句应用


一、基本术语


elasticsearch语句应用_第1张图片


二、基本操作

#以下操作基于kibana


获取集群/节点等基本信息
相关命令:
GET /_cluster/health
GET /_cluster/state
GET _nodes/stats
GET _mapping
等等

添加文档

POST /megacorp/employee
{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]
}

Post /megacorp/employee
{
    "first_name" :  "Jane",
    "last_name" :   "Smith",
    "age" :         32,
    "about" :       "I like to collect rock albums",
    "interests":  [ "music" ]
}
Post /megacorp/employee
{
    "first_name" :  "Douglas",
    "last_name" :   "Fir",
    "age" :         35,
    "about":        "I like to build cabinets",
    "interests":  [ "forestry" ]
}

elasticsearch语句应用_第2张图片

查询某一条数据
GET megacorp/employee/AWV_TVH2xpSIouFg4Tco
index/type/_id为查询依据
elasticsearch语句应用_第3张图片
相关命令:
得到具体某个字段

GET megacorp/employee/1/_source

查询所有数据(_search)

GET megacorp/employee/_search
{
  "query": {
    "match_all": {}
  }
}

_search命令代替了_id的位置
返回所有该index中的数据
elasticsearch语句应用_第4张图片

指定条件详细查找

GET topic/_search
{ 
  "_source": ["commented_times","clicks_times","url"], 
  "query": {
     "match": {
       "url": "http://cryptbb2gezhohku.onion/showthread.php?"
     }
    },
    "size": 500
}

查看数据量(_count)

GET megacorp/employee/_count

_count命令代替_id
elasticsearch语句应用_第5张图片

控制返回数据量大小(from、size)

GET /megacorp/employee/_search
{
  "from": 0
  , "size": 20
}

某一字段匹配(match)

GET megacorp/employee/_search
{
  "query": {
    "match": {
      "about": "rock"
    }
  }
}

多字段匹配(multi_match)

GET /megacorp/employee/_search
{
  "query" : {
        "multi_match": {
          "query": "rock",
          "fields": ["about","interests"]
        }
     }
 }

还可以提高某一字段的权重值

GET /megacorp/employee/_search
{
  "query" : {
        "multi_match": {
          "query": "rock",
          "fields": ["about","interests^3"]
        }
    }
}

添加高亮显示(highlight)

GET /megacorp/employee/_search
{
  "query": {
    "match": {
      "about": "rock"
    }
  }
  , "highlight": {
    "fields": {
      "about":{}
    }
  }
}

bool查询(bool)
包括must、must_not 、should、filter等逻辑

GET /megacorp/employee/_search
{
  "query" : {
        "bool": {
          "should": [
           {"match": {
             "about": "rock"
           }},
           {"match": {
             "about": "climbing"
           }}
          ],
          "minimum_should_match": 1
        }
    }
}

GET /megacorp/employee/_search
{
  "query": {
    "bool": {
     "must": [
       {
         "match": {
           "about": "rock"
         }
       },
       {
         "match": {
           "interests": "music"
         }
       }
     ], 
     "should": [
       {
         "match": {
           "first_name": "Jane"
         }
       }
     ], 
      "filter": {
        "range": {
          "age": {
            "gt": 20
          }
        }
      }
    }
  }
}

elasticsearch语句应用_第6张图片

模糊搜索(fuzziness)

GET /megacorp/employee/_search
{
  "query": {
   "match": {
     "about": {
       "query": "rockyy climbinga",
       "fuzziness": 1
     }
   }
  }
}

通配符搜索(wildcard)

GET /megacorp/employee/_search
{
  "query": {
    "wildcard": {
      "about": {
        "value": "roc*"
      }
    }
  }
}

不常用命令
查看文档是否存在

HEAD /megacorp/employee/1

删除文档

DELETE /megacorp/employee/1

使用script更新数据

POST /megacorp/employee/lvP3kWMBIzE12K7Jp7xQ/_update
{
  "script" : "ctx._source.age += 5"
}

分词器

GET /_analyze
{
  "analyzer": "standard",
  "text": "Text to analyze"
}

phrase查询

GET /megacorp/employee/_search
{
  "query" : {
        "match_phrase" : {
            "about" : "rock climbing"
        }
    }
}

term查询

GET /megacorp/employee/_search
{
  "query": {
    "term": {
      "age": {
        "value": 25
      }
    }
  }
}

你可能感兴趣的:(Elasticsearch)