您的位置:首页 > Web前端

[elasticsearch笔记] Query DSL- distance_feature/more_like_this/rank_feature/script/wrapper query

2019-07-26 14:05 1126 查看

文章目录

distance_feature

PUT items
{
"mappings": {
"properties": {
"name": {
"type": "keyword"
},
"production_date": {
"type": "date"
},
"location": {
"type": "geo_point"
}
}
}
}

PUT items/_doc/1
{
"name" : "chocolate",
"production_date": "2018-02-01",
"location": [-71.34, 41.12]
}

PUT items/_doc/2
{
"name" : "chocolate",
"production_date": "2018-01-01",
"location": [-71.3, 41.15]
}

PUT items/_doc/3
{
"name" : "chocolate",
"production_date": "2017-12-01",
"location": [-71.3, 41.12]
}

POST items/_refresh

#
# score = boost * pivot / (pivot + distance)
#
GET items/_search
{
"query": {
"bool": {
"must": {
"match": {
"name": "chocolate"
}
},
"should": {
"distance_feature": {
"field": "production_date",
"pivot": "7d",
"origin": "now"
}
}
}
}
}

GET items/_search
{
"query": {
"bool": {
"must": {
"match": {
"name": "chocolate"
}
},
"should": {
"distance_feature": {
"field": "location",
"pivot": "1000m",
"origin": [-71.3, 41.15]
}
}
}
}
}

more_like_this

GET /_search
{
"query": {
"more_like_this" : {
"fields" : ["title", "description"],
"like" : "Once upon a time",
"min_term_freq" : 1,
"max_query_terms" : 12
}
}
}

GET /_search
{
"query": {
"more_like_this" : {
"fields" : ["title", "description"],
"like" : [
{
"_index" : "imdb",
"_id" : "1"
},
{
"_index" : "imdb",
"_id" : "2"
},
"and potentially some more text here as well"
],
"min_term_freq" : 1,
"max_query_terms" : 12
}
}
}

GET /_search
{
"query": {
"more_like_this" : {
"fields" : ["name.first", "name.last"],
"like" : [
{
"_index" : "marvel",
"doc" : {
"name": {
"first": "Ben",
"last": "Grimm"
},
"_doc": "You got no idea what I'd... what I'd give to be invisible."
}
},
{
"_index" : "marvel",
"_id" : "2"
}
],
"min_term_freq" : 1,
"max_query_terms" : 12
}
}
}

PUT /imdb
{
"mappings": {
"properties": {
"title": {
"type": "text",
"term_vector": "yes"
},
"description": {
"type": "text"
},
"tags": {
"type": "text",
"fields" : {
"raw": {
"type" : "text",
"analyzer": "keyword",
"term_vector" : "yes"
}
}
}
}
}
}

rank_feature

The rank_feature query is a specialized query that only works on rank_feature fields and rank_features fields.

Its goal is to boost the score of documents based on the values of numeric features.
It is typically put in a should clause of a bool query so that its score is added to the score of the query.

PUT rank_feature_index
{
"mappings": {
"properties": {
"pagerank": {
"type": "rank_feature"
},
"url_length": {
"type": "rank_feature",
"positive_score_impact": false
},
"topics": {
"type": "rank_features"
}
}
}
}

PUT rank_feature_index/_doc/1
{
"url": "http://en.wikipedia.org/wiki/2016_Summer_Olympics",
"content": "Rio 2016",
"pagerank": 50.3,
"url_length": 42,
"topics": {
"sports": 50,
"brazil": 30
}
}

PUT rank_feature_index/_doc/2
{
"url": "http://en.wikipedia.org/wiki/2016_Brazilian_Grand_Prix",
"content": "Formula One motor race held on 13 November 2016 at the Autódromo José Carlos Pace in São Paulo, Brazil",
"pagerank": 50.3,
"url_length": 47,
"topics": {
"sports": 35,
"formula one": 65,
"brazil": 20
}
}

PUT rank_feature_index/_doc/3
{
"url": "http://en.wikipedia.org/wiki/Deadpool_(film)",
"content": "Deadpool is a 2016 American superhero film",
"pagerank": 50.3,
"url_length": 37,
"topics": {
"movies": 60,
"super hero": 65
}
}

POST rank_feature_index/_refresh

GET rank_feature_index/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"content": "2016"
}
}
],
"should": [
{
"rank_feature": {
"field": "pagerank"
}
},
{
"rank_feature": {
"field": "url_length",
"boost": 0.1
}
},
{
"rank_feature": {
"field": "topics.sports",
"boost": 0.4
}
}
]
}
}
}

