es常用操作手册

1、简单的集群管理

(1)快速检查集群的健康状况

GET /_cat/health?v

 

(2)快速查看集群中有哪些索引

GET /_cat/indices?v

 

(3)简单的索引操作

创建索引:PUT /test_index?pretty

删除索引:DELETE /test_index?pretty

 

2、文档的CRUD操作

(1)新增文档,建立索引

PUT /ecommerce/_doc/1

{

    "name" : "gaolujie yagao",

    "desc" :  "gaoxiao meibai",

    "price" :  30,

    "producer" :      "gaolujie producer",

    "tags": [ "meibai", "fangzhu" ]

}

 

不指定id:

POST /ecommerce/_doc
{
  "name": "jiaqiangban gaolujie yagao",
  "desc": "gaoxiao meibai",
  "price": 30,
  "producer": "china likes producer",
  "tags": [
    "meibai",
    "fangzhu"
  ]
}

 

(2)检索文档

GET /ecommerce/_doc/1

 

(3)替换文档

PUT /ecommerce/_doc/1

{

    "name" : "jiaqiangban gaolujie yagao",

    "desc" :  "gaoxiao meibai",

    "price" :  30,

    "producer" :      "gaolujie producer",

    "tags": [ "meibai", "fangzhu" ]

}

 

(4)更新文档

POST /ecommerce/_update/1

{

  "doc": {

    "name": "jiaqiangban gaolujie yagao"

  }

}

 

(5)删除文档

DELETE /ecommerce/_doc/1

 

3、query string search

搜索全部:GET /ecommerce/_doc/_search

 

4、query DSL

(1)查询所有的商品

GET /ecommerce/_doc/_search

{

  "query": { "match_all": {} }

}

 

(2)查询名称包含yagao的商品,同时按照价格降序排序

GET /ecommerce/_doc/_search

{

    "query" : {

        "match" : {

            "name" : "yagao"

        }

    },

    "sort": [

        { "price": "desc" }

    ]

}

 

(3)分页查询商品

GET /ecommerce/_doc/_search

{

  "query": { "match_all": {} },

  "from": 1,

  "size": 1

}

 

(4)指定要查询出来商品的名称和价格就可以

GET /ecommerce/_doc/_search

{

  "query": { "match_all": {} },

  "_source": ["name", "price"]

}

 

3、query filter(过滤查询filtered已被弃用,并在ES 5.0中删除)

搜索商品名称包含yagao,而且售价大于25元的商品

GET /ecommerce/_doc/_search

{

    "query" : {

        "bool" : {

            "must" : {

                "match" : {

                    "name" : "yagao"

                }

            },

            "filter" : {

                "range" : {

                    "price" : { "gt" : 25 }

                }

            }

        }

    }

}

按日期范围搜索

GET /forum/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "postDate": {
              "gt": "2017-03-10||-30d",

               "lt": "now-30d"
            }
          }
        }
      ]
    }
  }
}

 

4、full-text search(全文检索)

GET /ecommerce/_doc/_search

{

    "query" : {

        "match" : {

            "producer" : "yagao producer"

        }

    }

}

 

5、phrase search(短语搜索)

GET /ecommerce/_doc/_search

{

    "query" : {

        "match_phrase" : {

            "producer" : "yagao producer"

        }

    }

}

 

6、highlight search(高亮搜索结果)

GET /ecommerce/product/_search

{

    "query" : {

        "match" : {

            "producer" : "producer"

        }

    },

    "highlight": {

        "fields" : {

            "producer" : {}

        }

    }

}

 

7、聚合查询

(1)计算每个tag下的商品数量

将文本field的fielddata属性设置为true

FieldData:当排序(sort),统计(aggs)时,ES把涉及到的字段数据全部读取到内存(JVM Heap)中进行操作。相当于进行了数据缓存,提升查询效率。

indices.fielddata.cache.size 配置fieldData的Cache大小,可以配百分比也可以配一个准确的数值。cache到达约定的内存大小时会自动清理,驱逐一部分FieldData数据以便容纳新数据。默认值为unbounded无限

驱逐线 和 断路器。当缓存数据到达驱逐线时,会自动驱逐掉部分数据,把缓存保持在安全的范围内。当用户准备执行某个查询操作时,断路器就起作用了,缓存数据+当前查询需要缓存的数据量到达断路器限制时,会返回Data too large错误,阻止用户进行这个查询操作。

PUT /ecommerce/_mapping

{

  "properties": {

    "tags": {

      "type": "text",

      "fielddata": true

    }

  }

}

 

GET /ecommerce/_doc/_search

