Elasticsearch常用搜索

多字段查找

multi_match

以下表达式等价于field1 = value1 or field2 = value1

GET index/type/_search
{
  "query": {
    "multi_match": {
      "query": "value1",
      "fields": ["field1","field2"]
    }
  }
  , "_source": ["field1","field2"]
}

布尔查询

must等价于and,must_not等价于not, should等价于or

以下表达式等价于( field1 = value1 or field2 = value2 ) and field3 != value3

GET index/type/_search
{
  "size": 100,
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "match": {
                  "field1": "value1"
                }
              },
              {
                "match": {
                  "field2": "value2"
                }
              }
            ]
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "field3": "value3"
          }
        }
      ]
    }
  },
  "_source": [
    "field1","field2","field3"
  ]
}

模糊查询

fuzziness

参考链接

GET index/type/_search
{
  "size": 100,
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "city": {
              "query": "Hoiliste",
              "fuzziness": "auto"
            }
          }
        }
      ]
    }
  },
  "_source": [
    "city"
  ]
}

通配符查询

*匹配零个或多个字符

GET index/type/_search
{
  "size": 100,
  "query": {
    "bool": {
      "must": [
        {
          "wildcard": {
            "field1": {
              "value": "wu*"
            }
          }
        }
      ]
    }
  },
  "_source": [
    "field1"
  ]
}

正则查询

regexp

GET index/type/_search
{
  "size": 100,
  "query": {
    "regexp":{
      "field1":"[a-zA-Z]+[0-9]*"
    }
  },
  "_source": [
    "field1"
  ]
}

范围查询

range

GET index/type/_search
{
  "size": 100,
  "query": {
    "range": {
      "field1": {
        "from": "2018-04-16 05:22:39",
        "to": "2018-06-23 05:22:39",
        "include_lower": true,
        "include_upper": true
      }
    }
  },
  "_source": [
    "field1"
  ]
}

排序

sort

GET index/type/_search
{
  "size": 100,
  "query": {
    "bool": {}
  },
  "sort": [
    {
      "field1": {
        "order": "desc"
      }
    }
  ],
  "_source": [
    "field1"
  ]
}

多重过滤

filter

GET index/type/_search
{
  "size": 100,
  "query": {
    "bool": {
      "filter": [
        {
          "bool": {
            "must": {
              "term": {
                "field1": "value1"
              }
            }
          }
        },
        {
          "bool": {
            "must": {
              "term": {
                "field2": "value1"
              }
            }
          }
        }
      ]
    }
  }, "_source": ["cityTerm", "mlsOrgId"]
}

脚本查询

参考链接

script
支持Groovy、java等多种语言的脚本

{
  "query": {
    "bool": {
      "filter": [
        {
          "bool": {
            "filter": {
              "script": {
                "script": {
                  "inline": "doc['field1'].value - doc['field2'].value > 0"
                }
              }
            }
          }
        }
      ]
    }
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值