Elasticsearch match和term查询区别

order索引包含数据

GET order/_search
{
  "_source": ["order_name"]
}
查询结果
"hits" : [
  {
    "_index" : "order",
    "_type" : "_doc",
    "_id" : "1",
    "_score" : 1.0,
    "_source" : {
      "order_name" : "华为"
    }
  },
  {
    "_index" : "order",
    "_type" : "_doc",
    "_id" : "2",
    "_score" : 1.0,
    "_source" : {
      "order_name" : "华为mate60"
    }
  },
  {
    "_index" : "order",
    "_type" : "_doc",
    "_id" : "3",
    "_score" : 1.0,
    "_source" : {
      "order_name" : "华为P60"
    }
  }
]


1.1 term加上字段不分词查询
GET order/_search
{
  "query": {
    "term": {
      "order_name.keyword": {
        "value": "华为"
      }
    }
  }
  , "_source": ["order_name"]
}
查询结果:
"hits" : [
  {
    "_index" : "order",
    "_type" : "_doc",
    "_id" : "1",
    "_score" : 0.9808291,
    "_source" : {
      "order_name" : "华为"
    }
  }
]	


1.2 term分词查询
GET order/_search
{
  "query": {
    "term": {
      "order_name": {
        "value": "华为"
      }
    }
  }
  , "_source": ["order_name"]
}
查询结果=> 因为用的默认standard分词器,中文会逐字拆分 所以没查到
"hits" : [ ]    


2.1 match 分词查询
GET order/_search
{
  "query": {
    "match": {
      "order_name":"华为"
    }
  }
  , "_source": ["order_name"]
}

查询结果是三条数据都匹配查询出来了


2.2 match不分词查询
GET order/_search
{
  "query": {
    "match": {
      "order_name.keyword":"华为"
    }
  }
  , "_source": ["order_name"]
}

查询结果
"hits" : [
  {
    "_index" : "order",
    "_type" : "_doc",
    "_id" : "1",
    "_score" : 0.9808291,
    "_source" : {
      "order_name" : "华为"
    }
  }
]


3. standard分词器例子:

GET order/_analyze
{
  "text": ["华为"]
}
分词结果:
{
  "tokens" : [
    {
      "token" : "华",
      "start_offset" : 0,
      "end_offset" : 1,
      "type" : "",
      "position" : 0
    },
    {
      "token" : "为",
      "start_offset" : 1,
      "end_offset" : 2,
      "type" : "",
      "position" : 1
    }
  ]
}


你可能感兴趣的:(Elasticsearch,elasticsearch)