ElasticSearch-入门学习

我只是一个虾纸丫 提交于 2020-02-04 21:53:38

基本概念

索引:含有相同属性的文档集合 --相当于database
类型:索引可以定义一个或多个类型,文档必须属于一个类型 --相当于table
文档:文档是可以被索引的基本数据单位,是最小的存储单位 --相当于row

RESTFul API

索引创建:

http://localhost:9200/people put
{
  "settings": {
    "index": {
      "number_of_shards": 5,
      "number_of_replicas": 1
    }
  }
}

在索引上创建类型映射

http://localhost:9200/people/man/_mappings put/post
{
  "properties": {
    "name": {
      "type": "text"
    },
    "country": {
      "type": "keyword"
    },
    "age": {
      "type": "integer"
    },
    "date": {
      "type": "date",
      "format": "yyyy-MM-dd HH:mm:ss"
    }
  }
}

一次性创建索引和类型映射

http://localhost:9200/people put
{
  "settings": {
    "index": {
      "number_of_shards": 5,
      "number_of_replicas": 1
    }
  },
  "mappings": {
    "man": {
      "properties": {
        "name": {
          "type": "text"
        },
        "country": {
          "type": "keyword"
        },
        "age": {
          "type": "integer"
        },
        "date": {
          "type": "date",
          "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
        }
      }
    }
  }
}

插入文档(自定义文档id)

http://localhost:9200/people/man/1 put/post
{
  "name": "zhangnna",
  "country": "中国",
  "age": 25,
  "date": "2020-02-04"
}

插入文档(自动生成文档id)

http://localhost:9200/people/man post
{
  "name": "zhangnan",
  "country": "China",
  "age": 25,
  "date": "2020-02-04"
}

修改文档

http://localhost:9200/people/man/1/_update post
{
  "doc": {
    "name": "张四"
  }
}

修改文档(脚本修改,默认脚本:painless)

http://localhost:9200/people/man/1/_update post
{
  "script": {
    "lang": "painless",
    "inline": "ctx._source.age+=5"
  }
}
{
  "script": {
    "lang": "painless",
    "inline": "ctx._source.age+=params.age",
    "params": {
      "age": 25
    }
  }
}

删除索引

http://localhost:9200/people delete

删除文档

http://localhost:9200/people/man/1 delete

基本查询

http://localhost:9200/people/man/1 get

条件查询

http://localhost:9200/people/man/_search post
{
  "query": {
    "match_all": {}
  },
  "from": 0,
  "size": 2
}

条件查询-关键词查询

http://localhost:9200/people/man/_search post
{
  "query": {
    "match": {
      "name": "zhang"
    }
  },
  "sort": [
    {
      "_id": {
        "order": "desc"
      }
    }
  ]
}

聚合查询

http://localhost:9200/people/man/_search post
{
  "aggs": {
    "group_by_date": {
      "terms": {
        "field": "date"
      }
    },
    "group_by_age": {
      "terms": {
        "field": "age"
      }
    },
    "stats_age": {
      "stats": {
        "field": "age"
      }
    },
    "sun_age": {
      "sum": {
        "field": "age"
      }
    }
  }
}

高级查询-Query

全文本查询:针对文本类型数据

习语查询(match_phrase)
http://localhost:9200/people/man/_search post
{
  "query": {
    "match_phrase": {
      "name": "张三李白"
    }
  }
}
多个字段查询(multi_match)
http://localhost:9200/people/man/_search post
{
  "query": {
    "multi_match": {
      "query": "张三",
      "fields": [
        "name",
        "counrty"
      ]
    }
  }
}
语法查询(query_string)
http://localhost:9200/people/man/_search post
{
  "query": {
    "query_string": {
      "query": "(张三 and 李白) or snooker"
    }
  }
}

字段级别查询:针对结构化数据,如数字、日期等

范围查询(range gte是>=, gt是>)
http://localhost:9200/people/man/_search post
{
  "query": {
    "range": {
      "age": {
        "gte": 20,
        "lte": 49
      }
    }
  }
}

高级查询-Filter

在查询过程中,只判断该文档是否满足条件,只有Yes后者No。

http://localhost:9200/people/man/_search post
{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "age": 12
        }
      }
    }
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!