Elasticsearch ES 简单查询 Query String Search 入门

带条件查询

GET test_mapping_manual1/_search?q=name:hello
GET test_mapping_manual1/_search?from=0&size=3&sort=age:asc

尝试了text类型排序需要特别处理下. "reason" : "Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [name] in order to load field data by uninverting the inverted index. Note that this can use significant memory."

这种查询方式是创建索引的字段都会被检索

GET test_mapping_manual1/_search?q=hell
关闭字段索引,在mapping中设置
"cloName": {
"type": "text",
"index": false
}

全文检索 Fulltext search

match

GET test_mapping_manual1/_search
{
  "query": {
    "match": {
      "name": "John Smith"
    }
  }
}
# name包含John或者Smith都会被检索出来即这里对搜索词做了分词,且对source data也做了分词

这种把条件放到json体中

GET test_mapping_manual1/_search
{
  "query": {
    "match": {
      "name": "keyword1 keyword2 keyword2"
    }
  }
}
# 上边查询的含义是任何包含关键字 keyword1 或者 keyword2 或者keyword3都会查询出来
# 前提是设置好分词器,如果中文使用英文分词器可能会存在问题
# 查询结构顺序按照_score进行排序,内部排序算法是bm25

查询结果会统计命中几个关键字

match_all

# 查询所有全部数据
GET test_mapping_manual1/_search
{
  "query": {
    "match_all": {
      
    }
  }
}
# 等同于
GET test_mapping_manual1/_search

# 查询集群所有数据
GET _search

multi_match

# 这段含义是在字段name和desc中查找包含"John或者IT的内容的
GET test_mapping_manual1/_search
{
  "query": {
    "multi_match": {
      "query": "John IT",
      "fields": ["name","desc"]
    }
  }
}
# 语义: 默认分词器的对`John Smith`的分词结果为`John`和`Smith`
# name.last 中包含 `John` 或者 `Smith`  
# OR  
# name.first 中包含 `John` 或者 `Smith` 
# 如果设置了"operator": "and",则中间 OR 的关系变为 AND
GET teacher/_search
{
  "query": {
    "multi_match": {
      "query": "John Smith",
      "type": "most_fields",
      "fields": [
        "name.last",
        "name.first"
      ]
      // ,"operator": "and"
    }
  }
}


match_phrase

短语搜索

短语搜索内部原理是先分词,分词之后需要命中每个分词,并且顺序要一直

# 这段含义先把name查询条件分词为John 和Tim
# 然后去source中把name字段的内容分词
# 分词之后需要同时命中John 和Tim并且顺序要一致
GET test_mapping_manual1/_search
{
  "query": {
    "match_phrase": {
      "name": "John Tim"
    }
  }
}

验证分词

GET _analyze
{
  "analyzer": "standard",
  "text": "John Sam desc IT"
}

exact match

使用关键字term

term不会对搜索词分词

记住分词分两个部分,搜索词分词和源数据分词

keyword是不对source data的值分词

match_phase是分词的

所以重点来了:

如果搜索词部分词,但是source data内容分词了,这时是无法查询数据的,因为建索引的时候索引已经分词了; 另外还需要注意一点一个字段要被搜索到一定要被索引到

GET test_mapping_manual1/_search
{
  "query": {
    "term": {
      "name": "John Smith"
    }
  }
}
# 测试数据1
POST test_mapping_manual1/_doc
{
  "name": "John Smith",
  "age": 18,
  "desc": "IT phone"
}
# 此案例无法查询出数据
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值