Elastic Search之DSL查询语句


Elasticsearch提供了基于JSON的DSL( Domain Specific Language)来定义查询。常见的查询类型包括:

  • 查询所有:查询出所有数据,一般测试用。例如:match_all

  • 全文检索(full text)查询:利用分词器对用户输入内容分词,然后去倒排索引库中匹配。例如:

    • match_query
    • multi_match_query
  • 精确查询:根据精确词条值查找数据,一般是查找keyword、数值、日期、boolean等类型字段。例如:

    • ids
    • range
    • term
  • 地理(geo)查询:根据经纬度查询。例如:

    • geo_distance
    • geo_bounding_box
  • 复合(compound)查询:复合查询可以将上述各种查询条件组合起来,合并查询条件。例如:

    • bool
    • function_score

本文根据张虎翼老师的SpringCloud中ES的案例进行举例
查询索引库中文档的语句如下

查询所有
GET /hotel/_search
{
  "query": {
    "match_all": {}
  }
}
全文查询
match
GET /hotel/_search
{
  "query": {
    "match": {
      "name": "如家"
    }
  }
}
multi_match
GET /hotel/_search
{
  "query": {
    "multi_match": {
      "query": "外滩如家",
      "fields": ["brand","name","business"]
    }
  }
}
精确查询
term查询
GET /hotel/_search
{
  "query": {
    "term": {
      "city.keyword": {
        "value": "北京"
      }
    }
  }
}
range范围查询

gte 大于等于 lte 小于等于

GET /hotel/_search
{
  "query": {
    "range": {
      "price": {
        "gte": 100,
        "lte": 500
      }
    }
  }
}
distance查询
GET /hotel/_search
{
  "query": {
    "geo_distance": {
      "distance": "2km",
      "location": "31.21, 121.5"
    }
  }
}
复合查询之一
function score query查询排序
GET /hotel/_search
{
  "query": {
    "function_score": {
      "query": {
        "match": {
          "all": "外滩"
        }
      },
      "functions": [
        {
         "filter": {
           "term": {
             "brand": "如家"
           }
         },
        "weight": 10
        }
      ],
      "boost_mode": "sum"
    }
  }
}
复合查询之二
Boolean query查询
must should 参与打分
must_not filter 不参与打分
Get /hotel/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "如家"
          }
        }
      ],
      "must_not": [
        {
          "range": {
            "price": {
              "gt": 400
            }
          }
        }
      ],
      "filter": {
        "geo_distance": {
          "distance": "10km",
          "location": {
            "lat": 31.24,
            "lon": 121.5
          }
        }
      }
    }
  }
}
按照经纬度排序

找到"39.76875, 116.339199"附近的酒店

GET /hotel2/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "_geo_distance": {
        "location": {
          "lat": 39.76,
          "lon": 116.34
        },
        "order": "asc",
        "unit": "km"
      }
    }
  ]
}
分页
GET /hotel2/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "price": {
        "order": "asc"
      }
    }
  ],
  "from": 0,
  "size": 5
}
高亮
GET /hotel2/_search
{
  "query": {
    "match": {
      "all": "如家"
    }
  },
  "highlight": {
    "fields": {
      "name": {
        "require_field_match": "false"
      }
    }
  }
}
总体概览
GET /hotel2/_search
{
  "query": {
    "match": {
      "all": "如家"
    }
  },
  "from": 0,
  "size": 10,
  "sort": [
    {
      "price": {
        "order": "asc"
      },
      "_geo_distance": {
        "location": {
          "lat": 40.04,
          "lon": 116.33
        },
        "order": "asc",
        "unit": "km"
      }
    }
  ], 
  "highlight": {
    "fields": {
      "name": {
        "require_field_match": "false"
      }
    }
  }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值