DSL-- Term关键字

The term query finds documents that contain the exact term specified in the inverted index. For instance:

{
    "term" : { "user" : "Kimchy" } 
}

Finds documents which contain the exact term Kimchy in the inverted index of the user field.

boost参数

boost参数可以对查找的document数据加权, 提高score

GET /_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "status": {
              "value": "urgent",
              "boost": 2.0 
            }
          }
        },
        {
          "term": {
            "status": "normal" 
          }
        }
      ]
    }
  }
}

默认的权值是1.0,
但是urgent关键字的权值会被设置为2.0,
The urgent query clause has a boost of 2.0, meaning it is twice as important as the query clause for normal.
The normal clause has the default neutral boost of 1.0.

//定义一个索引
PUT my_index
{
  "mappings": {
    "my_type": {
      "properties": {
        "full_text": {
          "type":  "string" 
        },
        "exact_value": {
          "type":  "string",
          "index": "not_analyzed" 
        }
      }
    }
  }
}

The full_text field is analyzed by default.
The exact_value field is set to be not_analyzed.

//添加一条记录
PUT my_index/my_type/1
{
  "full_text":   "Quick Foxes!", 
  "exact_value": "Quick Foxes!"  
}

The full_text inverted index will contain the terms: [quick, foxes].
The exact_value inverted index will contain the exact term: [Quick Foxes!].

可以看到full_text字段默认会被分词分析, 所以”Quick Foxes!”, 会被拆分为两个单词[quick, foxes]
exact_value字段在mapping时被设置为”not_analyzed”, 则不会被分词所以分析, 所以会作为一个整体[Quick Foxes!].被记录

示例:


//匹配: because the exact_value field contains the exact term Quick Foxes!.
GET my_index/my_type/_search
{
  "query": {
    "term": {
      "exact_value": "Quick Foxes!" 
    }
  }
}


//不匹配, 因为full_text只是匹配quick,foxes, 而不是完整的term "Quick Foxes!"
GET my_index/my_type/_search
{
  "query": {
    "term": {
      "full_text": "Quick Foxes!" 
    }
  }
}


//匹配
GET my_index/my_type/_search
{
  "query": {
    "term": {
      "full_text": "foxes" 
    }
  }
}

//匹配, 注意此处使用的是match, match会首先分析查询条件, 然后去搜索两个分词quick, foxes
//This match query on the full_text field first analyzes the query string, then looks for documents containing quick or foxes or both.

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

引自:
https://www.elastic.co/guide/en/elasticsearch/reference/2.1/query-dsl-term-query.html#query-dsl-term-query

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值