Elasticsearch的高级查询

1,447 阅读4分钟

#Elasticsearch的高级查询

子条件查询

特定字段查询指定特定的值

Query context

在查询的过程中,除了判断文档是否满足条件之外,ES还会计算一个_score来标识匹配的程度,旨在判断目标文档和查询条件匹配有多好。

常用查询

全文本查询

针对文本类型数据

模糊匹配
url:http://localhost:9200/book/_doc/_search
请求体如下:
{
	"query":{
		"match":{
			"title":"Elasticsearch入门"
		}
	}
}

返回的结果如下:

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 0.3439677,
        "hits": [
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "3",
                "_score": 0.3439677,
                "_source": {
                    "author": "王五",
                    "word_count": 2000,
                    "title": "Elasticsearch入门",
                    "publish_date": "2018-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "4",
                "_score": 0.3439677,
                "_source": {
                    "author": "赵六",
                    "word_count": 100000,
                    "title": "Elasticsearch进阶",
                    "publish_date": "2017-11-11"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "10",
                "_score": 0.26592463,
                "_source": {
                    "author": "jack",
                    "word_count": 5000,
                    "title": "Elasticsearch的深入理解",
                    "publish_date": "2018-05-09"
                }
            }
        ]
    }
}

模糊匹配的问题:模糊匹配会把相关词的匹配的此都返回来了。

词语匹配
url:http://localhost:9200/book/_doc/_search
请求体:{
	"query":{
		"match_phrase":{
			"title":"Elasticsearch入门"
		}
	}
}

返回结果:

{
    "took": 7,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 2.6459458,
        "hits": [
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "3",
                "_score": 2.6459458,
                "_source": {
                    "author": "王五",
                    "word_count": 2000,
                    "title": "Elasticsearch入门",
                    "publish_date": "2018-10-01"
                }
            }
        ]
    }
}

######多字段匹配(multi_match)

url:http://localhost:9200/book/_doc/_search
请求体:
{
	"query":{
		"multi_match":{
			"query":"李启明",
			"fields":["author","title"]
		}
	}
}

返回结果

{
    "took": 6,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 3.5740404,
        "hits": [
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "2",
                "_score": 3.5740404,
                "_source": {
                    "author": "李四",
                    "word_count": 2000,
                    "title": "李启明的成长记",
                    "publish_date": "2018-06-01"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "9",
                "_score": 3.0162745,
                "_source": {
                    "author": "旺仔",
                    "word_count": 2000,
                    "title": "李启明回忆录",
                    "publish_date": "2017-06-01"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "8",
                "_score": 1.5404451,
                "_source": {
                    "author": "李启明",
                    "word_count": 2000,
                    "title": "旺仔回忆录",
                    "publish_date": "2017-06-02"
                }
            }
        ]
    }
}

######语法查询(query_string)

url:http://localhost:9200/book/_doc/_search
请求体:
{
	"query":{
		"query_string":{
			"query":"李启明 OR Elasticsearch",
			"fields":["author","title"]
			
		}
	}
}

返回结果:

