elasticsearch学习9--Term-level queries简介、term query与terms query

1、Term-level queries 简介

Term-level queries 术语级查询就是根据结构化数据中的精确值查找文档。与 Full text queries全文查询的不同之处在于,术语级查询不会分析检索词,而是匹配存储在字段中的确切术语,简单的说就是检索词不会被分词

在介绍两种query之前先准备一下数据。创建blogs_index和tags_index,并添加数据。

PUT /blogs_index
{
  "settings": {
    "index": {
      "number_of_shards": 1,
      "number_of_replicas": 1
    }
  },
  "mappings": {
    "_doc": {
      "dynamic": false,
      "properties": {
        "id": {
          "type": "integer"
        },
        "author": {
          "type": "keyword"
        },
        "title": {
          "type": "text",
          "analyzer": "ik_smart"
        },
        "tag":{
           "type": "integer"
        },
        "influence": {
          "type": "integer_range"
        },
        "createAt": {
          "type": "date"
        }
      }
    }
  }
}
PUT /tags_index
{
  "settings": {
    "index": {
      "number_of_shards": 1,
      "number_of_replicas": 1
    }
  },
  "mappings": {
    "_doc": {
      "dynamic": false,
      "properties": {
        "id": {
          "type": "integer"
        },
        "tag_name": {
          "type": "keyword"
        }
      }
    }
  }
}

批量导入数据:

POST _bulk
{"index":{"_index":"blogs_index","_type":"_doc","_id":"1"}}
{"id":1,"author":"大衣哥","title":"关注我,系统学编程"}
{"index":{"_index":"blogs_index","_type":"_doc","_id":"2"}}
{"id":2,"author":"刘备","title":"系统学编程,关注我"}

2、term query

一般用于检索不会被分词的字段,主要是类型为:integer、keyword、boolean 的字段。因为会分词的字段会出现如下情况,这种情况往往是我们不想遇到的。

语句1:检索文档1的title字段的完整内容,发现居然检索不到文档!
POST /blogs_index/_doc/_search
{
  "query": {
    "term" : { "title" : "关注我,系统学编程" }
  }
}

语句2:只检索关键词“编程”,可以检索文档1和文档2
POST /blogs_index/_doc/_search
{
  "query": {
    "term" : { "title" : "编程" }
  }
}

结果分析:
由于使用的是ik_smart分词器,文档1 title 的Token有(关注,我,系统学,编程),文档2 title 的Token有(系统学,编程,关注,我)。
【语句一】term对检索词不分词,所以检索的Token为“关注我,系统学编程”,两个文档中都没有。
【语句二】这个比较简单,同样term对检索词不分词,检索的Token为“编程”。两个文档都有。

在检索不会分词的字段时,mathc语句与term语句效果一致

POST /blogs_index/_doc/_search
{
  "query": {
    "term" : { "author" : "大衣哥" }
  }
}
POST /blogs_index/_doc/_search
{
  "query": {
    "match" : { "author" : "大衣哥" }
  }
}

结果分析:
match语句会对检索词分词,使用的分词器默认与被检索字段一致。对于author这个字段,type为keyword,所以哪怕使用的是match查询,检索词依然不会被分词。上述两个语句都只能检索到文档1!

3、terms query

3.1 等价于mysql 的 in()

比如,我想检索作者是【大衣哥】和【刘备】的文章:

POST /blogs_index/_doc/_search
{
  "query": {
    "terms" : { "author" : ["大衣哥","刘备"]}
  }
}

该语句等价于sql语句【where author in (“方才兄”,“方才”)】

3.2 等价于mysql的联表查询–Terms lookup mechanism

添加如下数据:

POST _bulk
{"index":{"_index":"blogs_index","_type":"_doc","_id":"3"}}
{"id":3,"author":"大衣哥","title":"关注我,系统学编程","tag":[1,2,3]}
{"index":{"_index":"tags_index","_type":"_doc","_id":"1"}}
{"id":1,"tag_name":"这是标签1"}
{"index":{"_index":"tags_index","_type":"_doc","_id":"2"}}
{"id":2,"tag_name":"这是标签2"}
{"index":{"_index":"tags_index","_type":"_doc","_id":"3"}}
{"id":3,"tag_name":"这是标签3"}}

进行如下查询

POST /tags_index/_search
{
  "query": {
    "terms": {
      "id": {
        "index": "blogs_index",
        "type": "_doc",
        "id": "3",
        "path": "tag"
      }
    }
  }
}

index:获取id的index
id:文档的编号(_id的值)
path:查询的字段
上述语句等价于select * from tags_index where id in (select tag from blogs_index where _id = 3)所以能检索到tags_index的文档1,2,3

注意:针对另一个index的查询条件,只能是 _id = xx,不能像sql一样随意书写where条件。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值