Elasticsearch-Query DSL

https://www.elastic.co/guide/en/elasticsearch/reference/7.3/query-dsl.html
Elasticsearch提供了基于JSON的完整查询DSL(Domain Specific Language 特定域的语言)来定义查
询。将查询DSL视为查询的AST(抽象语法树),它由两种子句组成

  • 叶子查询子句
    叶子查询子句 在特定域中寻找特定的值,如 match,term或 range查询。
  • 复合查询子句
    复合查询子句包装其他叶子查询或复合查询,并用于以逻辑方式组合多个查询(例如 bool或dis_max查询),或更改其行为(例如 constant_score查询)
    使用ElasticSearch的时候,避免不了使用DSL语句去查询,就像使用关系型数据库的时候要学会SQL语法一样。
POST /索引库名/_search
{
"query":{
"查询类型":{
"查询条件":"查询条件值"
}
}
}
  • 这里的query代表一个查询对象,里面可以有不同的查询属性
    查询类型:
    例如: match_all , match , term , range 等等
    查询条件:查询条件会根据类型的不同,写法也有差异

全文搜索(full-text query)

全文搜索能够搜索已分析的文本字段,如电子邮件正文,商品描述等。使用索引期间应用于字段的同一分析器处理查询字符串。全文搜索的分类很多 几个典型的如下
匹配搜索(match query)
全文查询的标准查询,它可以对一个字段进行模糊、短语查询。 match queries 接收text/numerics/dates, 对它们进行分词分析, 再组织成一个boolean查询。可通过operator 指定bool组合操作(or、and 默认是 or )。
现在,索引库中有2部手机,1台电视

PUT /lagou-property
{
  "settings": {},
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "analyzer": "ik_max_word"
      },
      "images": {
        "type": "keyword"
      },
      "price": {
        "type": "float"
      }
    }
  }
}


POST /lagou-property/_doc/
{
  "title": "小米电视4A",
  "images": "http://image.lagou.com/12479122.jpg",
  "price": 4288
} 
POST /lagou-property/_doc/
{
"title": "小米手机",
"images": "http://image.lagou.com/12479622.jpg",
"price": 2699
} 
POST /lagou-property/_doc/
{
"title": "华为手机",
"images": "http://image.lagou.com/12479922.jpg",
"price": 5699
}

  • 查询
POST /lagou-property/_search
{
  "query": {
    "match": {
      "title": "小米电视"
    }
  }
}


{
“took” : 414,
“timed_out” : false,
“_shards” : {
“total” : 1,
“successful” : 1,
“skipped” : 0,
“failed” : 0
},
“hits” : {
“total” : {
“value” : 2,
“relation” : “eq”
},
“max_score” : 1.2044649,
“hits” : [
{
“_index” : “lagou-property”,
“_type” : “_doc”,
“_id” : “S3jQ83UBZ_tNtiojWY_a”,
“_score” : 1.2044649,
“_source” : {
“title” : “小米电视4A”,
“images” : “http://image.lagou.com/12479122.jpg”,
“price” : 4288
}
},
{
“_index” : “lagou-property”,
“_type” : “_doc”,
“_id” : “THjQ83UBZ_tNtiojZo-D”,
“_score” : 0.52354836,
“_source” : {
“title” : “小米手机”,
“images” : “http://image.lagou.com/12479622.jpg”,
“price” : 2699
}
}
]
}
}

不仅会查询到电视,而且与小米相关的都会查询到,多个词之间是 or 的关系
and关系
某些情况下,我们需要更精确查找,我们希望这个关系变成 and ,可以这样做

POST /lagou-property/_search
{
  "query": {
    "match": {
      "title": {
        "query": "小米电视4A",
        "operator": "and"
      }
    }
  }
}


{
“took” : 4,
“timed_out” : false,
“_shards” : {
“total” : 1,
“successful” : 1,
“skipped” : 0,
“failed” : 0
},
“hits” : {
“total” : {
“value” : 1,
“relation” : “eq”
},
“max_score” : 2.8330114,
“hits” : [
{
“_index” : “lagou-property”,
“_type” : “_doc”,
“_id” : “S3jQ83UBZ_tNtiojWY_a”,
“_score” : 2.8330114,
“_source” : {
“title” : “小米电视4A”,
“images” : “http://image.lagou.com/12479122.jpg”,
“price” : 4288
}
}
]
}
}

只有同时包含 小米 和 电视 的词条才会被搜索到

短语搜索(match phrase query)

match_phrase 查询用来对一个字段进行短语查询,可以指定 analyzer、slop移动因子

