4.Elasticsearch查询

目录:
1.安装Elasticsearch
2.Elasticsearch概念
3.Elasticsearch索引
4.Elasticsearch查询
5.Elasticsearch聚合aggregations
6.Spring Data Elasticsearch入门
7.Repository文档操作

现有数据

{
     
  "took": 1,
  "timed_out": false,
  "_shards": {
     
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
     
    "total": 3,
    "max_score": 1,
    "hits": [
      {
     
        "_index": "dxbindex1",
        "_type": "goods",
        "_id": "2",
        "_score": 1,
        "_source": {
     
          "title": "小米电视",
          "images": "2.jpg",
          "price": 3999,
          "stock": 200,
          "saleable": true
        }
      },
      {
     
        "_index": "dxbindex1",
        "_type": "goods",
        "_id": "1",
        "_score": 1,
        "_source": {
     
          "title": "小米手机",
          "images": "1.jpg",
          "price": 2699,
          "stock": 100,
          "saleable": true
        }
      },
      {
     
        "_index": "dxbindex1",
        "_type": "goods",
        "_id": "3",
        "_score": 1,
        "_source": {
     
          "title": "apple手机",
          "images": "3.jpg",
          "price": 6899
        }
      }
    ]
  }
}

1、基本查询

基本语法

GET /索引库名/_search
{
     
    "query":{
     
        "查询类型":{
     
            "查询条件":"查询条件值"
        }
    }
}

这里的query代表一个查询对象,里面可以有不同的查询属性

  • 查询类型:
    • 例如:match_all, matchterm , range 等等
  • 查询条件:查询条件会根据类型的不同,写法也有差异。

1.1、查询所有(match_all)

1.2、匹配查询(match)

match类型查询,会把查询条件进行分词,然后进行查询,多个词条之间是or的关系。

示例

GET /dxbindex1/_search
{
     
    "query":{
     
        "match":{
     
            "title":"小米电视"
        }
    }
}

返回结果

{
     
  "took": 16,
  "timed_out": false,
  "_shards": {
     
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
     
    "total": 2,
    "max_score": 0.5753642,
    "hits": [
      {
     
        "_index": "dxbindex1",
        "_type": "goods",
        "_id": "2",
        "_score": 0.5753642,
        "_source": {
     
          "title": "小米电视",
          "images": "2.jpg",
          "price": 3999,
          "stock": 200,
          "saleable": true
        }
      },
      {
     
        "_index": "dxbindex1",
        "_type": "goods",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
     
          "title": "小米手机",
          "images": "1.jpg",
          "price": 2699,
          "stock": 100,
          "saleable": true
        }
      }
    ]
  }
}
  • took:查询花费时间,单位是毫秒
  • time_out:是否超时
  • _shards:分片信息
  • hits:搜索结果总览对象
    • total:搜索到的总条数
    • max_score:所有结果中文档得分的最高分
    • hits:搜索结果的文档对象数组,每个元素是一条搜索到的文档信息
      • _index:索引库
      • _type:文档类型
      • _id:文档id
      • _score:文档得分
      • _source:文档的源数据
and关系

某些情况下,我们需要更精确查找,我们希望这个关系变成and

示例

GET /dxbindex1/_search
{
     
    "query":{
     
        "match":{
     
            "title":{
     
                "query": "小米电视",
                "operator": "and"
            }
        }
    }
}

返回结果

{
     
  "took": 7,
  "timed_out": false,
  "_shards": {
     
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
     
    "total": 1,
    "max_score": 0.5753642,
    "hits": [
      {
     
        "_index": "dxbindex1",
        "_type": "goods",
        "_id": "2",
        "_score": 0.5753642,
        "_source": {
     
          "title": "小米电视",
          "images": "2.jpg",
          "price": 3999,
          "stock": 200,
          "saleable": true
        }
      }
    ]
  }
}
match 查询支持 minimum_should_match 最小匹配参数,我们可以将其设置为某个具体数字,更常用的做法是将其设置为一个百分数。这里是向下取整的。比如有5个查询字符,5*75%=3.75,向下取整为3,也就是至少需要match 3个字符。

示例

GET /dxbindex1/_search
{
     
    "query":{
     
        "match":{
     
            "title":{
     
                "query": "小米曲面电视",
            	  "minimum_should_match": "75%"
            }
        }
    }
}

返回结果

{
     
  "took": 2,
  "timed_out": false,
  "_shards": {
     
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
     
    "total": 1,
    "max_score": 0.5753642,
    "hits": [
      {
     
        "_index": "dxbindex1",
        "_type": "goods",
        "_id": "2",
        "_score": 0.5753642,
        "_source": {
     
          "title": "小米电视",
          "images": "2.jpg",
          "price": 3999,
          "stock": 200,
          "saleable": true
        }
      }
    ]
  }
}

1.3、多字段查询(multi_match)

multi_matchmatch类似,不同的是它可以在多个字段中查询。

示例

GET /dxbindex1/_search
{
     
    "query":{
     
        "multi_match": {
     
            "query":    "小米",
            "fields":   [ "title", "subTitle" ]
        }
	}
}

