欢迎投稿

今日深度:

Elasticsearch-terms搜索及结果优化,

Elasticsearch-terms搜索及结果优化,


测试数据:

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" }

POST /forum/article/_bulk
{ "update": { "_id": "1"} }
{ "doc" : {"tag" : ["java", "hadoop"]} }
{ "update": { "_id": "2"} }
{ "doc" : {"tag" : ["java"]} }
{ "update": { "_id": "3"} }
{ "doc" : {"tag" : ["hadoop"]} }
{ "update": { "_id": "4"} }
{ "doc" : {"tag" : ["java", "elasticsearch"]} }
  • 搜索articleID=KDKE-B-9947-#kL5或者QQPX-R-3956-#aD8的doc
GET /forum/article/_search
{
 "query": {
   "constant_score": {
     "filter": {
       "terms": {
         "articleID": [
           "KDKE-B-9947-#kL5",
           "QQPX-R-3956-#aD8"
         ]
       }
     }
   }
 }
}
  • 搜索tags中有java的帖子
GET /forum/article/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "terms": {
          "tag": ["java"]
        }
      }
    }
  }
}
  • 对搜索到的结果进行优化,只想搜索到tag中只有java的doc:
POST /forum/article/_bulk
{ "update": { "_id": "1"} }
{ "doc" : {"tag_cnt" : 2} }
{ "update": { "_id": "2"} }
{ "doc" : {"tag_cnt" : 1} }
{ "update": { "_id": "3"} }
{ "doc" : {"tag_cnt" : 1} }
{ "update": { "_id": "4"} }
{ "doc" : {"tag_cnt" : 2} }

添加一个字段表示tag中的数量

GET /forum/article/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "bool": {
          "must": [
            {
              "terms": {
                "tag": ["java"]
              }
            },
            {
              "term": {
                "tag_cnt": 1
              }
            }
          ]
        }
      }
    }
  }
}

就可以查询出我们想要的结果

www.htsjk.Com true http://www.htsjk.com/Elasticsearch/34655.html NewsArticle Elasticsearch-terms搜索及结果优化, 测试数据: POST /forum/article/_bulk{ "index" : { "_id" : 1 }}{ "articleID" : "XHDK-A-1293-#fJ3" , "userID" : 1 , "hidden" : false , "postDate" : "2017-01-01" }{ "index" : { "_id" : 2 }}{ "ar...
相关文章
    暂无相关文章
评论暂时关闭