分布式搜索引擎的DSL查询语句

linux系统
	安装docker
			拉取elasticSearch镜像
			启动容器
			安装:	kibana	(可视化工具)		
		案例:	https://gitee.com/lijx_jh/hotel-demo

具体教程查询gitee代码里的笔记
java操作的代码后续更新

DSL语句

创建索引库

PUT /hotel
{
  "mappings": {
    "properties": {
      "id": {
        "type": "keyword"
      },
      "name":{
        "type": "text",
        "analyzer": "ik_max_word",
        "copy_to": "all"
      },
      "address":{
        "type": "keyword",
        "index": false
      },
      "price":{
        "type": "integer"
      },
      "score":{
        "type": "integer"
      },
      "brand":{
        "type": "keyword",
        "copy_to": "all"
      },
      "city":{
        "type": "keyword",
        "copy_to": "all"
      },
      "starName":{
        "type": "keyword"
      },
      "business":{
        "type": "keyword"
      },
      "location":{
        "type": "geo_point"
      },
      "pic":{
        "type": "keyword",
        "index": false
      },
      "all":{
        "type": "text",
        "analyzer": "ik_max_word"
      }
    }
  }
}
查询文档操作
###########################查询文档##############################
# 查询所有: match  match_all
# 全文检索: match_query   multi_match_query
# 精确查找: ids   range   term
# 地理查询: geo_distance    geo_bounding_box
# 复合查询: bool    function_score
#################################################################
全文检索
###############################全文检索################################
# 全文检索基本流程:
#   1. 对用户搜索的内容做分词,得到词条
#   2. 根据词条去倒排索引库中匹配,得到文档id
#   3. 根据文档id找到文档,返回给用户
# 百度的输入框
# jd商城的输入框
#######################################################################

# match 单字段查询
GET /hotel/_search
{
 "query": {
    "match":{
    "all":"外滩"
    }
  }
}


# mulit_match,多字段查询,任何一个字段符合条件就算符合查询条件
# 查询所有
GET /hotel/_search
{
  "query": {
    "multi_match": {
      "query": "外滩",
      "fields": ["brand","name","business"]
    }
  }
}

精确查询
###########################精确查询####################################
# 一般查询的是不需要分词的字段  品牌/日期/数值/boolean


# term查询    精确查询,根据词条精确值查询
# value中的内容不是我们的词条时,则查不到
GET /hotel/_search
{
  "query": {
    "term": {
      "city": {
        "value": "上海"
      }
    }
  }
}


# range查询   根据值的范围查询
# 一般用在对数值类型做范围过滤的时候  (价格/日期/评分..)
GET /hotel/_search
{
  "query": {
    "range": {
      "price": {
        "gte": 100,
        "lte": 500
      }
    }
  }
}
地理坐标查询
###########################地理坐标查询################################
# 搜索附近的人

# 矩形范围查询  geo_bounding_box  
# 查询坐标落在某个矩形范围的所有文档
GET /hotel/_search
{
  "query": {
    "geo_bounding_box":{
      "location" :{
        "top_left":{
          "lat":31.1,
          "lon":121.5
        },
        "bottom_right":{
          "lat":30.1,
          "lon":121.7
        }
      }
    }
  }
}

# 附近查询  geo_distance  
# 查询到指定中心点小于某个距离值的所有文档。
GET /hotel/_search
{
  "query": {
    "geo_distance":{
      "distance":"1km",
      "location":"31.21,121.5"
    }
  }
}

排序查询
###########################排序#################################
GET /hotel/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "score": {
        "order": "desc"
      }
    },
    {
      "price": "asc"
    }
    
  ]
}
复合查询
############################复合查询#################################
# 就是将简单查询组合起来,实现更复杂的搜索逻辑
# function score      算分函数查询,可以控制文档相关性算分,控制文档排名
# bool query          布尔查询,利用逻辑关系组合多个其他查询
#
# 相关性算分    打分算分
# 早期:             TF(词条频率)
#                   IDF(逆文档频率)
# 5.1版本以后:      BM25算法


# 运行流程: 
#   1. 根据原始条件查询搜索文档,并计算相关性算分,称为原始算分
#   2. 根据过滤条件,过滤文档
#   3. 符合过滤条件的文档,基于算分函数运算,得到函数算分
#   4. 将原始算分与函数算分基于 运算模式进行运算,得到最终结果

GET /hotel/_search
{
  "query": {
    "function_score": {
      "query": {
        "match": {
          "all": "外滩"
        }
      },
      "functions": [
        {
          "filter": {
            "term": {
              "brand": "如家"
            }
          },
          "weight":10
        }
      ],
      "boost_mode": "sum"
    }
  }
}

布尔查询
#########################布尔查询##################################
# must          必须匹配每一个查询  
# should        选择性匹配子查询
# must_not      必须不匹配,不参与算分类似"非"   
# filter        必须匹配,不参与算分
#  不参与算分时(性能好)
  
GET /hotel/_search
{
  "query": {
    "bool": {
      "must": [
        {"term": {
          "city": {
            "value": "上海"
          }
        }}
      ],
      "should": [
        {"term": {
          "brand": {
            "value": "皇冠假日"
          }
        }},
        {"term": {
          "brand": {
            "value": "华美达"
          }
        }}
      ],
      "must_not": [
        {"range": {
          "price": {
            "gte": 100,
            "lte": 2000
          }
        }}
      ],
      "filter": [
        {"range": {
          "score": {
            "gte": 45
          }
        }}
      ]
    }
  }
}
排序查询
############################排序####################################
GET /hotel/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "score": {
        "order": "desc"
      },
      "price": {
        "order": "asc"
      }
    }
  ]
}

GET /hotel/_search
{
  "query": {
    "match_all": {}
  },
  "sort":[
    {
      "_geo_distance": {
    "location" :{
      "lat":31.034,
      "lon":121.612
    },
    "order":"asc",
    "unit":"km"
  }
    }
    ]
}
分页查询
##########################分页######################################
GET /hotel/_search
{
  "query": {
    "match_all": {}
  },
  "from": 0,    
  "size": 20,
  "sort": [
    {
      "price": "asc"
    }
  ]
}


# 百度上限75页  京东上限100页   
# 
高亮显示
############################高亮显示###################################

# 搜索结果中的关键字突出显示
# ES搜索子端必须与高亮字段一致

GET /hotel/_search
{
  "query": {
    "match": {
      "all": "店"
    }
  },
  "highlight": {
    "fields": {
      "name": {
         "require_field_match": "false"
      }
      , "brand": {
        "require_field_match": "false"
      }
    }
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值