Elasticsearch核心知识篇(49)
初识搜索引擎_filter与query深入对比解密:相关度,性能
filter与query示例
# 新增一些数据用于测试
PUT /waws_company/employee/1
{
"address": {
"country": "china",
"province": "jiangsu",
"city": "nanjing"
},
"name": "tom",
"age": 30,
"join_date": "2016-01-01"
}
PUT /waws_company/employee/2
{
"address": {
"country": "china",
"province": "shanxi",
"city": "xian"
},
"name": "marry",
"age": 35,
"join_date": "2015-01-01"
}
PUT /waws_company/employee/3
{
"address": {
"country": "china",
"province": "jiangsu",
"city": "nanjing"
},
"name": "waws",
"age": 37,
"join_date": "2016-01-01"
}
搜索请求:年龄必须大于等于31,同时join_date必须是2016-01-01
GET /waws_company/employee/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"join_date": "2016-01-01"
}
}
],
"filter": { #filter也是在bool中
"range": {
"age": {
"gte": 31
}
}
}
}
}
}
{
"took": 37,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "waws_company",
"_type": "employee",
"_id": "3",
"_score": 1,
"_source": {
"address": {
"country": "china",
"province": "jiangsu",
"city": "nanjing"
},
"name": "waws",
"age": 37,
"join_date": "2016-01-01"
}
}
]
}
}
filter与query对比大解密
- filter,仅仅只是按照搜索条件过滤出需要的数据而已,不计算任何相关度分数,对相关度没有任何影响
- query,会去计算每个document相对于搜索条件的相关度,并按照相关度进行排序
一般来说,如果你是在进行搜索,需要将最匹配搜索条件的数据先返回,那么用query;如果你只是要根据一些条件筛选出一部分数据,不关注其排序,那么用filter 除非是你的这些搜索条件,你希望越符合这些搜索条件的document越排在前面返回,那么这些搜索条件要放在query中;如果你不希望一些搜索条件来影响你的document排序,那么就放在filter中即可
filter与query性能
- filter,不需要计算相关度分数,不需要按照相关度分数进行排序,同时还有内置的自动cache最常使用filter的数据
- query,相反,要计算相关度分数,按照分数进行排序,而且无法cache结果
Elasticsearch核心知识篇(50)
初识搜索引擎_上机动手实战常用的各种query搜索语法(重点
)
- match all
GET /_search
{
"query": {
"match_all": {}
}
}
- match
GET /_search
{
"query": { "match": { "title": "my elasticsearch article" }}
}
- multi match
GET /waws_index/waws_type/_search
{
"query": {
"multi_match": {
"query": "test",
"fields": ["test_field", "test_field1"]
}
}
}
- range query
GET /waws_index/waws_type/_search
{
"query": {
"range": {
"age": {
"gte": 30
}
}
}
}
- term query
GET /waws_index/waws_type/_search
{
"query": {
"term": {
"test_field": "test hello" # 将field当成是一个整个串,不会分词
}
}
}
- terms query
GET /_search
{
"query": { "terms": { "tag": [ "search", "full_text", "nosql" ] }}
}
- exist query(2.x中的查询,现在已经不提供了)
Elasticsearch核心知识篇(51)
初识搜索引擎_上机动手实战多搜索条件组合查询
组合查询
- 例程1
GET /waws_web/waws_article/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "elasticsearch"
}
}
],
"should": [
{
"match": {
"content": "elasticsearch"
}
}
],
"must_not": [
{
"match": {
"author_id": 111
}
}
]
}
}
}
- 例程2
GET /waws_web/waws_article/_search
{
"bool": {
"must": { "match": { "title": "how to make millions" }},
"must_not": { "match": { "tag": "spam" }},
"should": [
{ "match": { "tag": "starred" }}
],
"filter": {
"range": { "date": { "gte": "2014-01-01" }}
}
}
}
- bool
- must
- must_not
- should
- filter
-
每个子查询都会计算一个document针对它的相关度分数,然后
bool综合所有分数
,合并为一个分数,当然filter是不会计算分数的 -
例程3
GET /waws_web/waws_article/_search
{
"bool": {
"must": { "match": { "title": "how to make millions" }},
"must_not": { "match": { "tag": "spam" }},
"should": [
{ "match": { "tag": "starred" }}
],
"filter": {
"bool": {
"must": [
{ "range": { "date": { "gte": "2014-01-01" }}},
{ "range": { "price": { "lte": 29.99 }}}
],
"must_not": [
{ "term": { "category": "ebooks" }}
]
}
}
}
}
- 例程4
GET /company/employee/_search
{
"query": {
"constant_score": {
"filter": {
"range": {
"age": {
"gte": 30
}
}
}
}
}
}