结果过滤

默认情况下,elasticsearch在搜索的结果中,会把文档中保存在_source的所有字段都返回。

如果我们只想获取其中的部分字段,我们可以添加_source的过滤

直接指定字段

示例:

POST /heima/_search
{
  "_source": ["title","price"],
  "query": {
    "term": {
      "price": 2699
    }
  }
}

返回的结果:  

{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "3q3hTW0BTp_XthqB2lMR",
        "_score": 1,
        "_source": {
          "price": 2699,
          "title": "小米手机"
        }
      }
    ]
  }
}

指定includes和excludes

我们也可以通过:

  • includes:来指定想要显示的字段
  • excludes:来指定不想要显示的字段

二者都是可选的。

示例:

POST /heima/_search
{
  "_source": {
    "includes":["title","price"]
  },
  "query": {
    "term": {
      "price": 2699
    }
  }
}

与下面的结果将是一样的:  

POST /heima/_search
{
  "_source": {
     "excludes": ["images"]
  },
  "query": {
    "term": {
      "price": 2699
    }
  }
}

结果:  

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "3q3hTW0BTp_XthqB2lMR",
        "_score": 1,
        "_source": {
          "price": 2699,
          "title": "小米手机"
        }
      }
    ]
  }
}

 高级查询

布尔组合(bool)

bool把各种其它查询通过must(与)、must_not(非)、should(或)的方式进行组合  

GET /heima/_search
{
    "query":{
        "bool":{
            "must":     { "match": { "title": "小米" }},
            "must_not": { "match": { "title":  "电视" }},
            "should":   { "match": { "title": "手机" }}
        }
    }
}

结果:  

{
  "took": 134,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1.0884295,
    "hits": [
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "3q3hTW0BTp_XthqB2lMR",
        "_score": 1.0884295,
        "_source": {
          "title": "小米手机",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 2699
        }
      }
    ]
  }
}

范围查询(range)

range 查询找出那些落在指定区间内的数字或者时间

range查询允许以下字符:

操作符 说明
gt 大于
gte 大于等于
lt 小于
lte 小于等于

示例:  

POST /heima/_search
{
    "query":{
        "range": {
            "price": {
                "gte":  3000,
                "lt":   5000
            }
        }
    }
}

结果:  

{
  "took": 7,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "3a3hTW0BTp_XthqB2lMR",
        "_score": 1,
        "_source": {
          "title": "大米手机",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 3288
        }
      },
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "363hTW0BTp_XthqB2lMR",
        "_score": 1,
        "_source": {
          "title": "小米电视4A",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 4288
        }
      }
    ]
  }
}

模糊查询(fuzzy)

我们新增一个商品:  

POST /heima/goods/4
{
    "title":"apple手机",
    "images":"http://image.leyou.com/12479122.jpg",
    "price":5899.00
}

fuzzy自动将拼写错误的搜索文本,进行纠正,纠正以后去尝试匹配索引中的数据

它允许用户搜索词条与实际词条出现偏差,但是偏差的编辑距离不得超过2:

POST /heima/_search
{
  "query": {
    "fuzzy": {
      "title": "appla"
    }
  }
}

上面的查询,也能查询到apple手机

fuzziness,你的搜索文本最多可以纠正几个字母去跟你的数据进行匹配,默认如果不设置,就是2我们可以通过

POST /heima/_search
{
  "query": {
    "fuzzy": {
      "title": {
        "value": "applaa",
        "fuzziness": 2
      }
    }
  }
}

 排序

单字段排序

sort 可以让我们按照不同的字段进行排序,并且通过order指定排序的方式 

POST /heima/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {"price": {"order": "desc"}}
  ]
}

多字段排序

假定我们想要结合使用 price和 _score(得分) 进行查询,并且匹配的结果首先按照价格排序,然后按照相关性得分排序:  

POST /heima/_search
{
    "query":{
        "match_all":{}
    },
    "sort": [
      { "price": { "order": "desc" }},
      { "_score": { "order": "desc" }}
    ]
}

高亮

elasticsearch中实现高亮的语法比较简单:  

POST /heima/_search
{
  "query": {
    "match": {
      "title": "电视"
    }
  },
  "highlight": {
    "pre_tags": "<font color='pink'>",
    "post_tags": "</font>",
    "fields": {
      "title": {}
    }
  }
}

在使用match查询的同时,加上一个highlight属性:

  • pre_tags:前置标签

  • post_tags:后置标签

  • fields:需要高亮的字段

    • title:这里声明title字段需要高亮,后面可以为这个字段设置特有配置,也可以空

结果:  

{
  "took": 12,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.90204775,
    "hits": [
      {
        "_index": "heima",
        "_type": "goods",
        "_id": "363hTW0BTp_XthqB2lMR",
        "_score": 0.90204775,
        "_source": {
          "title": "小米电视4A",
          "images": "http://image.leyou.com/12479122.jpg",
          "price": 4288
        },
        "highlight": {
          "title": [
            "小米<font color='pink'>电视</font>4A"
          ]
        }
      }
    ]
  }
}

分页

elasticsearch中实现分页的语法非常简单:  

POST /heima/_search
{
  "query": {
    "match_all": {}
  },
  "size": 2,
  "from": 0
}

size:每页显示多少条

from:当前页起始索引, int start = (pageNum - 1) * size;

 小案例

制作测试数据

PUT /goods/fruit/1
{
  "name": "xiangjiao",
  "describe": "haochi tian",
  "price": 40,
  "producer": "feilvbin",
  "tags": [
    "xiangjiao",
    "haochi"
  ]
}
 
 
PUT /goods/fruit/2
{
  "name":"pingguo",
  "describe":"cui",
  "price":60,
  "producer":"zhongguo",
  "tags":["haokan","xiang"]
}
 
PUT /goods/fruit/3
{
  "name":"lizi",
  "describe":"zide",
  "price":10,
  "producer":"zhongguo",
  "tags":["suan","tian"]
}
 
PUT /goods/fruit/4
{
  "name":"boluo",
  "describe":"getouda",
  "price":74,
  "producer":"malaxiya",
  "tags":["huang","youci"]
}
 
PUT /goods/fruit/5
{
  "name":"mihoutao",
  "describe":"suan",
  "price":45,
  "producer":"xinxilan",
  "tags":["lv","huang"]
}
 
PUT /goods/fruit/6
{
  "name":"xigua",
  "describe":"haochi",
  "price":109,
  "producer":"zhongguo",
  "tags":["da","haochi"]
}
View Code

相关文章: