部分笔记 不完全指北 ElasticSearch版本更迭很快,所作记录可能有些偏差

查看所有索引
GET /_cat/indices?v
创建一个索引demo
PUT one_index 
{
  "mappings": {
    "type_name": {
      "properties": { 
        "title":    {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
            }, 
        "name":     { "type": "text"  }, 
        "age":      { "type": "integer" },  
        "created":  {
          "type":   "date", 
          "format": "strict_date_optional_time||epoch_millis"
        }
      }
    }
  }
}
插入/更新文档
PUT(或POST) one_index/type_name/1
{
    "title": "ceshi", 
    "name": " mingzi", 
    "age": 12,  
    "created": 1515549710
}
查看索引类型结构
GET one_index/type_name/_mapping
mapping基本类型

text(原string), keyword, date, long, double, boolean , ip

创建结构
PUT /one_index
{
  "mappings": {
    "type_name" : {
      "properties" : {
        "user_id" : {
          "type" :   "long"
        }
      }
    }
  }
}
创建/更新结构
PUT /one_index/_mapping/type_name
{
  "properties" : {
    "user_id" : {
        "type" :   "long"
    }
  }
}
删除索引
Delete order_index
删除多个索引
DELETE /index_one,index_two
空查询
GET /_search {}

GET /index_/type1,type2/_search {}

GET /_search
{
    "query": {
        "match_all": {}
    }
}
分页
GET /_search
{
  "from": 30,
  "size": 10
}
match查询
tips:如果你在一个全文字段上使用 match 查询,在执行查询前,它将用正确的分析器去分析查询字符串
如果在一个精确值的字段上使用它, 例如数字、日期、布尔或者一个 not_analyzed 字符串字段,那么它将会精确匹配给定的值

GET /_search
{
    "query": {
        "match": {
            "tweet": "elasticsearch"
        }
    }
}
multi_match
tips:查询可以在多个字段上执行相同的 match 查询

GET /_search
{
    "query": {
        "multi_match": {
            "query":    "full text search",
            "fields":   [ "title", "body" ]
        }
    }    
}
range 查询
GET /_search
{
    "query": {
        "range": {
            "age": {
                "gte":  20,
                "lt":   30
            }
        }
    }
}
term查询
tips:用于精确值 匹配,这些精确值可能是数字、时间、布尔或者那些 not_analyzed 的字符串

GET /_search
{
    "query": {
         "term": { "age": 26 }
    }
}
terms查询
tips:terms 查询和 term 查询一样,但它允许你指定多值进行匹配。如果这个字段包含了指定值中的任何一个值,那么这个文档满足条件

GET /_search
{
    "query": {
         "terms": { 
            "tag": [ "search", "full_text", "nosql" ] 
        }
    }
}
exists 查询
tips:用于查找那些指定字段中有值 (exists) 的文档

GET /_search
{
    "query": {
         "exists": { 
            "field":    "title"
        }
    }
}
missing 查询
tips:用于查找那些指定字段中无值 (missing) 的文档

GET /_search
{
    "query": {
         "missing": { 
            "field":    "title"
        }
    }
}
组合查询
tips:
- must 文档 必须 匹配这些条件才能被包含进来。
- must_not 文档 必须不 匹配这些条件才能被包含进来。
- should 如果满足这些语句中的任意语句,将增加 _score ,否则,无任何影响。它们主要用于修正每个文档的相关性得分。
- filter 必须 匹配,但它以不评分、过滤模式来进行。这些语句对评分没有贡献,只是根据过滤标准来排除或包含文档。
GET /_search
{
    "query" : {
         "bool": { 
             "must": {
                 "match": { "part_name": "保险" }
             },
             "must_not" : {
                 "match": { "create_person" : "STDINGSUN2" }
             },
             "should" : {
                 "range" : { "price1" : { "gte" : 1000} }
             }
         }
    }
}
评分查询
tips:用于你只需要执行一个 filter 而没有其它查询