{
    "took": 16,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 6,
            "relation": "eq"
        },
        "max_score": 3.5740404,
        "hits": [
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "2",
                "_score": 3.5740404,
                "_source": {
                    "author": "李四",
                    "word_count": 2000,
                    "title": "李启明的成长记",
                    "publish_date": "2018-06-01"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "9",
                "_score": 3.0162745,
                "_source": {
                    "author": "旺仔",
                    "word_count": 2000,
                    "title": "李启明回忆录",
                    "publish_date": "2017-06-01"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "8",
                "_score": 1.5404451,
                "_source": {
                    "author": "李启明",
                    "word_count": 2000,
                    "title": "旺仔回忆录",
                    "publish_date": "2017-06-02"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "3",
                "_score": 0.3439677,
                "_source": {
                    "author": "王五",
                    "word_count": 2000,
                    "title": "Elasticsearch入门",
                    "publish_date": "2018-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "4",
                "_score": 0.3439677,
                "_source": {
                    "author": "赵六",
                    "word_count": 100000,
                    "title": "Elasticsearch进阶",
                    "publish_date": "2017-11-11"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "10",
                "_score": 0.26592463,
                "_source": {
                    "author": "jack",
                    "word_count": 5000,
                    "title": "Elasticsearch的深入理解",
                    "publish_date": "2018-05-09"
                }
            }
        ]
    }
}
字段级别的查询

针对结构化数据,如数字,日期等。

######范围查询

url:http://localhost:9200/book/_doc/_search
请求体:
{
	"query":{
		"range":{
			"word_count":{
				"gte":1000,
				"lte":2000
				
			}
		}
	}
}

返回结果:

{
    "took": 10,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 5,
            "relation": "eq"
        },
        "max_score": 1,
        "hits": [
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "2",
                "_score": 1,
                "_source": {
                    "author": "李四",
                    "word_count": 2000,
                    "title": "李启明的成长记",
                    "publish_date": "2018-06-01"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "3",
                "_score": 1,
                "_source": {
                    "author": "王五",
                    "word_count": 2000,
                    "title": "Elasticsearch入门",
                    "publish_date": "2018-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "author": "张三",
                    "word_count": 1000,
                    "title": "旺仔的成长记",
                    "publish_date": "2018-06-02"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "8",
                "_score": 1,
                "_source": {
                    "author": "李启明",
                    "word_count": 2000,
                    "title": "旺仔回忆录",
                    "publish_date": "2017-06-02"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "9",
                "_score": 1,
                "_source": {
                    "author": "旺仔",
                    "word_count": 2000,
                    "title": "李启明回忆录",
                    "publish_date": "2017-06-01"
                }
            }
        ]
    }
}

Filter context

在查询过程种,只判断该文档是否满足条件,只有Yes或者No.filter需要结婚bool一起使用,filter查询由于es会做缓存,索引在查询速度上会快过query查询

url:http://localhost:9200/book/_doc/_search
请求体:
{
"query":{
	"bool":{
		"filter":{
			"term":{
				"word_count":1000
			}
		}
		
	}
}
}

返回结果:

{
    "took": 5,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0,
        "hits": [
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "1",
                "_score": 0,
                "_source": {
                    "author": "张三",
                    "word_count": 1000,
                    "title": "旺仔的成长记",
                    "publish_date": "2018-06-02"
                }
            }
        ]
    }
}

复合条件查询

以一定的逻辑组合子条件查询

固定分数查询(只支持filter和bool查询)

url:http://localhost:9200/book/_doc/_search
reuqest body:

{
	"query":{
		"constant_score":{
			"filter":{
				"match":{
					"title":"Elasticsearch"
				}
			},
			#可以设置分数
			"boost":2
			
		}
	}
}

返回结果:

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 2,
        "hits": [
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "3",
                "_score": 2,
                "_source": {
                    "author": "王五",
                    "word_count": 2000,
                    "title": "Elasticsearch入门",
                    "publish_date": "2018-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "4",
                "_score": 2,
                "_source": {
                    "author": "赵六",
                    "word_count": 100000,
                    "title": "Elasticsearch进阶",
                    "publish_date": "2017-11-11"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "10",
                "_score": 2,
                "_source": {
                    "author": "jack",
                    "word_count": 5000,
                    "title": "Elasticsearch的深入理解",
                    "publish_date": "2018-05-09"
                }
            }
        ]
    }
}

布尔查询

{ "query":{ "bool":{ "must_not":{ "term":{ "title":"Elasticsearch" } } } } }

...more

{
	"query":{
		"bool":{
			"must_not":{
				"term":{
					"title":"Elasticsearch"
				}
			},
			"filter":[
				{
					"match":{
						"author":"李启明"
					}
				}
				]
		}
	}
}

返回结果:

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0,
        "hits": [
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "8",
                "_score": 0,
                "_source": {
                    "author": "李启明",
                    "word_count": 2000,
                    "title": "旺仔回忆录",
                    "publish_date": "2017-06-02"
                }
            }
        ]
    }
}