ES之常用踩坑用法

1. 分词器

ik_smart:最少拆分
ik_max_word:最细切分

1.1 区别

ik_smart

GET _analyze
{
  "analyzer": "ik_smart",
  "text": "张三老铁学习java!"
}
{
  "tokens" : [
    {
      "token" : "张",
      "start_offset" : 0,
      "end_offset" : 1,
      "type" : "CN_CHAR",
      "position" : 0
    },
    {
      "token" : "三老",
      "start_offset" : 1,
      "end_offset" : 3,
      "type" : "CN_WORD",
      "position" : 1
    },
    {
      "token" : "铁",
      "start_offset" : 3,
      "end_offset" : 4,
      "type" : "CN_CHAR",
      "position" : 2
    },
    {
      "token" : "学习",
      "start_offset" : 4,
      "end_offset" : 6,
      "type" : "CN_WORD",
      "position" : 3
    },
    {
      "token" : "java",
      "start_offset" : 6,
      "end_offset" : 10,
      "type" : "ENGLISH",
      "position" : 4
    }
  ]
}

ik_max_word

GET _analyze
{
  "analyzer": "ik_max_word",
  "text": "张三老铁学习java!"
}
{
  "tokens" : [
    {
      "token" : "张三",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "三老",
      "start_offset" : 1,
      "end_offset" : 3,
      "type" : "CN_WORD",
      "position" : 1
    },
    {
      "token" : "三",
      "start_offset" : 1,
      "end_offset" : 2,
      "type" : "TYPE_CNUM",
      "position" : 2
    },
    {
      "token" : "老",
      "start_offset" : 2,
      "end_offset" : 3,
      "type" : "CN_CHAR",
      "position" : 3
    },
    {
      "token" : "铁",
      "start_offset" : 3,
      "end_offset" : 4,
      "type" : "CN_CHAR",
      "position" : 4
    },
    {
      "token" : "学习",
      "start_offset" : 4,
      "end_offset" : 6,
      "type" : "CN_WORD",
      "position" : 5
    },
    {
      "token" : "java",
      "start_offset" : 6,
      "end_offset" : 10,
      "type" : "ENGLISH",
      "position" : 6
    }
  ]
}

2. ES 基本语法应用

2.1 精确查询

GET _search
{
	"query": {
		"term": {
			"id": "1"
		}
	}
}
{
  "took" : 15,
  "timed_out" : false,
  "_shards" : {
    "total" : 23,
    "successful" : 23,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.6931471,
    "hits" : [
      {
        "_index" : "article",
        "_type" : "articleentity",
        "_id" : "1",
        "_score" : 0.6931471,
        "_source" : {
          "id" : "1",
          "title" : "liuping",
          "content" : "忐忐忑忑",
          "userId" : 0,
          "createTime" : 1652083927471
        }
      },
      {
        "_index" : "product",
        "_type" : "product",
        "_id" : "1",
        "_score" : 0.6931471,
        "_source" : {
          "id" : "1",
          "title" : "小米2青春版手机",
          "price" : 1799.99,
          "origin" : "广东省深圳市",
          "brandId" : "3",
          "brandName" : "小米",
          "keyword" : "小米2青春版手机"
        }
      }
    ]
  }
}

 2.2 模糊查询

GET _search
{
  "query": {
    "bool": {
      "filter": [
        {
          "wildcard": {
            "id": "*1*"
          }
        }
      ]
    }
  }
}
{
  "took" : 53,
  "timed_out" : false,
  "_shards" : {
    "total" : 23,
    "successful" : 23,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "product",
        "_type" : "product",
        "_id" : "145454",
        "_score" : 0.0,
        "_source" : {
          "id" : "145454",
          "title" : "999",
          "price" : 0.0,
          "origin" : "string",
          "brandId" : "7778787",
          "brandName" : "7778877",
          "keyword" : "string"
        }
      },
      {
        "_index" : "article",
        "_type" : "articleentity",
        "_id" : "1",
        "_score" : 0.0,
        "_source" : {
          "id" : "1",
          "title" : "liuping",
          "content" : "忐忐忑忑",
          "userId" : 0,
          "createTime" : 1652083927471
        }
      },
      {
        "_index" : "product",
        "_type" : "product",
        "_id" : "1",
        "_score" : 0.0,
        "_source" : {
          "id" : "1",
          "title" : "小米2青春版手机",
          "price" : 1799.99,
          "origin" : "广东省深圳市",
          "brandId" : "3",
          "brandName" : "小米",
          "keyword" : "小米2青春版手机"
        }
      }
    ]
  }
}

3.index

与mysql对比

	    "sdate": 
			{
			"type": "integer",
     			"index": false
			}

 说明:如果index是false,则说明不支持查询,默认是true

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值