es

16.ES的查询

2018-06-14  本文已影响0人  MononokeHime

查询

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}
              }
            ]
          }
        }  
      ]
    }
  }
}
上一篇 下一篇

猜你喜欢

热点阅读