Elasticsearch基础入门(4)高级检索

前言 

在真实的业务系统中,elastic基础检索当然无法满足我们复杂的业务需求,所以学习使用高级检索是势在必行的,下面我们就来学习如何使用ElasticSearch进行高级检索。

本篇文章均在kibana可视化平台操作,没有安装kibana的朋友先去《ElasticSearch 整合kibana》

提醒

因为我们已经在kibana配置了elastic服务的IP地址,所以再以下请求中,我们只写表达式即可。

关于表达式的语法问题,大家不用担心,后面我们会详细的了解。 

kibana开发工具控制台 位置如下图所示,这个工具很方便,有各种提示

 

在搜索之前我们先插入几条数据

POST food/fruit/1
{
  "name":"apple pingguo苹果",
  "color":"red",
  "num":20
}
POST food/fruit/2
{
  "name":"banana xiangjiao香蕉 ",
  "color":"yellow",
  "num":1
}
POST food/fruit/3
{
  "name":"lemon ningmeng柠檬 ",
  "color":"yellow",
  "num":6
}
POST food/fruit/4
{
  "name":"orange jvzi橘子",
  "color":"orange",
  "num":66
}

一、使用query-string查询

 

1.1、我们尝试搜索所有水果

GET food/fruit/_search

结果:

{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 4,
    "max_score": 1,
    "hits": [
      {
        "_index": "food",
        "_type": "fruit",
        "_id": "2",
        "_score": 1,
        "_source": {
          "name": "banana xiangjiao香蕉 ",
          "color": "yellow",
          "num": 1
        }
      },
      {
        "_index": "food",
        "_type": "fruit",
        "_id": "4",
        "_score": 1,
        "_source": {
          "name": "orange jvzi橘子",
          "color": "orange",
          "num": 66
        }
      },
      {
        "_index": "food",
        "_type": "fruit",
        "_id": "1",
        "_score": 1,
        "_source": {
          "name": "apple pingguo苹果",
          "color": "red",
          "num": 20
        }
      },
      {
        "_index": "food",
        "_type": "fruit",
        "_id": "3",
        "_score": 1,
        "_source": {
          "name": "lemon ningmeng柠檬 ",
          "color": "yellow",
          "num": 6
        }
      }
    ]
  }
}

 

1.2、搜索颜色是红色的水果

GET food/fruit/_search?q=color:red

公式:<index>/<type>/_search?q=<field>:<value>

结果:

{
  "took": 6,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "food",
        "_type": "fruit",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "name": "apple pingguo苹果",
          "color": "red",
          "num": 20
        }
      }
    ]
  }
}

 

二、使用查询表达式

 

2.1、匹配所有  match_all

查询所有数据

kibana查询

GET _search
{
  "query": {
    "match_all": {}
  }
}

curl查询,这个是在Linux下terminal执行

curl -XGET "http://172.19.25.110:9200/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "match_all": {}
  }
}'

结果:这里不仅可以看到有多少条数据,而且还有文档的详细信息,因为数据太多了不方便展示,我这里就截取了部分数据。

{
    "took":5,
    "timed_out":false,
    "_shards":{
        "total":10,
        "successful":10,
        "skipped":0,
        "failed":0
    },
    "hits":{
        "total":6,
        "max_score":1,
        "hits":[
            {
                "_index":"book",
                "_type":"java",
                "_id":"2",
                "_score":1,
                "_source":{
                    "name":"java编程思想",
                    "price":60,
                    "author":"埃克尔",
                    "create_date_time":"2018-3-11 14:22:19"
                }
            },
            {
                "_index":"food",
                "_type":"fruit",
                "_id":"3",
                "_score":1,
                "_source":{
                    "name":"lemon ningmeng柠檬 ",
                    "color":"yellow",
                    "num":6
                }
            }
        ]
    }
}

 

2.2、检索 match

查询颜色为黄色的水果

kibana查询

GET food/fruit/_search
{
  "query": {
    "match": {
      "color": "yellow"
    }
  }
}

curl查询

curl -XGET "http://172.19.25.110:9200/food/fruit/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "color": "yellow"
    }
  }
}'

结果:

{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "food",
        "_type": "fruit",
        "_id": "2",
        "_score": 0.2876821,
        "_source": {
          "name": "banana xiangjiao香蕉 ",
          "color": "yellow",
          "num": 1
        }
      },
      {
        "_index": "food",
        "_type": "fruit",
        "_id": "3",
        "_score": 0.2876821,
        "_source": {
          "name": "lemon ningmeng柠檬 ",
          "color": "yellow",
          "num": 6
        }
      }
    ]
  }
}

大家可以观察一下_score这个关键字,它的值越大匹配到的文档就越精准,当然这跟es所使用的分词器有必然的关系。

 

2.3、利用filter搜索

之前的查询都是完全匹配,现在我们稍微试一下较复杂的搜索。

搜索颜色(color)是黄色并且数量(num)大于2的水果

GET food/fruit/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "color": "yellow"
          }
        }
      ],
      "filter": {
        "range": {
          "num": {
            "gt": 2
          }
        }
      }
    }
  }
}

这是一个match查询 加了 一个range 过滤器,利用组合查询就可以将我们的数据完全匹配出来。

结果:

{
  "took": 16,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "food",
        "_type": "fruit",
        "_id": "3",
        "_score": 0.2876821,
        "_source": {
          "name": "lemon ningmeng柠檬 ",
          "color": "yellow",
          "num": 6
        }
      }
    ]
  }
}

 

2.4、高亮搜索

要想实现高亮搜索,其实很简单,在前面查询的基础上增加一个新的参数 highlight    

GET food/fruit/_search
{
  "query": {
    "match": {
      "color": "red"
    }
  },
  "highlight": {
    "fields": {
      "color": {}
    }
  }
}

结果:

{
  "took": 753,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "food",
        "_type": "fruit",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "name": "apple pingguo苹果",
          "color": "red",
          "num": 20,
          "description": "fruit you know orange 中国."
        },
        "highlight": {
          "color": [
            "<em>red</em>"
          ]
        }
      }
    ]
  }
}

在结果中,除了搜索出原数据,并且会多个highlight,其中 关键字被HTML标签 <em> </em> 封装。

 

待更新

 

 

转载于:https://my.oschina.net/codingcloud/blog/1617051

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值