返回结果

{
     
  "took": 5,
  "timed_out": false,
  "_shards": {
     
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
     
    "total": 2,
    "max_score": 0.2876821,
    "hits": [
      {
     
        "_index": "dxbindex1",
        "_type": "goods",
        "_id": "2",
        "_score": 0.2876821,
        "_source": {
     
          "title": "小米电视",
          "images": "2.jpg",
          "price": 3999,
          "stock": 200,
          "saleable": true
        }
      },
      {
     
        "_index": "dxbindex1",
        "_type": "goods",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
     
          "title": "小米手机",
          "images": "1.jpg",
          "price": 2699,
          "stock": 100,
          "saleable": true
        }
      }
    ]
  }
}

1.4、词条匹配(term)

term 查询被用于精确值 匹配,这些精确值可能是数字、时间、布尔或者那些未分词的字符串。

示例

GET /dxbindex1/_search
{
     
    "query":{
     
        "term":{
     
            "price":2699.00
        }
    }
}

返回结果

{
     
  "took": 2,
  "timed_out": false,
  "_shards": {
     
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
     
    "total": 1,
    "max_score": 1,
    "hits": [
      {
     
        "_index": "dxbindex1",
        "_type": "goods",
        "_id": "1",
        "_score": 1,
        "_source": {
     
          "title": "小米手机",
          "images": "1.jpg",
          "price": 2699,
          "stock": 100,
          "saleable": true
        }
      }
    ]
  }
}

1.5、多词条精确匹配(terms)

terms 查询和 term 查询一样,但它允许你指定多值进行匹配。如果这个字段包含了指定值中的任何一个值,那么这个文档满足条件。

示例

GET /dxbindex1/_search
{
     
    "query":{
     
        "terms":{
     
            "price":[2699.00,2899.00,3999.00]
        }
    }
}

返回结果

{
     
  "took": 1,
  "timed_out": false,
  "_shards": {
     
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
     
    "total": 2,
    "max_score": 1,
    "hits": [
      {
     
        "_index": "dxbindex1",
        "_type": "goods",
        "_id": "2",
        "_score": 1,
        "_source": {
     
          "title": "小米电视",
          "images": "2.jpg",
          "price": 3999,
          "stock": 200,
          "saleable": true
        }
      },
      {
     
        "_index": "dxbindex1",
        "_type": "goods",
        "_id": "1",
        "_score": 1,
        "_source": {
     
          "title": "小米手机",
          "images": "1.jpg",
          "price": 2699,
          "stock": 100,
          "saleable": true
        }
      }
    ]
  }
}

2、结果过滤

2.1、直接返回指定字段

示例

GET /dxbindex1/_search
{
     
  "_source": ["title","price"],
  "query": {
     
    "term": {
     
      "price": 2699
    }
  }
}

返回结果

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

2.2、指定includes和excludes

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

示例

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

与下面结果一样

GET /dxbindex1/_search
{
     
  "_source": {
     
     "excludes": ["images","saleable","stock"]
  },
  "query": {
     
    "term": {
     
      "price": 2699
    }
  }
}

返回结果

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

3、高级查询

3.1、布尔组合(bool)

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

示例

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

返回结果

{
     
  "took": 4,
  "timed_out": false,
  "_shards": {
     
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
     
    "total": 1,
    "max_score": 0.5753642,
    "hits": [
      {
     
        "_index": "dxbindex1",
        "_type": "goods",
        "_id": "1",
        "_score": 0.5753642,
        "_source": {
     
          "title": "小米手机",
          "images": "1.jpg",
          "price": 2699,
          "stock": 100,
          "saleable": true
        }
      }
    ]
  }
}

3.2、范围查询(range)

示例

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

GET /dxbindex1/_search
{
     
    "query":{
     
        "range": {
     
            "price": {
     
                "gte":  1000.0,
                "lt":   2800.00
            }
    	}
    }
}

返回结果

{
     
  "took": 5,
  "timed_out": false,
  "_shards": {
     
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
     
    "total": 1,
    "max_score": 1,
    "hits": [
      {
     
        "_index": "dxbindex1",
        "_type": "goods",
        "_id": "1",
        "_score": 1,
        "_source": {
     
          "title": "小米手机",
          "images": "1.jpg",
          "price": 2699,
          "stock": 100,
          "saleable": true
        }
      }
    ]
  }
}

range查询允许以下字符:

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

3.3、模糊查询(fuzzy)

fuzzy 查询是 term 查询的模糊等价。它允许用户搜索词条与实际词条的拼写出现偏差,但是偏差的编辑距离不得超过2。

示例

GET /dxbindex1/_search
{
     
  "query": {
     
    "fuzzy": {
     
      "title": "appla"
    }
  }
}

返回结果

{
     
  "took": 1,
  "timed_out": false,
  "_shards": {
     
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
     
    "total": 1,
    "max_score": 0.6931472,
    "hits": [
      {
     
        "_index": "dxbindex1",
        "_type": "goods",
        "_id": "3",
        "_score": 0.6931472,
        "_source": {
     
          "title": "apple手机",
          "images": "3.jpg",
          "price": 6899
        }
      }
    ]
  }
}

