栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

ElasticSearch- DSL高级检索(Query)(三)

ElasticSearch- DSL高级检索(Query)(三)

0. 查询所有(match_all)

match_all关键字: 返回索引中的全部文档

GET /ems/emp/_search
{
 	"query": { "match_all": {} }
}	

1. 查询结果中返回指定条数(size)

size 关键字: 指定查询结果中返回指定条数。 默认返回值10条

GET /ems/emp/_search
{
 	"query": { "match_all": {} },
	"size": 1
}	

2. 分页查询(from)

from 关键字: 用来指定起始返回位置,和size关键字连用可实现分页效果

GET /ems/emp/_search
{
      "query": {"match_all": {}},
      "sort": [
        {
          "age": {
            "order": "desc"
          }
        }
      ],
      "size": 2, 
      "from": 1
}

3. 查询结果中返回指定字段(_source)

_source 关键字: 是一个数组,在数组中用来指定展示那些字段

GET /ems/emp/_search
{
      "query": { "match_all": {} },
      "_source": ["account_number", "balance"]
}

4. 关键词查询(term)

term 关键字: 用来使用关键词查询

GET /ems/emp/_search
{
  "query": {
    "term": {
      "address": {
        "value": "北京"
      }
    }
  }
}

NOTE1: 通过使用term查询得知ES中默认使用分词器为标准分词器(StandardAnalyzer),标准分词器对于英文单词分词,对于中文单字分词。

NOTE2: 通过使用term查询得知,在ES的Mapping Type 中 keyword , date ,integer, long , double , boolean or ip 这些类型不分词,只有text类型分词。

5. 范围查询(range)

range 关键字: 用来指定查询指定范围内的文档

GET /ems/emp/_search
{
  "query": {
    "range": {
      "age": {
        "gte": 8,
        "lte": 30
      }
    }
  }
}

6. 前缀查询(prefix)

prefix 关键字: 用来检索含有指定前缀的关键词的相关文档

GET /ems/emp/_search
{
  "query": {
    "prefix": {
      "content": {
        "value": "redis"
      }
    }
  }
}

7. 通配符查询(wildcard)

wildcard 关键字: 通配符查询 ? 用来匹配一个任意字符 * 用来匹配多个任意字符

GET /ems/emp/_search
{
  "query": {
    "wildcard": {
      "content": {
        "value": "re*"
      }
    }
  }
}

8. 多id查询(ids)

ids 关键字 : 值为数组类型,用来根据一组id获取多个对应的文档

GET  /ems/emp/_search
{
  "query": {
    "ids": {
      "values": ["lg5HwWkBxH7z6xax7W3_","lQ5HwWkBxH7z6xax7W3_"]
    }
  }
}

9. 模糊查询(fuzzy)

fuzzy 关键字: 用来模糊查询含有指定关键字的文档

GET /ems/emp/_search
{
  "query": {
    "fuzzy": {
      "content":"spring"
    }
  }
}

fuzzy 模糊查询 最大模糊错误 必须在0-2之间

搜索关键词长度为 2 不允许存在模糊 0 搜索关键词长度为3-5 允许一次模糊 0 1 搜索关键词长度大于5 允许最大2模糊

10. 布尔查询(bool)

bool 关键字: 用来组合多个条件实现复杂查询

​ must: 相当于&& 同时成立

​ should: 相当于|| 成立一个就行

​ must_not: 相当于! 不能满足任何一个

GET /ems/emp/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "age": {
              "gte": 0,
              "lte": 30
            }
          }
        }
      ],
      "must_not": [
        {"wildcard": {
          "content": {
            "value": "redi?"
          }
        }}
      ]
    }
  },
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ]
}

11. 高亮查询(highlight)

highlight 关键字: 可以让符合条件的文档中的关键词高亮

GET /ems/emp/_search
{
  "query": {
    "term": {
      "content": {
        "value": "redis"
      }
    }
  },
  "highlight": {
    "fields": {
      "*": {}
    }
  }
}

自定义高亮html标签: 可以在highlight中使用pre_tags和post_tags

GET /ems/emp/_search
{
  "query":{
    "term":{
      "content":"框架"
    }
  },
  "highlight": {
    "pre_tags": [""],
    "post_tags": [""],
    "fields": {
      "*":{}
    }
  }
}

多字段高亮 使用require_field_match开启多个字段高亮

 GET /ems/emp/_search
{
  "query":{
    "term":{
      "content":"框架"
    }
  },
  "highlight": {
    "pre_tags": [""],
    "post_tags": [""],
    "require_field_match":false,
    "fields": {
      "*":{}
    }
  }
}
12. 多字段查询(multi_match,会对要查询的词先进行分词,然后再去索引库查找符合的数据)
GET /ems/emp/_search
{
  "query": {
    "multi_match": {
      "query": "中国",
      "fields": ["name","content"] #这里写要检索的指定字段
    }
  }
}
这里推荐在字段中写可以分词的字段

13. 多字段分词查询(query_string,好处是可以对你要查询的词进行分词处理,然后到索引库去匹配)
GET /dangdang/book/_search
{
  "query": {
    "query_string": {
      "query": "中国声音",
      "analyzer": "ik_max_word", 
      "fields": ["name","content"]
    }
  }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/600102.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号