增删改查

添加

PUT /index_name/_doc/id
{
  数据
}

当存在给定索引是新增一条文档,如果不存在test索引,es通过auto mapping自动创建索引并添加记录

创建demo数据,商品


 @ApiModelProperty(value = "id",dataType = "long")
    @Id
    private Long id;

    @ApiModelProperty(value = "商品名称",dataType = "string")
    @Field(type = FieldType.Text,analyzer = "ik_max_word")
    private String name;

    @ApiModelProperty(value = "副标题",dataType = "string")
    @Field(type = FieldType.Text,analyzer = "ik_max_word")
    private String subTitle;


    @ApiModelProperty(value = "品牌名称",dataType = "string")
    @Field(type = FieldType.Keyword)
    private String brandName;

    @ApiModelProperty(value = "一级分类id",dataType = "long")
    @Field(type = FieldType.Long)
    private Long oneCategoryId;

    @ApiModelProperty(value = "二级分类id",dataType = "long")
    @Field(type = FieldType.Long)
    private Long twoCategoryId;




    @ApiModelProperty(value = "展示图",dataType = "string")
    @Field(type = FieldType.Keyword)
    private String pic;

    @ApiModelProperty(value = "商品货号",dataType = "string")
    @Field(type = FieldType.Keyword)
    private String productSn;



    @ApiModelProperty(value = "上架状态 0 未上架  1已上架",dataType = "int")
    @Field(type = FieldType.Integer)
    private Integer publishStatus;

    @ApiModelProperty(value = "是否新品 0不是新品 1 是新品",dataType = "int")
    @Field(type = FieldType.Integer)
    private Integer newStatus;

    @ApiModelProperty(value = "是否推 荐  0不推荐  1推荐",dataType = "int")
    @Field(type = FieldType.Integer)
    private Integer recommendStatus;


    @ApiModelProperty(value = "排序",dataType = "int")
    @Field(type = FieldType.Long)
    private Integer sort;


    @ApiModelProperty(value = "价格",dataType = "long")
    @Field(type = FieldType.Long)
    private Long price;

es创建数据dsl

//创建mapping
PUT es_pro
{
  "mappings" : {
      "properties" : {
        "_class" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "brandName" : {
          "type" : "keyword"
        },
        "id" : {
          "type" : "long"
        },
        "name" : {
          "type" : "text",
          "analyzer" : "ik_max_word"
        },
        "newStatus" : {
          "type" : "integer"
        },
        "oneCategoryId" : {
          "type" : "long"
        },
        "pic" : {
          "type" : "keyword"
        },
        "price" : {
          "type" : "long"
        },
        "productSn" : {
          "type" : "keyword"
        },
        "publishStatus" : {
          "type" : "integer"
        },
        "recommendStatus" : {
          "type" : "integer"
        },
        "sort" : {
          "type" : "long"
        },
        "subTitle" : {
          "type" : "text",
          "analyzer" : "ik_max_word"
        },
        "twoCategoryId" : {
          "type" : "long"
        }
      }
    }
}

//插入数据
PUT /es_pro/_doc/5
{
 "id" : 5,
          "name" : "小米11",
          "subTitle" : "5G 骁龙888 2K AMOLED四曲面柔性屏 1亿像素 手机",
          "brandName" : "小米",
          "oneCategoryId" : 6,
          "twoCategoryId" : 7,
          "pic" : "小米11.png",
          "productSn" : "sn123123",
          "publishStatus" : 1,
          "newStatus" : 1,
          "recommendStatus" : 0,
          "sort" : 1,
          "price" : 333300
}

PUT /es_pro/_doc/6
{
"id" : 6,
          "name" : "华为nava8",
          "subTitle" : "5G手机 ",
          "brandName" : "华为",
          "oneCategoryId" : 6,
          "twoCategoryId" : 7,
          "pic" : "nava8黑.png",
          "productSn" : "sn123123",
          "publishStatus" : 1,
          "newStatus" : 0,
          "recommendStatus" : 1,
          "sort" : 1,
          "price" : 89900
}

