POST library/_search
{
"_source": {
"includes": ["title", "available"]
},
"query": {
"match_all": {}
},
"rescore":{
"query":{
"rescore_query":{
"function_score":{
"query":{
"match_all":{}
},
"script_score":{
"script":"doc['year'].value"
}
}
}
}
}
}
GET library/_search
{
"query": {
"multi_match": {
"query": "complete conan doyle",
"fields": ["title", "author", "characters"]
}
}
}
这种方式,逐渐被废弃和替换
GET library/_search
{
"query": {
"multi_match": {
"query": "complete conan doyle",
"fields": ["title", "author", "charactor"],
"use_dis_max": true
}
}
}
GET library/_search
{
"query": {
"multi_match": {
"query": "complete conan doyle",
"fields": ["title", "author", "characters"],
"type":"best_fields",
"tie_breaker": 0.8
}
}
}
GET library/_search
{
"query": {
"dis_max": {
"tie_breaker": 0.8,
"queries": [
{
"match": {
"title": "complete conan doyle"
}
},{
"match": {
"author": "complete conan doyle"
}
},{"match":{
"characters": "complete conan doyle"
}}]
}
}
}
GET library/_search
{
"query": {
"multi_match": {
"query": "complete conan doyle",
"fields": ["title", "author", "characters"],
"type":"cross_fields",
"operator":"and"
}
}
}
POST library/_search
{
"query": {
"multi_match": {
"query": "Die Leiden",
"fields": ["title", "otitle"],
"type":"most_fields"
}
}
}
GET library/_search
{
"query": {
"bool": {
"should": [
{"match": {
"title": "die leiden"
}},
{
"match": {
"otitle": "die leiden"
}
}
]
}
}
}
GET library/_search
{
"query": {
"multi_match": {
"query": "sherlock holmes",
"fields": ["title","author"],
"type":"phrase"
}
}
}
GET library/_search
{
"query": {
"dis_max": {
"queries": [
{
"match_phrase":{"title":"sherlock holmes"}},
{ "match_phrase":{
"author":"sherlock holmes"
}}]
}
}
}
GET library/_search
{
"query": {
"multi_match": {
"query": "sherlock hol",
"fields": ["title","author"],
"type":"phrase_prefix"
}
}
}
GET library/_search
{
"query": {
"dis_max": {
"queries": [
{
"match_phrase_prefix":{"title":"sherlock holmes"}},
{ "match_phrase_prefix":{
"author":"sherlock holmes"
}}]
}
}
}
#7、聚合操作
POST interns/review/1
{
"intern":"Richard",
"grade":"bad",
"type":"grade"
}
POST interns/review/2
{
"intern":"Ralf",
"grade":"perfact",
"type":"grade"
}
POST interns/review/3
{
"intern":"Richard",
"grade":"bad",
"type":"grade"
}
POST interns/review/3
{
"intern":"Richard",
"grade":"bad",
"type":"grade"
}
POST interns/review/4
{
"intern":"Richard",
"grade":"bad",
"type":"review"
}
POST interns/review/5
{
"intern":"Richard",
"grade":"good",
"type":"grade"
}
POST interns/review/6
{
"intern":"Ralf",
"grade":"good",
"type":"grade"
}
POST interns/review/7
{
"intern":"Ralf",
"grade":"perfect",
"type":"grade"
}
POST interns/review/8
{
"intern":"Richard",
"grade":"medium",
"type":"review"
}
POST interns/review/9
{
"intern":"Monica",
"grade":"medium",
"type":"grade"
}
POST interns/review/10
{
"intern":"Monica",
"grade":"medium",
"type":"grade"
}
POST interns/review/11
{
"intern":"Ralf",
"grade":"good",
"type":"grade"
}
POST interns/review/12
{
"intern":"Ralf",
"grade":"good",
"type":"grade"
}
//聚合——查找对于Richard的典型评价
GET /interns/_search
{
"query": {
"match": {
"intern": "Richard"
}
},
"aggregations":{
"description":{
"significant_terms": {
"field": "grade.keyword"
}
}
}
}
GET library/_search
{
"query": {
"term":{
"available": true
}
},
"aggregations":{
"description":{
"significant_terms":
{
"field":"title.keyword"
}
}
}
}
GET /interns/_search
{
"query": {
"match": {
"intern": "Richard"
}
},
"aggregations":{
"description":{
"significant_terms": {
"field": "grade.keyword",
"background_filter": {
"term":{
"type":"grade"
}
}
}
}
}
}
GET /interns/_search
{
"query": {
"match": {
"intern": "Richard"
}
},
"aggregations":{
"description":{
"significant_terms": {
"field": "grade.keyword",
"min_doc_count": 3
}
}
}
}
GET library/_search
GET library/_search
{
"size":0,
"aggs":{
"when":{
"histogram": {
"field": "year",
"interval": 100
},
"aggs":{
"book":{
"top_hits": {
"sort":{
"year":"asc"
},
"_source":{
"includes":["title","avaliable"]
}
}
}
}
}
}
}
GET library/_search
{
"query": {
"bool":{
"filter": {
"term":{
"copies": 1
}
}
}
},
"size":0,
"aggs": {
"when": {
"histogram": {
"field": "year",
"interval": 100
},
"aggs": {
"book": {
"top_hits": {
"highlight": {
"fields": {
"title": {}
}
},
"explain": true,
"version": true,
"_source": {
"includes": ["title","avalible"]},
"script_fields":{
"century":{
"script":"(doc[\"year\"].value / 100).intValue()"
}
},
"size":1
}
}
}
}
}
}
注意terms聚合和significant_terms的区别: 前者返回全部信息,后者返回最重要,得分最高的信息。
GET /interns/_search
{
"query": {
"match": {
"intern": "Richard"
}
},
"aggs":{
"description":{
"terms": {
"field": "grade.keyword"
}
}
}
}
PUT object
POST object/_mapping/doc
{
"doc":{
"properties":{
"title":{"type":"text",
"fields":{
"keyword":{
"type":"keyword"
}
}
},
"quantity":{"type":"integer"},
"edition":{
"type":"object",
"properties":{
"isbn":{"type":"keyword"},
"circulation":{"type":"integer"}
}
}
}
}
}
POST object/doc/1
{
"title":"Title",
"quantity":100,
"edition":[
{
"isbn":"1234567890",
"circulation":50000
},
{
"isbn":"9876543210",
"circulation":2000
}
]
}
GET object/_search
{
"_source": {
"includes": ["_id","title"]
},
"query": {
"bool": {
"must": [
{"term":{
"edition.isbn": "1234567890"
}},
{"term": {
"edition.circulation": {
"value": 2000
}
}}]
}
}
}
PUT nested_test
POST nested_test/_mapping/doc
{
"doc":{
"properties":{
"title":{"type":"text",
"fields":{
"keyword":{
"type":"keyword"
}
}
},
"quantity":{"type":"integer"},
"edition":{
"type":"nested",
"properties":{
"isbn":{"type":"keyword"},
"circulation":{"type":"integer"}
}
}
}
}
}
POST nested_test/doc/1
{
"title":"Title",
"quantity":100,
"edition":[
{
"isbn":"1234567890",
"circulation":50000
},
{
"isbn":"9876543210",
"circulation":2000
}
]
}
GET nested_test/_search
{
"_source": {
"includes": ["_id","title"]
},
"query": {
"bool": {
"must": [
{"term":{
"edition.isbn": "1234567890"
}},
{"term": {
"edition.circulation": {
"value": 2000
}
}}]
}
}
}
_index方式在6.X版本已不支持,如下官网介绍: https://www.elastic.co/guide/en/elasticsearch/reference/6.x/modules-advanced-scripting.html
GET library/_search
{
"_source": {
"includes": ["_id", "_score", "title", "year"]
},
"query": {
"function_score": {
"query": {"match_all": {}},
"script_score": {
"script":"def year = doc[\"year\"].value; if (year < 1800) { return 1.0} else if (year < 1900) { return 2.0 } else { return year - 1000 }"
}
}
}
}