玖叶教程网

前端编程开发入门

ES基础操作命令(es 基本操作)

1.创建索引


1.代码

put /test/type/1
{
    "name":"北辰君"
    "age":"20"
}

2.语法

put /索引/类型/Id
{
    请求体
}
//新版本type可以省略

3.效果图

2.创建索引规则


1.代码

put /test1
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "age": {
        "type": "long"
      }
    }
  }
}

2.效果图

3.官方文档地址

https://www.elastic.co/guide/en/elasticsearch/reference/current/keyword.html

3.修改文档信息


1.代码

POST /test/type/1/_update
{
  "doc": {
    "name": "幽幽晚风君且临"
  }
}

2.效果图

4.删除


1.代码

DELETE /test/type/1

2.效果图

5.查询


1.基本查询

GET test/type/_search?q=name:幽幽晚风君且临

2.效果图

6.复杂查询


1.基本简单查询

GET test/type/_search
{
  "query": {
    "match": {
      "name": "幽幽晚风君且临"
    }
  }
}

2.查询时指定返回字段

GET test/type/_search
{
  "query": {
    "match": {
      "name": "幽幽晚风君且临"
    }
  },
  "_source": ["name","age"]
}

3.排序

GET test1/_search
{
  "query": {
    "match": {
      "name": "幽幽晚风君且临"
    }
  },
 "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ]
}

4.分页

GET test1/_search
{
  "query": {
    "match": {
      "name": "幽幽晚风君且临"
    }
  },
 "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ],
  "from": 0, 
  "size": 1
}
// from 起始页
// size 每页多少数据

5.多条件查询

GET test1/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "晚风"
          }
        },
        {
          "match": {
            "age": 20
          }
        }
      ]
    }
  }
}

//must 相当于mysql  and
//should 相当于mysql or
//must_not 相当于mysql !=

6.过滤器filter取范围值

GET test1/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "幽幽晚风君且临"
          }
        }
      ],
      "filter": {
        "range": {
          "age": {
            "gte": 20,
            "lt": 30
          }
        }
      }
    }
  }
}

//gte大于等于
//lt小于 e =

7.term精确查询

  • term会直接查询精确的
  • metch会使分词器解析!(先分析文档,在通过分析的文档进行查询)
  • text 会被分词器解析 keyword不会被分词器解析,它就是一个分词
GET test1/_search
{
  "query": {
    "term": {
      "desc": "桃"
    }
  }
}

精确查询多个值:

GET test1/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
              "name": "晚风"
          }
        },
        {
          "term": {
              "desc": "桃"
          }
        }
      ]
    }
  }
}

8.字段高亮

GET test1/_search
{
  "query": {
   "match": {
     "name": "晚风"
   }
  },
  "highlight": {
    "fields": {
      "name": {}
    }
  }
}

9.自定义高亮样式

GET test1/_search
{
  "query": {
   "match": {
     "name": "晚风"
   }
  },
  "highlight": {
    "pre_tags": "<p class='name' style='color:red'>", 
    "post_tags": "</p>", 
    "fields": {
      "name": {}
    }
  }
}


发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言