PUT /es_pro/_doc/7
{
"id" : 7,
          "name" : "mate30",
          "subTitle" : "5G  全网通 手机",
          "brandName" : "华为",
          "oneCategoryId" : 6,
          "twoCategoryId" : 7,
          "pic" : "hwmate20白.png",
          "productSn" : "sn12313",
          "publishStatus" : 1,
          "newStatus" : 0,
          "recommendStatus" : 1,
          "sort" : 2,
          "price" : 399900
}

PUT /es_pro/_doc/8
{
"id" : 8,
          "name" : "华为 HUAWEI 手环6",
          "subTitle" : "NFC版 运动手环 智能手环 ",
          "brandName" : "华为",
          "oneCategoryId" : 6,
          "twoCategoryId" : 8,
          "pic" : "华为手环6青.png",
          "productSn" : "sn123141",
          "publishStatus" : 1,
          "newStatus" : 1,
          "recommendStatus" : 0,
          "sort" : 2,
          "price" : 33800
}

PUT /es_pro/_doc/9
{
 "id" : 9,
          "name" : "小米全面屏电视",
          "subTitle" : "55英寸 E55X  显示屏",
          "brandName" : "小米",
          "oneCategoryId" : 6,
          "twoCategoryId" : 9,
          "pic" : "e55x显示屏.png",
          "productSn" : "sn1231",
          "publishStatus" : 1,
          "newStatus" : 0,
          "recommendStatus" : 1,
          "sort" : 3,
          "price" : 249900
}

PUT /es_pro/_doc/10
{
 "id" : 10,
          "name" : "华为 AD80HW",
          "subTitle" : " 23.8英寸",
          "brandName" : "华为",
          "oneCategoryId" : 6,
          "twoCategoryId" : 9,
          "pic" : "ad801.png",
          "productSn" : "sn432342",
          "publishStatus" : 1,
          "newStatus" : 1,
          "recommendStatus" : 1,
          "sort" : 4,
          "price" : 127167
}

PUT /es_pro/_doc/11
{
 "id" : 11,
          "name" : "华为笔记本电脑 MateBook ",
          "subTitle" : "MateBook X 2020款 电脑",
          "brandName" : "华为",
          "oneCategoryId" : 6,
          "twoCategoryId" : 10,
          "pic" : "matebook2020青.png",
          "productSn" : "sn1231",
          "publishStatus" : 1,
          "newStatus" : 1,
          "recommendStatus" : 1,
          "sort" : 5,
          "price" : 678700
}

PUT /es_pro/_doc/12
{
 "id" : 12,
          "name" : "小米手环6 ",
          "subTitle" : "全面彩屏 ",
          "brandName" : "小米",
          "oneCategoryId" : 6,
          "twoCategoryId" : 8,
          "pic" : "mi6手环.png",
          "productSn" : "sc1231",
          "publishStatus" : 1,
          "newStatus" : 0,
          "recommendStatus" : 1,
          "sort" : 6,
          "price" : 30000
}

PUT /es_pro/_doc/13
{
 "id" : 13,
          "name" : "小米10",
          "subTitle" : "双模5G 骁龙865  手机",
          "brandName" : "小米",
          "oneCategoryId" : 6,
          "twoCategoryId" : 7,
          "pic" : "mi10冰海蓝.png",
          "productSn" : "sn767",
          "publishStatus" : 1,
          "newStatus" : 1,
          "recommendStatus" : 1,
          "sort" : 7,
          "price" : 389900
}
PUT /es_pro/_doc/14
{
 "id" : 14,
          "name" : "小米11手机",
          "subTitle" : "青春版",
          "brandName" : "小米",
          "oneCategoryId" : 6,
          "twoCategoryId" : 7,
          "pic" : "mi11青春版橙.png",
          "productSn" : "sas12313",
          "publishStatus" : 1,
          "newStatus" : 1,
          "recommendStatus" : 0,
          "sort" : 3,
          "price" : 134400
}

