Skip to content

Latest commit

 

History

History
159 lines (141 loc) · 4.41 KB

012.Elasticsearch中使用Term Filter来过滤数据.md

File metadata and controls

159 lines (141 loc) · 4.41 KB

Elasticsearch中使用Term Filter来过滤数据

  • 查看准备数据
POST /forum/article/_bulk
{ "index": { "_id": 1 }}
{ "articleID" : "XHDK-A-1293-#fJ3", "userID" : 1, "hidden": false, "postDate": "2017-01-01" }
{ "index": { "_id": 2 }}
{ "articleID" : "KDKE-B-9947-#kL5", "userID" : 1, "hidden": false, "postDate": "2017-01-02" }
{ "index": { "_id": 3 }}
{ "articleID" : "JODL-X-1937-#pV7", "userID" : 2, "hidden": false, "postDate": "2017-01-01" }
{ "index": { "_id": 4 }}
{ "articleID" : "QQPX-R-3956-#aD8", "userID" : 2, "hidden": true, "postDate": "2017-01-02" }

ES中使用Term Filter过滤数据

  • 查看索引mapping结构
GET /forum/_mapping/article

查看索引mapping结构

  • 使用用户ID过滤数据查询
{
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "userID": 1
        }
      }
    }
  }
}

使用用户ID过滤数据查询

  • 查询没有隐藏的帖子
{
  "query":{
    "constant_score":{
      "filter":{
        "term":{
          "hidden":false
        }
      }
    }
  }
}

查询没有隐藏的帖子

  • 根据帖子ID查询帖子
{
  "query":{
    "constant_score":{
      "filter":{
        "term":{
          "articleID":"QQPX-R-3956-#aD8"
        }
      }
    }
  }
}

根据帖子ID查询帖子

  • 根据帖子ID查询帖子没有结果的分析mapping

根据帖子ID查询帖子没有结果的分析mapping

  • 根据articleID的关键字来进行查询
{
  "query":{
    "constant_score":{
      "filter":{
        "term":{
          "articleID":"QQPX-R-3956-#aD8"
        }
      }
    }
  }
}

articleID.keyword,是es最新版本内置建立的field,就是不分词的。所以一个articleID过来的时候,会建立两次索引,一次是自己本身,是要分词的, 分词后放入倒排索引;另外一次是基于articleID.keyword,不分词,保留256个字符最多,直接一个字符串放入倒排索引中。 根据articleID的关键字来进行查询

  • 重建索引-articleID不分词
{
	"mappings": {
    "article": {
      "properties": {
        "articleID": {
          "type": "keyword"
        }
      }
    }
  }
}

重建索引-aricleID不分词

  • 数据初始化
POST /forum/article/_bulk
{ "index": { "_id": 1 }}
{ "articleID" : "XHDK-A-1293-#fJ3", "userID" : 1, "hidden": false, "postDate": "2017-01-01" }
{ "index": { "_id": 2 }}
{ "articleID" : "KDKE-B-9947-#kL5", "userID" : 1, "hidden": false, "postDate": "2017-01-02" }
{ "index": { "_id": 3 }}
{ "articleID" : "JODL-X-1937-#pV7", "userID" : 2, "hidden": false, "postDate": "2017-01-01" }
{ "index": { "_id": 4 }}
{ "articleID" : "QQPX-R-3956-#aD8", "userID" : 2, "hidden": true, "postDate": "2017-01-02" }

数据初始化

  • 重建索引后查看mapping关系

重建索引后查看mapping关系

  • 再次使用articleID搜索帖子
{
  "query":{
    "constant_score":{
      "filter":{
        "term":{
          "articleID":"QQPX-R-3956-#aD8"
        }
      }
    }
  }
}

再次使用articleID搜索帖子

  • 再次查看articleID的分词结果
GET /forum/_analyze
{
  "field": "articleID",
  "text": "XHDK-A-1293-#fJ3"
}

查看分词结果