{

  "size": 0,    // 表示返回数据个数

  "aggs": {

    "all_tags": {    // 这个只是聚合结果的名字

      "terms": { "field": "tags" }

    }

  }

}

 

(2)对名称中包含yagao的商品,计算每个tag下的商品数量

GET /ecommerce/_doc/_search

{

  "size": 0,

  "query": {

    "match": {

      "name": "yagao"

    }

  },

  "aggs": {

    "all_tags": {

      "terms": {

        "field": "tags"

      }

    }

  }

}

 

(3)先分组,再算每组的平均值,计算每个tag下的商品的平均价格

GET /ecommerce/_doc/_search

{

    "size": 0,

    "aggs" : {

        "group_by_tags" : {

            "terms" : { "field" : "tags" },

            "aggs" : {

                "avg_price" : {

                    "avg" : { "field" : "price" }

                }

            }

        }

    }

}

 

(4)计算每个tag下的商品的平均价格,并且按照平均价格降序排序

GET /ecommerce/_doc/_search

{

    "size": 0,

    "aggs" : {

        "all_tags" : {

            "terms" : { "field" : "tags", "order": { "avg_price": "desc" } },

            "aggs" : {

                "avg_price" : {

                    "avg" : { "field" : "price" }

                }

            }

        }

    }

}

 

(5)按照指定的价格范围区间进行分组,然后在每组内再按照tag进行分组,最后再计算每组的平均价格

GET /ecommerce/_doc/_search

{

  "size": 0,

  "aggs": {

    "group_by_price": {

      "range": {

        "field": "price",

        "ranges": [

          {

            "from": 0,

            "to": 20

          },

          {

            "from": 20,

            "to": 40

          },

          {

            "from": 40,

            "to": 50

          }

        ]

      },

      "aggs": {

        "group_by_tags": {

          "terms": {

            "field": "tags"

          },

          "aggs": {

            "average_price": {

              "avg": {

                "field": "price"

              }

            }

          }

        }

      }

    }

  }

}

 

 

8、match、term和bool查询

(1)精确匹配:match_phrase

{

  "query": {

    "match_phrase": {

        "content" : {

            "query" : "我的宝马多少马力”,

            "slop" : 1 // 调节因子,这里的位移是指分词器_analyze分词结果顺序的位移,而不是index中句子词组间的位移

        }

    }

  }

}

 

(2)多字段匹配:multi_match

{

  "query": {

    "multi_match": {

        "query" : "我的宝马多少马力",

        "fields" : ["title", "content"]

    }

  }

}

 

(3)完全匹配:term

{

  "query": {

    "term": {

      "content": "汽车保养"

    }

  }

}

 

(4)联合查询:bool

{

  "query": {

    "bool": {

      "must": {

        "term": {

          "content": "宝马"

        }

      },

      "must_not": {

        "term": {

          "tags": "宝马"

        }

      }

    }

  }

}

 

9、查看mapping

GET /ecommerce/_mapping

 

10、定位不合法搜索原因

GET /test_index/_doc/_validate/query?explain

{

  "query": {

    "math": {

      "test_field": "test"

    }

  }

}

 

11、测试分词器

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

// 验证字段上的分词器

GET /forum/_analyze
{
  "field": "articleID",
  "text": "XHDK-A-1293-#fJ3"
}

12、mget

GET /test_index/_mget

{

  "ids":[7,8]

}

 

GET /_mget

{

  "docs": [

    {

      "_index": "test_index",

      "_id": 7

    },

    {

      "_index": "test_index",

      "_id": 7

    }

  ]

}

 

13、自定义排序

GET /company/employee/_search 
{
  "query": {
    "constant_score": {
      "filter": {
        "range": {
          "age": {
            "gte": 30
          }
        }
      }
    }
  },
  "sort": [
    {
      "join_date": {
        "order": "asc"
      }
    }
  ]
}

 

14、function_score查询

POST /pms/_doc/_search
{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "must": [
            {
              "match_all": {}
            }
          ],
          "filter": {
            "bool": {
              "must_not": {
                "term": {
                  "id": 28
                }
              }
            }
          }
        }
      },
      "functions": [
        {
          "filter": {
            "match": {
              "name": "红米5A"
            }
          },
          "weight": 8
        },
        {
          "filter": {
            "match": {
              "subTitle": "红米5A"
            }
          },
          "weight": 2
        },
        {
          "filter": {
            "match": {
              "keywords": "红米5A"
            }
          },
          "weight": 2
        },
        {
          "filter": {
            "term": {
              "brandId": 6
            }
          },
          "weight": 5
        },
        {
          "filter": {
            "term": {
              "productCategoryId": 19
            }
          },
          "weight": 3
        }
      ],
      "score_mode": "sum",
      "min_score": 2
    }
  }
}

 