GET /lagou-property/_search
{
  "query": {
    "match_phrase": {
      "title": "小米电视"
    }
  }
}

{
“took” : 6,
“timed_out” : false,
“_shards” : {
“total” : 1,
“successful” : 1,
“skipped” : 0,
“failed” : 0
},
“hits” : {
“total” : {
“value” : 1,
“relation” : “eq”
},
“max_score” : 1.2044649,
“hits” : [
{
“_index” : “lagou-property”,
“_type” : “_doc”,
“_id” : “S3jQ83UBZ_tNtiojWY_a”,
“_score” : 1.2044649,
“_source” : {
“title” : “小米电视4A”,
“images” : “http://image.lagou.com/12479122.jpg”,
“price” : 4288
}
}
]
}
}

  • 空格

GET /lagou-property/_search
{
  "query": {
    "match_phrase": {
      "title": "小米 4A"
    }
  }
}

{
“took” : 3,
“timed_out” : false,
“_shards” : {
“total” : 1,
“successful” : 1,
“skipped” : 0,
“failed” : 0
},
“hits” : {
“total” : {
“value” : 0,
“relation” : “eq”
},
“max_score” : null,
“hits” : [ ]
}
}

  • 移动因子
GET /lagou-property/_search
{
  "query": {
    "match_phrase": {
      "title": {
        "query": "小米 4A",
        "slop": 2
      }
    }
  }
}

{
“took” : 0,
“timed_out” : false,
“_shards” : {
“total” : 1,
“successful” : 1,
“skipped” : 0,
“failed” : 0
},
“hits” : {
“total” : {
“value” : 1,
“relation” : “eq”
},
“max_score” : 1.2441062,
“hits” : [
{
“_index” : “lagou-property”,
“_type” : “_doc”,
“_id” : “S3jQ83UBZ_tNtiojWY_a”,
“_score” : 1.2441062,
“_source” : {
“title” : “小米电视4A”,
“images” : “http://image.lagou.com/12479122.jpg”,
“price” : 4288
}
}
]
}
}

  • query_string 查询

Query String Query提供了无需指定某字段而对文档全文进行匹配查询的一个高级查询,同时可以指定在哪些字段上进行匹配

  • 默认
# 默认不指定
GET /lagou-property/_search
{
  "query": {
    "query_string": {
      "query": "2699"
    }
  }
}

{
“took” : 17,
“timed_out” : false,
“_shards” : {
“total” : 1,
“successful” : 1,
“skipped” : 0,
“failed” : 0
},
“hits” : {
“total” : {
“value” : 1,
“relation” : “eq”
},
“max_score” : 1.0,
“hits” : [
{
“_index” : “lagou-property”,
“_type” : “_doc”,
“_id” : “THjQ83UBZ_tNtiojZo-D”,
“_score” : 1.0,
“_source” : {
“title” : “小米手机”,
“images” : “http://image.lagou.com/12479622.jpg”,
“price” : 2699
}
}
]
}
}

  • 指定字段
# 指定字段
GET /lagou-property/_search
{
  "query": {
    "query_string": {
      "query": "2699",
      "default_field": "title"
    }
  }
}

{
“took” : 1,
“timed_out” : false,
“_shards” : {
“total” : 1,
“successful” : 1,
“skipped” : 0,
“failed” : 0
},
“hits” : {
“total” : {
“value” : 0,
“relation” : “eq”
},
“max_score” : null,
“hits” : [ ]
}
}

  • 逻辑查询
    and
GET /lagou-property/_search
{
  "query": {
    "query_string": {
      "default_field": "title",
      "query": "手机 AND 小米"
    }
  }
}

{
“took” : 0,
“timed_out” : false,
“_shards” : {
“total” : 1,
“successful” : 1,
“skipped” : 0,
“failed” : 0
},
“hits” : {
“total” : {
“value” : 1,
“relation” : “eq”
},
“max_score” : 1.0470967,
“hits” : [
{
“_index” : “lagou-property”,
“_type” : “_doc”,
“_id” : “THjQ83UBZ_tNtiojZo-D”,
“_score” : 1.0470967,
“_source” : {
“title” : “小米手机”,
“images” : “http://image.lagou.com/12479622.jpg”,
“price” : 2699
}
}
]
}
}

  • or
GET /lagou-property/_search
{
  "query": {
    "query_string": {
      "default_field": "title",
      "query": "手机 OR 小米"
    }
  }
}

