- 创建索引 index/type/id
index = 关系型数据库database
type = 关系型数据库table
id = 关系型数据库主键id 必须唯一 不指定则自动生成PUT /index/type/1
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
- 检索索引
GET index/type/1
{
"_index" : "megacorp", //索引
"_type" : "employee", //索引type
"_id" : "1", //索引id
"_version" : 1, //修改次数
"found" : true, //是否找到
"_source" : { //内容
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
}
- 基本搜索
GET index/type/_search 全文搜索
GET index/type/_search?q=last_name:Smith query_string 字符串搜索
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1.0,
"hits": [
{
"_index": "test_database",
"_type": "test_table1",
"_id": "_search",
"_score": 1.0,
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
}
]
}
}
- DSL搜索 搜索last_name为smith的
GET /megacorp/employee/_search
{
"query" : {
"match" : {
"last_name" : "Smith"
}
}
}
搜索last_name为smith 切年龄大于30的 GET /megacorp/employee/_search
{
"query" : {
"bool": {
"must": {
"match" : {
"last_name" : "smith"
}
},
"filter": {
"range" : {
"age" : { "gt" : 30 }
}
}
}
}
}
短语搜索GET /megacorp/employee/_search 完全匹配搜索的短语
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
}
}
聚合搜索 {
"aggs": {
"all_interests": {
"terms": { "field": "interests" }
}
}
}
{
...
"hits": { ... },
"aggregations": {
"all_interests": {
"buckets": [
{
"key": "music",
"doc_count": 2
},
{
"key": "forestry",
"doc_count": 1
},
{
"key": "sports",
"doc_count": 1
}
]
}
}
}