Elasticsearch-常用查询举例

关键记忆

term,terms,constant_score,filter,
bool,must,should,must_not,
gt,lte,gte,lte,||+1M,
exists,missing

查询语句

term查询数字

SELECT document
FROM   products
WHERE  price = 20

 

{
    "term" : {
        "price" : 20
    }
}

term文本查询 

 

SELECT product
FROM   products
WHERE  productID = "XHDK-A-1293-#fJ3"

 

{
    "query" : {
        "constant_score" : {
            "filter" : {
                "term" : {
                    "productID" : "XHDK-A-1293-#fJ3"
                }
            }
        }
    }
}

 

{
    "query" : {
        "constant_score" : { 
            "filter" : {
                "term" : { 
                    "price" : 20
                }
            }
        }
    }
}

布尔过滤器

{
   "bool" : {
      "must" :     [],
      "should" :   [],
      "must_not" : [],
   }
}

must

所有的语句都 必须(must) 匹配,与 AND 等价。

must_not

所有的语句都 不能(must not) 匹配,与 NOT 等价。

should

至少有一个语句要匹配,与 OR 等价。

{
   "query" : {
      "filtered" : { 
         "filter" : {
            "bool" : {
              "should" : [
                 { "term" : {"price" : 20}}, 
                 { "term" : {"productID" : "XHDK-A-1293-#fJ3"}} 
              ],
              "must_not" : {
                 "term" : {"price" : 30} 
              }
           }
         }
      }
   }
}

嵌套布尔过滤器

SELECT document
FROM   products
WHERE  productID      = "KDKE-B-9947-#kL5"
  OR (     productID = "JODL-X-1937-#pV7"
       AND price     = 30 )
{
   "query" : {
      "filtered" : {
         "filter" : {
            "bool" : {
              "should" : [
                { "term" : {"productID" : "KDKE-B-9947-#kL5"}}, 
                { "bool" : { 
                  "must" : [
                    { "term" : {"productID" : "JODL-X-1937-#pV7"}}, 
                    { "term" : {"price" : 30}} 
                  ]
                }}
              ]
           }
         }
      }
   }
}

 

查找多个精确值

{
    "terms" : {
        "price" : [20, 30]
    }
}
GET /my_store/products/_search
{
    "query" : {
        "constant_score" : {
            "filter" : {
                "terms" : { 
                    "price" : [20, 30]
                }
            }
        }
    }
}

范围

SELECT document
FROM   products
WHERE  price BETWEEN 20 AND 40
"range" : {
    "price" : {
        "gte" : 20,
        "lte" : 40
    }
}
  • gt> 大于(greater than)
  • lt< 小于(less than)
  • gte>= 大于或等于(greater than or equal to)
  • lte<= 小于或等于(less than or equal to)

日期范围 

"range" : {
    "timestamp" : {
        "gt" : "2014-01-01 00:00:00",
        "lt" : "2014-01-07 00:00:00"
    }
}

+一个月 

"range" : {
    "timestamp" : {
        "gt" : "2014-01-01 00:00:00",
        "lt" : "2014-01-01 00:00:00||+1M" 
    }
}

字符串范围

"range" : {
    "title" : {
        "gte" : "a",
        "lt" :  "b"
    }
}

存在查询

 

SELECT tags
FROM   posts
WHERE  tags IS NOT NULL
{
    "query" : {
        "constant_score" : {
            "filter" : {
                "exists" : { "field" : "tags" }
            }
        }
    }
}

缺失查询

SELECT tags
FROM   posts
WHERE  tags IS NULL
{
    "query" : {
        "constant_score" : {
            "filter": {
                "missing" : { "field" : "tags" }
            }
        }
    }
}

 

你可能感兴趣的:(Elasticsearch)