利用kibana插件对Elasticsearch查询

利用kibana插件对Elasticsearch查询

Elasticsearch是功能非常强大的搜索引擎,使用它的目的就是为了快速的查询到需要的数据。

查询分类:

  基本查询:使用Elasticsearch内置查询条件进行查询

  组合查询:把多个查询组合在一起进行复合查询

  过滤:查询同时,通过filter条件在不影响打分的情况下筛选数据

创建索引
PUT lagou
    {
      "mappings": {
        "job":{
          "properties": {
            "title":{
              "store": true,
              "type": "text",
              "analyzer": "ik_max_word"
            },
            "company_name":{
              "store": true,
              "type": "keyword"
            },
            "desc":{
              "type": "text"
            },
            "comments":{
              "type": "integer"
            },
            "add_time":{
              "type": "date",
              "format": "yyyy-MM-dd"
            }
          }
        }
      }
    }
    

 


PUT lagou    

POST lagou/job
{
  "title":"python django 开发工程师",
  "company_name":"美团",
  "desc":"美团是一个在吗在吗在吗",
  "comments":20,
  "add_time":"2017-4-16"
}

POST lagou/job
{
  "title":"python 爬虫 开发工程师",
  "company_name":"数据冰山",
  "desc":"专门找数据的一家公司 python",
  "comments":15,
  "add_time":"2016-4-16"
}
POST lagou/job
{
  "title":"django 后端 开发工程师",
  "company_name":"百度科技有限公司",
  "desc":"我也不知道这里应该写一点什么东西了 python",
  "comments":20,
  "add_time":"2017-4-16"
}
POST lagou/job
{
  "title":"python GUI  开发工程师",
  "company_name":"熊猫",
  "desc":"在线视频教育python",
  "comments":6,
  "add_time":"2017-4-16"
}

 

#match查询,

 

对我们的输入进行一个分词,指明一个字段,会去找这个字段有没有我们写的这个关键词,关键词不区分大小写,在做分词的时候会自动对大小写进行转换

GET lagou/_search
GET lagou/job/_search
{
  "query": {
    "match": {
      "title": "爬取"
    }
  }
}

#term查询#

传递过来的关键词不会进行任何处理不会解析,text 会分词,keyword不会分词的

GET lagou/job/_search
{
  "query": {
    "term": {
      "company_name": "百度科技有限公司"
    }
  }
}

 #terms查询

只要关键字中有一个都会匹配出来

GET lagou/job/_search
{
  "query": {
    "terms": {
      "title": ["django","开发","python"]
    }
  }
}

#控制查询的返回数量

GET lagou/_search
{
  "query": {
    "match": {
      "title": "python"
    }
  },
  "from": 1,
  "size": 3
}

#从哪开始,数量多少

#match——all  查询

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

#match_phrase查询#短语查询

#slop 两词之间最小的距离,query 必须都满足所有的分词的关键词

GET lagou/_search
{
  "query": {
    "match_phrase": {
      "title": {
        "query": "python django",
        "slop":6
      }
    }
  }
}

 

#multi_match查询#

比如可以指定多个字段#比如查询title和desc这两个字段里面包含python 的关键词的文档GET lagou/job/_search     
#     ^3   指的是权重,什么比什么的权重高多少

GET lagou/_search
{
  "query": {
    "multi_match": {
      "query": "python",
      "fields": ["title","desc^3"]
    }
  }
}

#指定返回的字段

GET lagou/_search
{
  "stored_fields": ["title"],
  "query": {
    "match": {
      "title": "开发"
    }
  }
}

#通过sort把结果排序

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

#范围查询#range查询

GET lagou/_search
{
  "query": {
    "range": {
      "comments": {
        "gte": 10,
        "lte": 20,
        "boost": 2.0
      }
    }
  }
}

#range查询

GET lagou/_search
{
  "query": {
    "range": {
      "add_time": {
        "gte": "2017-04-01",
        "lte": "now"
      }
    }
  }
}

#wildcard 查询#简单的模糊查询

GET lagou/_search
{
  "query": {
      "wildcard": {
        "title": {
          "value": "pyth*n",
          "boost": 2
        }
      }
  }
}
posted @ 2017-11-30 10:37  风水涣  阅读(679)  评论(0编辑  收藏  举报