GET /_search
{
    "query" : { 
        "constant_score":   {
            "filter": {
                "term": { "category": "ebooks" } 
            }
        }
    }
}
验证查询语句
GET /gb/tweet/_validate/query
或( GET /gb/tweet/_validate/query?explain ):带解释
{
   "query": {
      "tweet" : {
         "match" : "really powerful"
      }
   }
}
排序查询
GET /_search
{
    "query" : {
         "bool" : {
             "filter" : {
                 "match" : {
                     "create_person" : "GD001"
                 }
             }
         }
    },
    "sort" : { "@timestamp" : { "order" : "desc" } }
}
多级排序查询
GET /_search
{
    "query" : {
        "bool" : {
            "must":   { "match": { "tweet": "manage text search" }},
            "filter" : { "term" : { "user_id" : 2 }}
        }
    },
    "sort": [
        { "date":   { "order": "desc" }},
        { "_score": { "order": "desc" }}
    ]
}
字段多值的排序查询
tips:对于数字或日期,你可以将多值字段减为单值,这可以通过使用 min 、 max 、 avg 或是 sum 排序模式

GET /_search
{
    "query" : {
        "sort": {
            "dates": {
                "order": "asc",
                "mode":  "min"
            }
    }
}
查询(获取特定的字段)
GET /_search
{
    "query":   { "match_all": {}},
    "_source": [ "title", "created" ]
}
查询(去除_source)
GET /_search
{
    "_source": false,
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}
查询(过滤某些字段)
GET /_search
{
    "_source": {
        "includes": [ "obj1.*", "obj2.*" ],
        "excludes": [ "*.description" ]
    },
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}
复杂域数据类型

多值域

    eq : { "tag": [ "search", "nosql" ]}

空域

    eq : 
    "null_value":               null,
    "empty_array":              [],
    "array_with_null_value":    [ null ]

多层级对象

    {
        "tweet":            "Elasticsearch is very flexible",
        "user": {
            "id":           "@johnsmith",
            "gender":       "male",
            "age":          26,
            "name": {
                "full":     "John Smith",
                "first":    "John",
                "last":     "Smith"
            }
        }
    }
动态映射

如果遇到新字段,对象 my_type 就会抛出异常
而内部对象 stash 遇到新字段就会动态创建新字段

    可以用 dynamic 配置来控制这种行为 ,可接受的选项如下:

    true   动态添加新的字段--缺省
    false    忽略新的字段
    strict    如果遇到新字段抛出异常

    tips:配置参数 dynamic 可以用在根 object 或任何 object 类型的字段上。
         你可以将 dynamic 的默认值设置为 strict , 而只在指定的内部对象中开启它

    PUT /my_index
    {
        "mappings": {
            "my_type": {
                "dynamic":      "strict", 
                "properties": {
                    "title":  { "type": "string"},
                    "stash":  {
                        "type":     "object",
                        "dynamic":  true 
                    }
                }
            }
        }
    }
自定义动态映射
日期检查

当 Elasticsearch 遇到一个新的字符串字段时,它会检测这个字段是否包含一个可识别的日期,比如 2014-01-01 。 如果它像日期,这个字段就会被作为 date 类型添加。否则,它会被作为 string 类型添加。

日期检测可以通过在根对象上设置 date_detection 为 false 来关闭    
PUT /my_index
{
    "mappings": {
        "my_type": {
            "date_detection": false
        }
    }
}
动态模板
模板按照顺序来检测
es :以 _es 结尾的字段名需要使用 spanish 分词器。
en :所有其他字段使用 english 分词器。
PUT /my_index
{
    "mappings": {
        "my_type": {
            "dynamic_templates": [
                { "es": {
                      "match":              "*_es", 
                      "match_mapping_type": "string",
                      "mapping": {
                          "type":           "string",
                          "analyzer":       "spanish"
                      }
                }},
                { "en": {
                      "match":              "*", 
                      "match_mapping_type": "string",
                      "mapping": {
                          "type":           "string",
                          "analyzer":       "english"
                      }
                }}
            ]
}}}