elasticsearch支持相似與sql語句的查詢表達式

  1. 寫在以前
    ElasticSearch是一個基於Lucene的搜索服務器。它提供了一個分佈式多用戶能力的全文搜索引擎,基於RESTful web接口。Elasticsearch是用Java開發的,並做爲Apache許可條款下的開放源碼發佈,是當前流行的企業級搜索引擎。設計用於雲計算中,可以達到實時搜索,穩定,可靠,快速,安裝使用方便。
    在以前在項目開發中的不一樣的查詢條件都須要單獨些Java bean去封裝查詢語句,後面就想能不能讓es也支持相似與sql where name=xxx and age=20這類的過濾條件。因此在這兒就寫了個解析表達式同時生成es可以支持的query dsl的小工具 支持的操做符號有==,!= ,<,>,>=,<= 同時還支持加括號增長查詢條件優先級的功能。
  2. 實現
    源碼中的conditionNode用到了二叉樹的結構,表達不是很清楚,直接上個列子吧,好比我要查詢的表達式爲:git

    name==yang and age==20

    生成的conditionNode 的json結構爲github

    {
    "op":"eq",
    "type":"normal",
    "left":{
        "field":"name",
        "value":"yang",
        "op":"eq",
        "type":"normal"
    },
    "right":{
        "field":"age",
        "value":"20",
        "op":"eq",
        "type":"normal"
    },
    "relation":"and"
    }

    能夠看到在最外層的conditionNode的左右兩個節點封裝單獨的兩個conditionNode ,且其關係爲and,最後將 解析後的condition生成query dsl:web

    {
      "bool" : {
    "must" : [
      {
        "bool" : {
          "must" : [
            {
              "query_string" : {
                "query" : "name:\"yang\""
              }
            }
          ],
          "disable_coord" : false,
          "adjust_pure_negative" : true,
          "boost" : 1.0
        }
      },
      {
        "bool" : {
          "must" : [
            {
              "query_string" : {
                "query" : "age:\"20\"",
                
              }
            }
          ],
          "disable_coord" : false,
          "adjust_pure_negative" : true,
          "boost" : 1.0
        }
      }
    ],
    "disable_coord" : false,
    "adjust_pure_negative" : true,
    "boost" : 1.0
      }
    }

    若是隻是支持順序解析倒也沒有什麼特別的,這裏舉個添加括號提升查詢條件優先級的列子:
    expression : (name==yang and age>20) or (name == wang and age<=18)
    解析後的conditionNode爲:sql

    {
    "op":"eq",
    "type":"normal",
    "left":{
        "op":"eq",
        "type":"normal",
        "left":{
            "field":"name",
            "value":"yang",
            "op":"eq",
            "type":"normal"
        },
        "right":{
            "field":"age",
            "value":"20",
            "op":"gte",
            "type":"normal"
        },
        "relation":"and"
    },
    "right":{
        "op":"eq",
        "type":"normal",
        "left":{
            "field":"name",
            "value":"wang",
            "op":"eq",
            "type":"normal"
        },
        "right":{
            "field":"age",
            "value":"18",
            "op":"lte",
            "type":"normal"
        },
        "relation":"and"
    },
    "relation":"or"
    }

    最後根據該conditionNode生成的dsl語句爲:express

    {
      "bool" : {
    "should" : [
      {
        "bool" : {
          "must" : [
            {
              "bool" : {
                "must" : [
                  {
                    "query_string" : {
                      "query" : "name:\"wang\""
                    }
                  }
                ],
                "disable_coord" : false,
                "adjust_pure_negative" : true,
                "boost" : 1.0
              }
            },
            {
              "bool" : {
                "must" : [
                  {
                    "range" : {
                      "age" : {
                        "from" : null,
                        "to" : "18",
                        "include_lower" : true,
                        "include_upper" : true,
                        "boost" : 1.0
                      }
                    }
                  }
                ],
                "disable_coord" : false,
                "adjust_pure_negative" : true,
                "boost" : 1.0
              }
            }
          ],
          "disable_coord" : false,
          "adjust_pure_negative" : true,
          "boost" : 1.0
        }
      },
      {
        "bool" : {
          "must" : [
            {
              "bool" : {
                "must" : [
                  {
                    "query_string" : {
                      "query" : "name:\"yang\""
                    }
                  }
                ],
                "disable_coord" : false,
                "adjust_pure_negative" : true,
                "boost" : 1.0
              }
            },
            {
              "bool" : {
                "must" : [
                  {
                    "range" : {
                      "age" : {
                        "from" : "20",
                        "to" : null,
                        "include_lower" : false,
                        "include_upper" : true,
                        "boost" : 1.0
                      }
                    }
                  }
                ],
                "disable_coord" : false,
                "adjust_pure_negative" : true,
                "boost" : 1.0
              }
            }
          ],
          "disable_coord" : false,
          "adjust_pure_negative" : true,
          "boost" : 1.0
        }
      }
    ],
    "disable_coord" : false,
    "adjust_pure_negative" : true,
    "boost" : 1.0
      }
    }
冗餘的東西有點多,你們將就着看吧,這裏貼上源碼地址

https://github.com/jacobyangs...json

相關文章
相關標籤/搜索