Elasticsearch的DSL之 term and match

记录学习ES的DSL的一些比较重要的概念和方法。


term VS match


首先要说的是, 一个doc进入ES被索引,哪些字段(fields)被索引是可以自定义的。 默认,ES会对所有的fields进行索引。

PUT /my_index
{
  "mappings": {
    "my_type": {
      "properties": {
        "status_code": {
          "type": "string",
          "index": "not_analyzed"
        }
      }
    }
  }
}


这是个简单的mapping, 有个status_code字段,类型是string, index的方式是“not_analyzed”。 ES中某个字段的index方式有三种值: no/not_analyzed/analyzed, 对应的意思是:

no = > 该字段不进入index, 即不可对该字段进行query。

not_analyzed => 该字段作为一个term进入index,不进行分词等analyse操作。对除string类型之外的fields, 这个是默认的。对string类型的字段,默认要进行分词的,除非指定不进行分词(即not_analyzed) 。

analyzed =》 只对string类型的field有效, 且是默认的。 通过analyzer, 该field会被切分成若干的分词(terms), 


再来说,term和match

term是用来进行分词精确匹配的(The term query finds documents that contain the exact term specified in the inverted index.)

match是用来进行全文检索的(full-text search)。


看下面的一个例子, 就能够很清楚的知道二者的区别了。例子来自https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html


myindex下的一个类型是my_type的mapping:

PUT my_index
{
  "mappings": {
    "my_type": {
      "properties": {
        "full_text": {
          "type":  "string" 
        },
        "exact_value": {
          "type":  "string",
          "index": "not_analyzed" 
        }
      }
    }
  }
}

PUT my_index/my_type/1
{
  "full_text":   "Quick Foxes!", 
  "exact_value": "Quick Foxes!"  
}



The full_text field is analyzed by default.


The exact_value field is set to be not_analyzed.


The full_text inverted index will contain the terms: [quickfoxes].


The exact_value inverted index will contain the exact term: [Quick Foxes!].

Now, compare the results for the term query and the match query:

(1)GET my_index/my_type/_search
{
  "query": {
    "term": {
      "exact_value": "Quick Foxes!"
    }
  }
}

(2)GET my_index/my_type/_search
{
  "query": {
    "term": {
      "full_text": "Quick Foxes!" 
    }
  }
}

(3)GET my_index/my_type/_search
{
  "query": {
    "term": {
      "full_text": "foxes" 
    }
  }
}

(4)GET my_index/my_type/_search
{
  "query": {
    "match": {
      "full_text": "Quick Foxes!" 
    }
  }
}

This query matches because the exact_value field contains the exact term Quick Foxes!.

This query does not match, because the full_text field only contains the terms quick andfoxes. It does not contain the exact term Quick Foxes!.

term query for the term foxes matches the full_text field.

This match query on the full_text field first analyzes the query string, then looks for documents containing quick or foxes or both.

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值