15、 查询含有fileds的字段

(1)best-fields策略,主要是说将某一个field匹配尽可能多的关键词的doc,优先返回回来。

(2)most-fields策略,主要是说尽可能返回更多field匹配到某个关键词的doc,优先返回回来。

PUT my_index

{

  "mappings": {

    "properties": {

      "text": {

        "type": "text",

        "fields": {

          "english": {

            "type":     "text",

            "analyzer": "english"

          }

        }

      }

    }

  }

}

 

PUT my_index/_doc/1

{ "text": "quick brown fox" }

 

PUT my_index/_doc/2

{ "text": "quick brown foxes" }

 

GET my_index/_search

{

  "query": {

    "multi_match": {

      "query": "quick brown foxes",

      "fields": [

        "text",

        "text.english"

      ],

      "type": "most_fields"

    }

  }

}

16. 新增索引
PUT  / arthas_entity_solution
{
  "aliases": {
    "arthas_entity_solution_alias": {}
  },
  "mappings": {
    "dynamic": "strict",
    "properties": {
      "solutionId": {
        "type": "keyword"
      },
      "developType": {
        "type": "keyword"
      },
      "topCategoryCode": {
        "type": "keyword"
      },
      "secondCategoryCode": {
        "type": "keyword"
      },
      "localizedName": {
        "properties": {
          "zh": {
            "type": "text",
            "analyzer": "ik_max_word",
            "search_analyzer": "ik_smart",
            "fields": {
              "keyword": {
                "type": "keyword"
              }
            }
          },
          "en": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword"
              }
            }
          }
        }
      },
      "nameZh": {
        "type": "keyword"
      },
      "nameEn": {
        "type": "keyword"
      },
      "imageUrl": {
        "type": "keyword"
      },
      "communicationType": {
        "type": "keyword"
      },
      "capabilityNameZh": {
        "type": "keyword"
      },
      "capabilityNameEn": {
        "type": "keyword"
      },
      "gmtCreate": {
        "type": "long"
      },
      "gmtModified": {
        "type": "long"
      }
    }
  }
}
 
17. 新增mapping字段
 
PUT  / arthas_entity_solution/_mapping
{
  "properties": {
    "localizedName": {
      "properties": {
        "zh": {
          "type": "text",
          "analyzer": "ik_max_word",
          "search_analyzer": "ik_smart",
          "fields": {
            "keyword": {
              "type": "keyword"
            }
          }
        },
        "en": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword"
            }
          }
        }
      }
    }
  }
}
 
18、全文检索控制精准度
 
(1)都要包含
GET /forum/_search
{
  "query": {
    "match": {
      "title": {
        "query": "java elasticsearch spark hadoop",
        "operator": "and"
      }
    }
  }
}
 
(2)匹配其中部分
GET /forum/_search
{
  "query": {
    "match": {
      "title": {
        "query": "java elasticsearch spark hadoop",
        "minimum_should_match": "75%"
      }
    }
  }
}
 
19、同时使用filter和match
GET /_search
{
  "query": { 
    "bool": { 
      "must": [
        { "match": { "title":   "Search"        }}, 
        { "match": { "content": "Elasticsearch" }}  
      ],
      "filter": [ 
        { "term":  { "status": "published" }}, 
        { "range": { "publish_date": { "gte": "2015-01-01" }}} 
      ]
    }
  }
}
20、索引别名新增删除指向
POST /_aliases
{ "actions": [ { "remove": { "alias": "my_index", "index": "my_index_v1" }}, { "add": { "alias": "my_index", "index": "my_index_v2" }} ] }

21、查询字段为null的数据

{

  "query": {

    "bool": {

      "must_not": {

        "exists": {

          "field": "name"

        }

      }

    }

  }

}

22、查询10条以上数据字段

{

    "size": 20,

    "query": {

        "bool": {

            "must": [

                {

                    "term": {

                        "topCategoryCode": "jjaf"

                    }

                },

                {

                    "term": {

                        "secondCategoryCode": "sp"

                    }

                }

            ]

        }

    }

}

23、terms查询

{

    "query": {

        "bool": {

            "should": {

                "terms": {

                    "entityId": [

                        94001,

                        670002,

                        590001,

                        614008

                    ]

                }

            }

        }

    }

}

24、查询包含匹配数组

{

    "query": {

        "bool": {

            "must": [

                {

                    "term": {

                        "communicationType": {

                            "value": "bluetooth"

                        }

                    }

                },

                {

                    "term": {

                        "communicationType": {

                            "value": "sigmesh"

                        }

                    }

                }

            ]

        }

    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值