PUT /es_pro/_doc/15
{
 "id" : 15,
          "name" : "RedmiBook Air ",
          "subTitle" : "2020 小米 air笔记本 电脑",
          "brandName" : "小米",
          "oneCategoryId" : 6,
          "twoCategoryId" : 10,
          "pic" : "miair.png",
          "productSn" : "za1231",
          "publishStatus" : 1,
          "newStatus" : 0,
          "recommendStatus" : 1,
          "sort" : 8,
          "price" : 435300
}

PUT /es_pro/_doc/16
{
"id" : 16,
 "name" : "小米笔记本Pro ",
          "subTitle" : "15 全面屏 电脑",
          "brandName" : "小米",
          "oneCategoryId" : 6,
          "twoCategoryId" : 10,
          "pic" : "mipro笔记本.png",
          "productSn" : "zzs12312",
          "publishStatus" : 1,
          "newStatus" : 0,
          "recommendStatus" : 1,
          "sort" : 9,
          "price" : 766600
}

PUT /es_pro/_doc/17
{
 "id" : 17,
          "name" : "小米电视",
          "subTitle" : " E32S 显示屏",
          "brandName" : "小米",
          "oneCategoryId" : 6,
          "twoCategoryId" : 9,
          "pic" : "小米e32s显示屏.png",
          "productSn" : "asd2123",
          "publishStatus" : 1,
          "newStatus" : 1,
          "recommendStatus" : 0,
          "sort" : 4,
          "price" : 333300
}

PUT /es_pro/_doc/18
{
 "id" : 18,
          "name" : "小米11 手机",
          "subTitle" : "5G 骁龙888",
          "brandName" : "小米",
          "oneCategoryId" : 6,
          "twoCategoryId" : 7,
          "pic" : "小米11.png",
          "productSn" : "zsaz123133",
          "publishStatus" : 1,
          "newStatus" : 1,
          "recommendStatus" : 0,
          "sort" : 5,
          "price" : 452300
}
PUT /es_pro/_doc/19
{
 "id" : 19,
          "name" : "红米40 手机",
          "subTitle" : " Redmi K40 ",
          "brandName" : "小米",
          "oneCategoryId" : 6,
          "twoCategoryId" : 7,
          "pic" : "redmi40黑.png",
          "productSn" : "zs123123",
          "publishStatus" : 1,
          "newStatus" : 0,
          "recommendStatus" : 1,
          "sort" : 10,
          "price" : 111100
}

查询

GET /bank/_search
{
  "query": { "match_all": {} },
  "sort": [
    { "account_number": "asc" }
  ],
  "from": 10,
  "size": 10
}

match(分词查询)

将给定条件分词,返回包含任意个分词得文档

//查询地址 包含nc、cd得test文档
GET test/_search
{
  "query": {
    "match": {
      "addr": "nc cd"
    }
  }
}

match_phrase(整体匹配)

返回包含完整给定条件得文档

//查询地址是sichuan     nc得test文档
GET test/_search
{
  "query": {
    "match_phrase": {
      "addr": "sichuan     nc "
    }
  }
}

bool(条件组合)

通过must、must_not等关系组合获取结果

//查询地址 包含nc、cd且地址不是sichuan     nc得test文档
GET test/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "addr": "nc cd"
          }
        }
      ],  
      "must_not": [
        {
          "match_phrase": {
            "addr": "sichuan     nc"
          }
        }
      ]
    }
  }
}

filter

按给定条件进行过滤

//获取年龄大于等于11得文档
GET test/_search
{
  "query": {
    "bool": {
      "must": [
        {"match_all": {}}
      ]
      , "filter": [
        {
          "range": {
            "age": {
              "gte": 11
            }
          }
        }
      ]
    }
  }
}

修改

删除

posted @ 2021-03-29 13:50  犬犬呀  阅读(68)  评论(0)    收藏  举报