# S is the value of the rank feature
# S / (S + pivot)
# If the rank feature has a negative score impact then the function will be computed as pivot / (S + pivot), which decreases when S increases.
GET rank_feature_index/_search
{
"query": {
"rank_feature": {
"field": "pagerank",
"saturation": {
"pivot": 8
}
}
}
}

#
GET rank_feature_index/_search
{
"query": {
"rank_feature": {
"field": "pagerank",
"saturation": {}
}
}
}

# log(scaling_factor + S)
GET rank_feature_index/_search
{
"query": {
"rank_feature": {
"field": "pagerank",
"log": {
"scaling_factor": 4
}
}
}
}

# S^exp^ / (S^exp^ + pivot^exp^)
GET rank_feature_index/_search
{
"query": {
"rank_feature": {
"field": "pagerank",
"sigmoid": {
"pivot": 7,
"exponent": 0.6
}
}
}
}

rank features

PUT rank_features_index
{
"mappings": {
"properties": {
"topics": {
"type": "rank_features"
}
}
}
}

PUT rank_features_index/_doc/1
{
"topics": {
"politics": 20,
"economics": 50.8
}
}

PUT rank_features_index/_doc/2
{
"topics": {
"politics": 5.2,
"sports": 80.1
}
}

GET rank_features_index/_search
{
"query": {
"rank_feature": {
"field": "topics.politics"
}
}
}

script

GET kibana_sample_data_flights/_search?size=1
GET kibana_sample_data_flights/_search
{
"query": {
"bool": {
"filter": {
"script": {
"script": {
"source": "doc['FlightTimeMin'].value > 1500",
"lang": "painless"
}
}
}
}
}
}

GET kibana_sample_data_flights/_search
{
"query": {
"bool": {
"filter": {
"script": {
"script": {
"source": "doc['FlightTimeMin'].value > params.param1",
"lang": "painless",
"params": {
"param1": 1500
}
}
}
}
}
}
}

script_score

详情

GET kibana_sample_data_flights/_search?size=1
#
# saturation(value,k) = value/(k + value)
# demo: saturation(doc['FlightTimeMin'].value,1)
# sigmoid(value, k, a) = value^a/ (k^a + value^a)
# demo: sigmoid(doc['FlightTimeMin'].value,2,1)
# randomScore(100)
GET kibana_sample_data_flights/_search
{
"query": {
"script_score": {
"query": {
"match": {
"DestCountry": "AU"
}
},
"script": {
"source": "randomScore(100)"
}
}
}
}

# saturation(value,k) = value/(k + value)
"script" : {
"source" : "saturation(doc['likes'].value, 1)"
}

# sigmoid(value, k, a) = value^a/ (k^a + value^a)
"script" : {
"source" : "sigmoid(doc['likes'].value, 2, 1)"
}

"script" : {
"source" : "randomScore(100)"
}

"script" : {
"source" : "decayNumericLinear(params.origin, params.scale, params.offset, params.decay, doc['dval'].value)",
"params": {
"origin": 20,
"scale": 10,
"decay" : 0.5,
"offset" : 0
}
}

"script" : {
"source" : "decayGeoExp(params.origin, params.scale, params.offset, params.decay, doc['location'].value)",
"params": {
"origin": "40, -70.12",
"scale": "200km",
"offset": "0km",
"decay" : 0.2
}
}

"script" : {
"source" : "decayDateGauss(params.origin, params.scale, params.offset, params.decay, doc['date'].value)",
"params": {
"origin": "2008-01-01T01:00:00Z",
"scale": "1h",
"offset" : "0",
"decay" : 0.5
}
}

"script" : {
"source" : "params.weight * _score",
"params": {
"weight": 2
}
}

"script" : {
"source" : "Math.log10(doc['field'].value * params.factor)",
params" : {
"factor" : 5
}
}

"script" : {
"source" : "Math.log10((doc['field'].size() == 0 ? 1 : doc['field'].value()) * params.factor)",
params" : {
"factor" : 5
}
}

wrapper query

base64({“term” : { “user” : “Kimchy” }}) = “eyJ0ZXJtIiA6IHsgInVzZXIiIDogIktpbWNoeSIgfX0=”

GET /_search
{
"query" : {
"wrapper": {
"query" : "eyJ0ZXJtIiA6IHsgInVzZXIiIDogIktpbWNoeSIgfX0="
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: