Elasticsearch (ES篇): 字段类型、分页排序

718 阅读2分钟

「这是我参与2022首次更文挑战的第19天,活动详情查看:2022首次更文挑战」。

字段类型

  • 字符串类型

text 、 keyword

  • 数值类型 (8种)

long, integer, short, byte, double, float, half_float, scaled_float

  • 日期类型

date

  • 布尔值类型

boolean

  • 二进制类型

binary

等等......

指定字段的类型 (使用mapping)

PUT /powermall/_doc/2

{

  "mapping" : {

     "properties" : {

        "name" : {

            "type" : "keyword"

        },

        "initPrice" : {

            "type": "double"

     },

     "nowPrice" : {

       "type" : "double"

     },

     "store" : {

       "type" : "integer"

     },

     "nameDesc" : {

       "type" : "text"

     }

     }

   },

   "name" : "huawei phone",

   "initPrice" : 3998.01,

   "nowPrice" : 3989.01,

   "store" : 200,

   "nameDesc" : "华为手机huawei phone"

}

如果文档字段没有指定类型,那么es 就会给我们默认配置字段类型;

#不推荐使用指定type的方式去操作,比如:

PUT /powermall2/goods/5

{

  "name" : "vovi shou ji",

  "price" : 3998.00,

  "store" : 8000

}

文档 替换:

PUT /powermall/_doc/1

{

   "name" : "huawei phone 2",

   "initPrice" : 3998.01,

   "nowPrice" : 3989.01,

   "store" : 200,

   "nameDesc" : "华为手机huawei phone 2"

}

(相当于是先删除后插入)

替换需要带上全部的字段,否则就会丢失掉字段;

文档 更新

POST /powermall/_update/1

{

  "doc" : {

    "nameDesc" : "华为手机huawei phone 2x3"

  }

}

删除数据:

DELETE /powermall2/_doc/1

数据 查询

GET /powermall/_search

{

  "query": {

    "match_all": {}

  }

}
GET /powermall/_search

{

  "query": {

    "match_phrase": {

        "name" : "phone"

    }

  }

}

match_phrase会精确匹配查询的短语,需要全部单词和顺序要完全一样,标点符号除外;

GET /powermall/_search

{

  "query": {

    "match_phrase_prefix": {

        "name" : "huawei"

    }

  }

}

match_phrase_prefix 和 match_phrase 用法是一样的,区别就在于它允许对最后一个词进行前缀匹配,比如查询 I like sw 就能匹配到 I like swimming and riding.


GET /powermall/_search

{

  "query": {

    "match": {

        "nameDesc" : "huawei"

    }

  },

  "_source": ["name","nowPrice"]

}
  • 使用_source,让结果只显示某几个字段;
  • match任何一个匹配上即可;
  • match和match phrase区别,举个例子 比如
  • He is chinese. 他是中国人
  • He is a devoloper engineer. 他是开发工程师
  • He works very hard.  他工作非常努力
  • 输入关键词 He is 
  • 用match可以查询出来上面的三条数据;
  • 用match_phrase只能查询出来前两条;
  • match 是分词之后,有任意一个匹配就可以;

分页排序

GET /powermall/_search

{

  "query": {

    "match": {
    "nameDesc" : "huawei"

    }

  },

  "sort": [

    {

      "store": {

        "order": "desc"

      }

    }

  ],

  "_source": ["name","nowPrice"],

  "from": 0,

  "size": 1

}

must 查询,相当于sql的and查询,所有的条件满足 where id = 1 and phone= xxx才可以;

GET /powermall/_search

{

  "query": {

    "bool": {

      "must": [

        {

          "match": {

            "nameDesc" : "huawei"

          }

        },

        {

          "match": {

            "name": "huawei"

          }

        }

      ]

    }

  },

  "_source": ["name","nowPrice"]

}

should查询,相当于SQL的or查询, where id = 1 or name = xxx;

GET /powermall/_search

{

  "query": {

    "bool": {

      "should": [

        {

          "match": {

            "nameDesc" : "huawei"

          }

        },

        {

          "match": {

            "name": "huawei"

          }

        }

      ]

    }

  },

  "_source": ["name","nowPrice"]

}

must_not 不满足条件的查询结果返回,满足条件的结果不返回;

GET /powermall/_search

{

  "query": {

    "bool": {

      "must_not": [

        {

          "match": {

            "nameDesc" : "huawei"

          }

        },

        {

          "match": {

            "name": "huawei"

          }

        }

      ]

    }

  },

  "_source": ["name","nowPrice"]

}

filter条件过滤

GET /powermall/_search

{

  "query": {

    "bool": {

      "must": [

        {

          "match": {

            "nameDesc" : "huawei"

          }

        },

        {

          "match": {

            "name": "huawei"

          }

        }

      ],

      "filter": [

        {

          "range": {

            "store": {

              "gte": 10,

              "lte": 20000

            }

          }

        }

      ]

    }

  },

  "_source": ["name","nowPrice"]

}

高亮显示

GET /powermall/_search

{

  "query": {

    "bool": {

      "should": [

        {

          "match": {

            "nameDesc" : "huawei"

          }

        }

      ]

    }

  },

  "highlight": {

    "pre_tags": "<span style='color:red'>",

    "post_tags": "</span>",

    "fields": {

      "nameDesc": {}

    }

  }

}