Elasticsearch 1: 基本原理和概念
Elasticsearch 2: 管理索引和文档
Elasticsearch 3: 数据检索和分析
Elasticsearch 4: 相关性检索和组合查询
Elasticsearch 5: 聚集查询
Elasticsearch 6: 索引别名
Elasticsearch 集群
SpringBoot 整合 Elasticsearch
在全文检索中,检索结果与查询条件的相关性是一个极为重要的问题,优秀 的全文检索引擎应该将那些与查询条件相关性高的文档排在最前面。想象一下。 如果满足查询条件的文档成千上万,让用户在这些文档中再找出自己最满意的那 一条,这无异于再做一次人工检索。用户一般很少会有耐心在检索结果中翻到第 3 页,所以处理好检索结果的相关性对于检索引擎来说至关重要。
相关性问题有两方面问题要解决, 一是如何评价单个查询条件的相关性, 二是如何将多个查询条件的相关性组合起来。
文中使用的数据可以参考Elasticsearch 数据检索和分析导入数据相关内容
POST /kibana_sample_data_logs/_search
{
"query": {
"match": {
"message": "chrome"
}
},
"explain": true
}
POST /kibana_sample_data_logs/_explain/To9w9H4Bo-eQ1KgLEh-h
{
"query": {
"match": {
"message": "chrome"
}
}
}
POST /kibana_sample_data_flights/_search
{
"query": {
"range": {
"AvgTicketPrice": {
"gte": 1000,
"lte": 1200,
"boost": 2
}
}
}
}
POST /kibana_sample_data_logs/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"message": "firefox"
}
}
],
"should": [
{
"term": {
"geo. src": "CN"
}
},
{
"term": {
"geo. dest": "CN"
}
}
]
}
}
}
POST /kibana_sample_data_logs/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"message": "firefox"
}
}
],
"should": [
{
"term": {
"geo.src": "CN"
}
},
{
"term": {
"geo.dest": "CN"
}
}
]
}
},
"sort": [
{
"_score": {
"order": "asc"
}
}
]
}
POST /kibana_sample_data_logs/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"message": "firefox"
}
}
],
"should": [
{
"term": {
"geo. src": "CN"
}
},
{
"term": {
"geo. dest": "CN"
}
}
],
"filter": {
"term": {
"extension": "zip"
}
}
}
}
}
POST /kibana_sample_data_logs/_search
{
"query": {
"dis_max": {
"queries": [
{
"match": {
"message": "firefox"
}
},
{
"term": {
"geo. src": "CN"
}
},
{
"term": {
"geo. dest": "CN"
}
}
]
}
}
}
POST /kibana_sample_data_logs/_search
{
"query": {
"dis_max": {
"queries": [
{
"match": {
"message": "firefox"
}
},
{
"term": {
"geo. src": "CN"
}
},
{
"term": {
"geo. dest": "CN"
}
}
],
"tie_breaker": 0.7
}
}
}
POST /kibana_sample_data_logs/_search
{
"query": {
"constant_score": {
"filter": {
"match": {
"geo.src": "CN"
}
},
"boost": 1.3
}
}
}
POST /kibana_sample_data_logs/_search
{
"query": {
"boosting": {
"positive": {
"term": {
"geo.src": "CN"
}
},
"negative": {
"term": {
"geo. dest": "CN"
}
},
"negative_boost": 0.2
}
},
"sort": [
{
"_score": "asc"
}
]
}
POST /kibana_sample_data_logs/_search
{
"query": {
"function_score": {
"query": {
"query_string": {
"fields": [
"message"
],
"query": "(firefox 6.0a1) OR (chrome 11.0.696.50)"
}
},
"functions": [
{
"weight": 2
},
{
"random_score": {
}
}
],
"score_mode": "max",
"boost_mode": "avg"
}
}
}
POST /kibana_sample_data_flights/_search
{
"query": {
"function_score": {
"query": {
"bool": {
"must": [
{
"match": {
"OriginCountry": "CN"
}
},
{
"match": {
"DestCountry": "US"
}
}
]
}
},
"field_value_factor": {
"field": "AvgTicketPrice",
"factor": 0.001,
"modifier": "reciprocal",
"missing": 1000
}
}
}
}
POST /kibana_sample_data_flights/_search
{
"query": {
"function_score": {
"query": {
"match": {
"OriginCityName": "Beijing"
}
},
"gauss": {
"timestamp": {
"origin": "2022-02-24",
"scale": "7d",
"offset": "1d",
"decay": 0.3
}
}
}
}
}
POST /kibana_sample_data_flights/_search
{
"query": {
"multi_match": {
"query": "CN",
"fields": [
"OriginCountry^2",
"DestCountry"
],
"type": "best_fields"
}
}
}
{
"title": "Quick brown rabbits",
"body": "Brown rabbits are commonly seen."
}
{
"title": "Keeping pets healthy",
"body": "My quick brown fox eats rabbits on a regular basis."
}
POST /kibana_sample_data_flights/_search
{
"query": {
"dis_max": {
"queries": [
{
"match": {
"OriginCountry": {
"query": "CN",
"boost": 2
}
}
},
{
"match": {
"DestCountry": "CN"
}
}
]
}
}
}
POST /kibana_sample_data_flights/_search
{
"query": {
"multi_match": {
"query": "CN",
"fields": [
"OriginCountry^2",
"DestCountry"
],
"type": "most_fields"
}
}
}
POST /kibana_sample_data_flights/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"OriginCountry": {
"query": "CN",
"boost": 2
}
}
},
{
"match": {
"DestCountry": "CN"
}
}
]
}
}
}
POST /kibana_sample_data_logs/_search
{
"query": {
"multi_match": {
"query": "firefox success",
"fields": [
"message",
"tags"
],
"type": "best_fields",
"operator": "and"
}
}
}
POST /kibana_sample_data_logs/_search
{
"query": {
"multi_match": {
"query": "firefox success",
"fields": [
"message",
"tags"
],
"type": "cross_fields",
"operator": "and"
}
}
}