美文网首页es
16.ES的查询

16.ES的查询

作者: MononokeHime | 来源:发表于2018-06-14 13:03 被阅读0次

查询

ES是功能非常强大的搜索引擎,使用它的目的就是为了快速的查询到需要的数据
查询分类:
基本查询:使用ES内置的查询条件进行查询
组合查询:把多个查询组合在一起进行复杂查询
过滤:查询同时,通过filter条件在不影响打分的情况下筛选数据

1.match查询

GET lagou/job/_search
{
  "query": {
    "match": {
      "title": "Django"
    }
  }
}
image.png

2.term查询

GET lagou/job/_search
{
  "query": {
    "term": {
      "title": "Django"
    }
  }
}

term查询不会对title做处理,就算定义了分析器,也不会对title分词,只是将title作为关键词进行搜索
3.terms查询

GET lagou/job/_search
{
  "query": {
    "terms": {
      "title": ["Django", "python", "java"]
    }
  }
}

只要有一个满足就会返回结果

4.控制查询的返回结果

GET lagou/job/_search
{
  "query": {
    "match": {
      "title": "Django"
    }
  },
  "from": 1,
  "size":2
}

只取返回结果中的2个
5.match_all查询
返回所有的结果

GET lagou/job/_search
{
  "query": {
    "match_all": {}
  }
}

6.match_parse查询
短语查询

GET lagou/job/_search
{
  "query": {
    "match_phrase": {
      "title": {
        "query": "Django工程师",
        "slop":6
      }
    }
  }
}

查询的时候将Django工程师进行分词为Django工程师。结果必须同时满足Django工程师才会返回

7.multi_match查询
比如可以指定多个字段
比如查询title和desc这两个字段里面包含python的关键文档

GET lagou/job/_search
{
  "query": {
    "multi_match": {
      "query": "python", 
      "fields": ["title^3","desc"]
    }
  }
}
# ^3表示权重

8.指定返回的字段

GET lagou/job/_search
{
  "stored_fields": ["title","salary"], 
  "query": {
      "match": {
        "title":"Django"
    }
  }
}

其中"title","salary"两个字段的属性必须设置为stored
9.通过sort把结果排序
按照comments进行降序

GET lagou/job/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "comments": {
        "order": "desc"
      }
    }
  ]
}

10.范围查询

GET lagou/job/_search
{
  "query": {
    "range": {
      "comments": {
        "gte": 10,   #大于等于
        "lte": 20,   #小于等于
        "boost": 2.o #权重大小
      }
    }
  }
}

组合查询

bool查询
老版本的filtered已经被bool查询替代
bool查询包括must,should,must_not filter来完成
格式

bool:{
  "filter":[],
  "must":[],
  "should":[],
  "must_not":{},
}

新建索引

POST lagou/testjob/_bulk
{"index":{"_id":1}}
{"salary":10,"title":"python"}
{"index":{"_id":2}}
{"salary":20,"title":"Scrapy"}
{"index":{"_id":3}}
{"salary":30,"title":"Django"}
{"index":{"_id":4}}
{"salary":30,"title":"Elasticsearch"}
image.png

1.最简单的filter查询
薪资为20k的工作

mysql:select * from testjob where salary=20

GET lagou/testjob/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all":{}
      },
      "filter": {
        "term": {
          "salary": "20"
        }
      }
    }
  }
}

mysql:select * from testjob where salary=20 or salary=10

GET lagou/testjob/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all":{}
      },
      "filter": {
        "terms": {
          "salary": ["20","10"]
        }
      }
    }
  }
}

查看分析器解析的结果

GET _analyze
{
  "analyzer": "ik_max_word",
  "text": "python网络开发工程师"
}

2.查询薪资等于20k或者工作为python的工作,排除价格为30k的
mysql:select * from testjob where (salary=20 or title="python") and (salary!=30)

GET lagou/testjob/_search
{
  "query": {
    "bool": {
      "should": [
        {"term": {"salary": 20}},
        {"term": {"title": "python"}}
      ],
      "must_not": [
        {"term": {
          "price": 30
        }}
      ]
    }
  }
}

3.嵌套查询

mysql: select * from testjob where title="python" or (title="elasticsearch" and salary=30)

GET lagou/testjob/_search
{
  "query": {
    "bool": {
      "should": [
        {"term": {
          "title": "python"}},
        {
          "bool": {
            "must": [
              {"term": {
                "title": "elasticsearch"
              }},
              {"term": {
                "salary": 30}
              }
            ]
          }
        }  
      ]
    }
  }
}

相关文章

  • 16.ES的查询

    查询 ES是功能非常强大的搜索引擎,使用它的目的就是为了快速的查询到需要的数据查询分类:基本查询:使用ES内置的查...

  • DML-数据操纵语言

    一、查询 查询指定列 查询所有列 取消相同取值的行 比较查询 多重条件查询 范围查询 集合查询 匹配查询 空值查询...

  • 需求查询

    需求查询需求查询需求查询需求查询需求查询需求查询需求查询

  • Oracel_子查询

    SQL子查询 子查询语法 子查询 (内查询) 在主查询之前一次执行完成。 子查询的结果被主查询(外查询)使用 。 ...

  • ThinkPHP查询

    查询方式 表达式查询 快捷查询 区间查询 组合查询 统计查询 动态查询 SQL查询 ThikPHP支持原生SQL查...

  • Access查询有哪些

    Access查询有哪些 Access查询分选择查询追加查询更新查询生成表查询交叉表查询联合查询等

  • hibernate中的查询

    HQL 查询所有 条件查询 分页查询 Criteria 查询所有 条件查询 分页查询 查询总记录 原生SQL

  • 第七章子查询

    • 括号内的查询叫做子查询,也叫内部查询,先于主查询执行。• 子查询的结果被主查询(外部查询)使用• expr o...

  • MySQL-高级查询

    嵌套查询(子查询) 把内层的查询结果作为外层的查询条件 示例 多表查询 多个表之间联合查询 连接查询 内连接 外连...

  • mysql-数据查询语句-多表

    连接查询 连接查询,是关系数据库中最主要的查询,包括等值查询、自然连接查询、非等值查询、自身连接查询、外连接查询和...

网友评论

    本文标题:16.ES的查询

    本文链接:https://www.haomeiwen.com/subject/bywfeftx.html