通过fuzziness来指定允许的编辑距离:

GET /dxbindex1/_search
{
     
  "query": {
     
    "fuzzy": {
     
        "title": {
     
            "value":"appla",
            "fuzziness":0
        }
    }
  }
}

返回结果

{
     
  "took": 0,
  "timed_out": false,
  "_shards": {
     
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
     
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

4、过滤(filter)

4.1、条件查询中进行过滤

所有的查询都会影响到文档的评分及排名。如果我们需要在查询结果中进行过滤,并且不希望过滤条件影响评分,那么就不要把过滤条件作为查询条件来用。而是使用filter方式。
注意:filter中还可以再次进行bool组合条件过滤。

示例

GET /dxbindex1/_search
{
     
    "query":{
     
        "bool":{
     
        	"must":{
      "match": {
      "title": "小米手机" }},
        	"filter":{
     
                "range":{
     "price":{
     "gt":2000.00,"lt":3800.00}}
        	}
        }
    }
}

返回结果

{
     
  "took": 4,
  "timed_out": false,
  "_shards": {
     
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
     
    "total": 1,
    "max_score": 0.87546873,
    "hits": [
      {
     
        "_index": "dxbindex1",
        "_type": "goods",
        "_id": "1",
        "_score": 0.87546873,
        "_source": {
     
          "title": "小米手机",
          "images": "1.jpg",
          "price": 2699,
          "stock": 100,
          "saleable": true
        }
      }
    ]
  }
}

4.2、无查询条件,直接过滤

如果一次查询只有过滤,没有查询条件,不希望进行评分,我们可以使用constant_score取代只有 filter 语句的 bool 查询。在性能上是完全相同的,但对于提高查询简洁性和清晰度有很大帮助。

示例

GET /dxbindex1/_search
{
     
    "query":{
     
        "constant_score":   {
     
            "filter": {
     
            	 "range":{
     "price":{
     "gt":2000.00,"lt":3000.00}}
            }
        }
    }
}

返回结果

{
     
  "took": 4,
  "timed_out": false,
  "_shards": {
     
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
     
    "total": 1,
    "max_score": 1,
    "hits": [
      {
     
        "_index": "dxbindex1",
        "_type": "goods",
        "_id": "1",
        "_score": 1,
        "_source": {
     
          "title": "小米手机",
          "images": "1.jpg",
          "price": 2699,
          "stock": 100,
          "saleable": true
        }
      }
    ]
  }
}

5、排序

5.1、单字段排序

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

示例

GET /dxbindex1/_search
{
     
  "query": {
     
    "match": {
     
      "title": {
      
        "query" :"小米",
        "operator": "and"
      }
    }
  },
  "sort": [
    {
     
      "price": {
     
        "order": "desc"
      }
    }
  ]
}

返回结果

{
     
  "took": 1,
  "timed_out": false,
  "_shards": {
     
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
     
    "total": 2,
    "max_score": null,
    "hits": [
      {
     
        "_index": "dxbindex1",
        "_type": "goods",
        "_id": "2",
        "_score": null,
        "_source": {
     
          "title": "小米电视",
          "images": "2.jpg",
          "price": 3999,
          "stock": 200,
          "saleable": true
        },
        "sort": [
          3999
        ]
      },
      {
     
        "_index": "dxbindex1",
        "_type": "goods",
        "_id": "1",
        "_score": null,
        "_source": {
     
          "title": "小米手机",
          "images": "1.jpg",
          "price": 2699,
          "stock": 100,
          "saleable": true
        },
        "sort": [
          2699
        ]
      }
    ]
  }
}

5.2、多字段排序

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

示例

GET dxbindex1/goods/_search
{
     
    "query":{
     
        "bool":{
     
        	"must":{
      "match": {
      "title": "小米手机" }},
        	"filter":{
     
                "range":{
     "price":{
     "gt":2000,"lt":4000}}
        	}
        }
    },
    "sort": [
      {
      "price": {
      "order": "desc" }},
      {
      "_score": {
      "order": "desc" }}
    ]
}

返回结果

{
     
  "took": 3,
  "timed_out": false,
  "_shards": {
     
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
     
    "total": 2,
    "max_score": null,
    "hits": [
      {
     
        "_index": "dxbindex1",
        "_type": "goods",
        "_id": "2",
        "_score": 0.2876821,
        "_source": {
     
          "title": "小米电视",
          "images": "2.jpg",
          "price": 3999,
          "stock": 200,
          "saleable": true
        },
        "sort": [
          3999,
          0.2876821
        ]
      },
      {
     
        "_index": "dxbindex1",
        "_type": "goods",
        "_id": "1",
        "_score": 0.87546873,
        "_source": {
     
          "title": "小米手机",
          "images": "1.jpg",
          "price": 2699,
          "stock": 100,
          "saleable": true
        },
        "sort": [
          2699,
          0.87546873
        ]
      }
    ]
  }
}

你可能感兴趣的:(Elasticsearch,elasticsearch,java)