ES 自己做的例子彙總

 

 

首先初始化數據

POST /forums/articles/_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 /forums/articles/_bulk
{ "index": { "_id": 5 }}
{ "articleID" : "XHDK-C-1293-#fJ3", "userID" : 2, "hidden": false, "postDate": "2017-02-01" }
{ "index": { "_id": 6 }}
{ "articleID" : "KDKE-D-9947-#kL5", "userID" : 3, "hidden": false, "postDate": "2017-02-02" }
{ "index": { "_id": 7 }}
{ "articleID" : "JODL-E-1937-#pV7", "userID" : 4, "hidden": false, "postDate": "2017-02-01" }
{ "index": { "_id": 8 }}
{ "articleID" : "QQPX-F-3956-#aD8", "userID" : 5, "hidden": true, "postDate": "2017-03-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"]} }
{ "update": { "_id": "5"} }
{ "doc" : {"tag" : ["java", "hadoop"]} }
{ "update": { "_id": "6"} }
{ "doc" : {"tag" : ["java","mysql"]} }
{ "update": { "_id": "7"} }
{ "doc" : {"tag" : ["hadoop"]} }
{ "update": { "_id": "8"} }
{ "doc" : {"tag" : ["java", "elasticsearch","mysql"]} }

第一個簡單的查詢 查詢postDate 在2017-01-01的帖子

GET /forums/articles/_search
{
  "query": {
    "constant_score": {
      "filter": {
          "bool": {
            "must": [
              { "term": {
                "postDate": "2017-01-01"
              } }
            ]
          }
      },
      "boost": 1.2
    }
  }
}

第二個查詢,查詢查詢postDate 在2017-01-01的帖子或者是用戶userid=2發的帖子

POST /forums/articles/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "bool": {
          "should": [
            {
              "term": {
                "postDate": "2017-01-01"
              }
            },
            {
              "term": {
                "userID": 2
              }
            }
          ]
        }
      }
    }
  }
}

第三個查詢,查詢用戶userid=2 在2017-01-01和2017-01-01發的帖子

POST /forums/articles/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "bool": {
          "must": {
            "term": {
              "userID": 2
            }
          },
          "should": [
            {
              "term": {
                "postDate": "2017-01-01"
              }
            },
            {
              "term": {
                "postDate": "2017-01-02"
              }
            }
          ]
        }
      }
    }
  }
}

第4個需求  查詢tag中包含java和hadoop的文章

POST /forums/articles/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "bool": {
          "must": [
            {
              "terms": {
                "tag": [
                  "java",
                  "hadoop"
                ]
              }
            }
          ]
        }
      }
    }
  }
}

第5個需求,我們要查詢tag只是java的數據

這時原有數據如果統計的話比較麻煩,我們更新一下原有數據,添加一個新的字段tag_cnt用來表示tag數量,這樣只需要查tag中含有java並且tag_cnt=1的數據即可。

POST /forums/articles/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "bool": {
          "must": [
            {
              "term": {
                "tag_cnt": 1
              }
            },
            {
              "term": {
                "tag": "java"
              }
            }
          ]
        }
      }
    }
  }
}

第6個需求  添加了字段view_cnt用來表示訪問次數,查詢訪問次數在40到80的文章並且發帖日期在2017年2月1日後

 

POST /forums/articles/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "bool": {
          "must": [
            {
              "range": {
                "view_cnt": {
                  "gte": 40,
                  "lte": 80
                }
              }
            },
            {
              "range": {
                "postDate": {
                  "gte": "2017-02-01"
                }
              }
            }
          ]
        }
      }
    }
  }
}
postDate": {
                  "gte": "2017-01-01||+30d"
                }

上面這種是針對日期類型的一種用法。

第7個需求:查詢title中多個關鍵詞的情況,比如java hadoop mysql 或者elasticsearch中出現3次的

GET /forums/articles/_search
{
  "query": {
    "bool": {
      "should": [
        {"match": {
          "title": "java"
        }},
        {"match": {
          "title": "elasticsearch"
        }},
        {
          "match": {
            "title": "hadoop"
          }
        },
        {"match": {
          "title": "mysql"
        }}
      ],
        "minimum_should_match": 3
    }
  }
}

boost 可以進行細粒度的權重控制。

muti_query的例子,先更新數據

POST /forums/articles/_bulk
{ "update": { "_id": "1"} }
{ "doc" : {"content" : "i like to write best elasticsearch article"} }
{ "update": { "_id": "2"} }
{ "doc" : {"content" : "i think java is the best programming language"} }
{ "update": { "_id": "3"} }
{ "doc" : {"content" : "i am only an elasticsearch beginner"} }
{ "update": { "_id": "4"} }
{ "doc" : {"content" : "elasticsearch and hadoop are all very good solution, i am a beginner"} }
{ "update": { "_id": "5"} }
{ "doc" : {"content" : "spark is best big data solution based on scala ,an programming language similar to java"} }

按單個字段的結果優先排序

POST /forums/articles/_search
{
  "query": {
    "dis_max": {
      "queries": [
        {
          "match": {
            "title": "java solution"
          }
        },
        {
          "match": {
            "content": "java solution"
          }
        }
      ]
    }
  }
}

加入字段權重 同時加入 tie_breaker進行score分值折中 ,title權重爲2

POST /forums/articles/_search
{
  "query": {
    "dis_max": {
      "queries": [
        {
          "match": {
            "title": { "query": "java solution",
            "minimum_should_match": "50%",
            "boost" : 2
            }
            
          }
        },
        {
          "match": {
            "content": "java solution"
          }
        }
      ],
      "tie_breaker": 0.7
    }
  }
}

cross_fields的

POST /forums/articles/_search
{
  "query": {
    "multi_match": {
      "query": "Peter Smith",
      "type": "cross_fields",
      "operator" : "and",
      "fields": [
        "new_author_first_name",
        "new_author_last_name"
      ]
    }
  }
}

 

發佈了102 篇原創文章 · 獲贊 24 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章