{
“took” : 1,
“timed_out” : false,
“_shards” : {
“total” : 1,
“successful” : 1,
“skipped” : 0,
“failed” : 0
},
“hits” : {
“total” : {
“value” : 3,
“relation” : “eq”
},
“max_score” : 1.0470967,
“hits” : [
{
“_index” : “lagou-property”,
“_type” : “_doc”,
“_id” : “THjQ83UBZ_tNtiojZo-D”,
“_score” : 1.0470967,
“_source” : {
“title” : “小米手机”,
“images” : “http://image.lagou.com/12479622.jpg”,
“price” : 2699
}
},
{
“_index” : “lagou-property”,
“_type” : “_doc”,
“_id” : “TXjQ83UBZ_tNtiojlY_A”,
“_score” : 0.52354836,
“_source” : {
“title” : “华为手机”,
“images” : “http://image.lagou.com/12479922.jpg”,
“price” : 5699
}
},
{
“_index” : “lagou-property”,
“_type” : “_doc”,
“_id” : “S3jQ83UBZ_tNtiojWY_a”,
“_score” : 0.39019167,
“_source” : {
“title” : “小米电视4A”,
“images” : “http://image.lagou.com/12479122.jpg”,
“price” : 4288
}
}
]
}
}

  • 模糊查询

GET /lagou-property/_search
{
  "query": {
    "query_string": {
      "default_field": "title",
      "query": "大米~1"
    }
  }
}


其中 ~1可改1个字,

{
“took” : 1,
“timed_out” : false,
“_shards” : {
“total” : 1,
“successful” : 1,
“skipped” : 0,
“failed” : 0
},
“hits” : {
“total” : {
“value” : 2,
“relation” : “eq”
},
“max_score” : 0.26177418,
“hits” : [
{
“_index” : “lagou-property”,
“_type” : “_doc”,
“_id” : “THjQ83UBZ_tNtiojZo-D”,
“_score” : 0.26177418,
“_source” : {
“title” : “小米手机”,
“images” : “http://image.lagou.com/12479622.jpg”,
“price” : 2699
}
},
{
“_index” : “lagou-property”,
“_type” : “_doc”,
“_id” : “S3jQ83UBZ_tNtiojWY_a”,
“_score” : 0.19509584,
“_source” : {
“title” : “小米电视4A”,
“images” : “http://image.lagou.com/12479122.jpg”,
“price” : 4288
}
}
]
}
}

  • 多字段查询
GET /lagou-property/_search
{
  "query": {
    "query_string": {
      "fields": ["title","price"],
      "query": "2699"
    }
  }
}

{
“took” : 1,
“timed_out” : false,
“_shards” : {
“total” : 1,
“successful” : 1,
“skipped” : 0,
“failed” : 0
},
“hits” : {
“total” : {
“value” : 1,
“relation” : “eq”
},
“max_score” : 1.0,
“hits” : [
{
“_index” : “lagou-property”,
“_type” : “_doc”,
“_id” : “THjQ83UBZ_tNtiojZo-D”,
“_score” : 1.0,
“_source” : {
“title” : “小米手机”,
“images” : “http://image.lagou.com/12479622.jpg”,
“price” : 2699
}
}
]
}
}

  • 多字段匹配搜索(multi match query)
    如果你需要在多个字段上进行文本搜索,可用multi_match 。multi_match在 match的基础上支持对多个字段进行文本查询
GET /lagou-property/_search
{
  "query": {
    "multi_match": {
      "query": "2699",
      "fields": [
        "title",
        "price"
      ]
    }
  }
}
# 还可以使用*匹配多个字段
GET /lagou-property/_search
{
  "query": {
    "multi_match": {
      "query": "2699",
      "fields": [
        "title",
        "pr*"
      ]
    }
  }
}
# * 匹配

GET /lagou-property/_search
{
  "query": {
    "multi_match": {
      "query": "http://image.lagou.com/12479622.jpg",
      "fields": [
        "title",
        "ima*"
      ]
    }
  }
}


{
“took” : 4,
“timed_out” : false,
“_shards” : {
“total” : 1,
“successful” : 1,
“skipped” : 0,
“failed” : 0
},
“hits” : {
“total” : {
“value” : 1,
“relation” : “eq”
},
“max_score” : 0.9808292,
“hits” : [
{
“_index” : “lagou-property”,
“_type” : “_doc”,
“_id” : “THjQ83UBZ_tNtiojZo-D”,
“_score” : 0.9808292,
“_source” : {
“title” : “小米手机”,
“images” : “http://image.lagou.com/12479622.jpg”,
“price” : 2699
}
}
]
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值