ES索引常用命令

ES创建索引库

PUT product
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1
  },
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "desc": {
        "type": "text"
      },
      "price": {
        "type": "long"
      },
      "tag": {
        "type": "text"
      },
      "issue_date": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss"
      }
    }
  }
}

  

  

 

ES创建索引

PUT /product/_doc/1
{
  "name": "张三",
  "desc": "测试数据",
  "price": 1299,
  "tag": ["张三","里斯","王五"]
}
PUT /product/_doc/2
{
  "name":"李四",
  "desc":"xiaomi",
  "price":3999,
  "tag":["m1","m2","m3"]
}

  

ES删除索引

DELETE /product/_doc/3

  

ES修改索引

修改单个字段
POST /product/_update/3
{
  "doc": {
    "price": 1999
  }
}
全量替换 PUT /product/_doc/3 { "name":"wangwu", "desc":"huawei", "price":1999, "tag":["h1","h2","h3"] }

 

 根据查询条件修改

POST /tmc_demo/_update_by_query
{
  "query":{
    "term": {
      "name": "vivo"
    }
  },
  "script": {
    "source": "ctx._source['desc'] = 'oppo 高通骁龙75'"
  }
}

  

ES查询  

查询全部
GET /product/_search
根据id查询
GET /product/_doc/3

 

ES多条件查询

ElasticSearch之bool

1、must (must字段对应的是个列表,也就是说可以有多个并列的查询条件,一个文档满足各个子条件后才最终返回)

2、should (只要符合其中一个条件就返回)

3、must_not (与must相反,也就是说可以有多个并列的查询条件,一个文档各个子条件后才最终的结果都不满足)

4、filter(条件过滤查询,过滤条件的范围用range表示gt表示大于、lt表示小于、gte表示大于等于、lte表示小于等于)

GET /product/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "price": "1999"
          }
        },
        {
          "match": {
            "name": "wangwu"
          }
        }
      ]
    }
  }
}


GET /product/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "price": "1999"
          }
        },
        {
          "match": {
            "price": "9999"
          }
        }
      ]
    }
  }
}

GET /product/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "price": "1999"
          }
        },
        {
          "match": {
            "price": "3999"
          }
        }
      ]
    }
  }
}

  

GET /product/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "price": "1999"
          }
        }
        
      ]
    }
  }
}

GET /product/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "price": {
              "gte": 1999,
              "lte": 5000
            }
          }
        }
      ]
    }
  }
}

bool查询总结 

query,根据你的查询条件,去计算文档的匹配度得到一个分数,并且根据分数进行排序,不会做缓存的。

filter,根据你的查询条件去查询文档,不去计算分数,而且filter会对经常被过滤的数据进行缓存。

must:与关系,相当于关系型数据库中的 and。

should:或关系,相当于关系型数据库中的 or。

must_not:非关系,相当于关系型数据库中的 not。

filter:过滤条件。

range:条件筛选范围。

gt:大于,相当于关系型数据库中的 >。

gte:大于等于,相当于关系型数据库中的 >=。

lt:小于,相当于关系型数据库中的 <。

lte:小于等于,相当于关系型数据库中的 <=

 

 

 

 

 ES中match和term差别对比

term:精准查询,不进行分词

match:模糊查询,进行分词

fuzzy:模糊查询,不进行分词

match 会将关键词进行分词分成“my”和“cat”,查找时包含其中任一均可被匹配到。

 

filter 和must 区别

根据嵌套类型查询 (filter 与 must 是属于同一个级别的查询方式,都可以作为 query->bool 的属性)
filter: 不计算评分, 查询效率高;有缓存; (推荐)
  term: 精确匹配;
  match: 模糊匹配, 倒排索引;
must: 要计算评分,查询效率低;无缓存;
  term: 精确匹配 , 要评分;
  match:模糊匹配, 要评分;

 

ES查询高亮显示

GET /product/_search
{
  "query": {
    "match": {
      "desc": "vivo"
    }
  },
  "highlight": {
   "fields": {
      "desc": {}
    },
    "pre_tags": "<font color='red'>",
    "post_tags": "</font>",
    "fragment_size": 10
  }
}

  

在使用 match 查询的同时,加上一个highlight 属性:
pre_tags:前置标签
post_tags:后置标签
fields:需要高亮的字段
title:这里声明 title 字段需要高亮,后面可以为这个字段设置特有配置,也可以空

 

ES判断字段是否存在

GET /tmc_demo/_search
{
  "query": {
    "exists": {
      "field": "name"
    }
  }
}

 

ES模糊查询

GET /tmc_demo/_search
{
  "query": {
    "wildcard": {
      "name": {
        "value": "*"
      }
    }
  }
}

  

 

posted @ 2023-06-04 17:18  爵士灬  阅读(137)  评论(0编辑  收藏  举报