term,match,match_phrase,query_string,simple_query_string的区别

term,match,match_phrase,query_string,simple_query_string区别

数据准备

设置mapping:

PUT /book
{
   
  "mappings": {
   
    "properties": {
   
      "title": {
   
        "type": "keyword"
      },
      "content": {
   
        "type": "text"
      }
    }
  }
}

插入两条数据:

POST /book/_doc/1
{
   
  "title":"three kingdoms",
  "content":"the three kingdoms period started with the end of the Han dynasty"
}

POST /book/_doc/2
{
   
  "title":"journey to the west",
  "content":"awakened to emptiness"
}

查看_mapping:

GET /book/_mapping

{
   
  "book" : {
   
    "mappings" : {
   
      "properties" : {
   
        "content" : {
   
          "type" : "text"
        },
        "title" : {
   
          "type" : "keyword"
        }
      }
    }
  }
}

查看两条document分词结果:

POST /book/_analyze
{
   
  "field": "title",
  "text": "three kingdoms"
}

_analyze结果: 可见 title 是没有被分词的

{
   
  "tokens" : [
    {
   
      "token" : "three kingdoms",
      "start_offset" : 0,
      "end_offset" : 14,
      "type" : "word",
      "position" : 0
    }
  ]
}
POST /book/_analyze
{
   
  "field": "content",
  "text": "awakened to emptiness"
}

_analyze结果: text会被分词

{
   
  "tokens" : [
    {
   
      "token" : "awakened",
      "start_offset" : 0,
      "end_offset" : 8,
      "type" : "<ALPHANUM>",
      "position" : 0
    },
    {
   
      "token" : "to",
      "start_offset" : 9,
      "end_offset" : 11,
      "type" : "<ALPHANUM>",
      "position" : 1
    },
    {
   
      "token" : "emptiness",
      "start_offset" : 12,
      "end_offset" : 21,
      "type" : "<ALPHANUM>",
      "position" : 2
    }
  ]
}

term

term查询keyword

term是将传入的文本原封不动地(不分词)拿去查询,keyword字段不分词,需要完全匹配才可查处结果

查询一:

GET /book/_search
{
   
  "query": {
   
    "term": {
   
      "title": "kingdoms"
    }
  }
}
结果:
{
   
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
   
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
   
    "total" : {
   
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

查询二:

GET /book/_search
{
   
  "query": {
   
    "term": {
   
      "title": "three kingdoms"
    }
  }
}
结果:
{
   
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
   
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
   
    "total" : {
   
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.6931471,
    "hits" : [
      {
   
        "_index" : "book",
        "_type" : "_doc",
        "_id"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值