Created
March 31, 2021 09:06
-
-
Save spinscale/28d5d5eeff268d843da15ccc0cb0fb6e to your computer and use it in GitHub Desktop.
Daily Elastic Byte - Interesting field types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################# | |
## unsigned long | |
############################# | |
DELETE unsigned_long_test | |
PUT unsigned_long_test | |
{ | |
"mappings": { | |
"properties": { | |
"my_counter": { | |
"type": "unsigned_long" | |
} | |
} | |
} | |
} | |
# Long.MAX_VALUE = 9223372036854775807 (signed) | |
PUT unsigned_long_test/_doc/1 | |
{"my_counter": 9223372036854775808} | |
GET unsigned_long_test/_search | |
GET unsigned_long_test/_search | |
{ | |
"query": { | |
"range": { | |
"my_counter": { | |
"gt": 9223372036854775807 | |
} | |
} | |
} | |
} | |
# ensure your JSON parsing library can deal | |
# with scientific notation | |
GET unsigned_long_test/_search | |
{ | |
"size": 0, | |
"aggs": { | |
"by_histogram": { | |
"histogram": { | |
"field": "my_counter", | |
"interval": 50 | |
} | |
} | |
} | |
} | |
############################# | |
## version | |
## | |
## See https://semver.org | |
############################# | |
DELETE version_index | |
PUT version_index | |
{ | |
"mappings": { | |
"properties": { | |
"my_version": { | |
"type": "version" | |
} | |
} | |
} | |
} | |
PUT version_index/_bulk?refresh | |
{ "index" : {} } | |
{"my_version": "1.5.0-alpha"} | |
{ "index" : {} } | |
{"my_version": "1.2.5"} | |
{ "index" : {} } | |
{"my_version": "1.2.5-alpha2"} | |
{ "index" : {} } | |
{"my_version": "1.2.5-alpha2.beta"} | |
{ "index" : {} } | |
{"my_version": "1.2.5-alpha1"} | |
{ "index" : {} } | |
{"my_version": "1.2.5-beta1"} | |
GET version_index/_search?filter_path=**.my_version | |
{ | |
"sort": [ | |
{ | |
"my_version": { | |
"order": "desc" | |
} | |
} | |
] | |
} | |
############################# | |
## flattened | |
## | |
## https://www.elastic.co/guide/en/elasticsearch/reference/7.11/flattened.html | |
############################# | |
DELETE flattened | |
PUT flattened | |
{ | |
"mappings": { | |
"properties": { | |
"labels": { | |
"type": "flattened" | |
} | |
} | |
} | |
} | |
POST flattened/_doc/1 | |
{ | |
"labels": { | |
"size": "xl", | |
"color" : "red", | |
"type" : "v-neck", | |
"price": 25, | |
"last_updated" : 1614594536173 | |
} | |
} | |
GET flattened/_mapping | |
POST flattened/_doc/2?refresh | |
{ | |
"labels": { | |
"last_updated" : 2 | |
} | |
} | |
POST flattened/_search | |
{ | |
"query": { | |
"term": {"labels": "red"} | |
} | |
} | |
POST flattened/_search | |
{ | |
"query": { | |
"term": {"labels.color": "red"} | |
} | |
} | |
# everything is stored as a keyword | |
# so the timestamp of 2 is greater than the other tone | |
POST flattened/_search | |
{ | |
"sort": [ | |
{ | |
"labels.last_updated": { | |
"order": "desc" | |
} | |
} | |
] | |
} | |
############################# | |
## aggregate_metric | |
## | |
## https://www.elastic.co/guide/en/elasticsearch/reference/7.11/aggregate-metric-double.html | |
############################# | |
DELETE sales_per_day | |
PUT sales_per_day | |
{ | |
"mappings": { | |
"properties": { | |
"agg_metric": { | |
"type": "aggregate_metric_double", | |
"metrics": [ "min", "max", "value_count", "sum" ], | |
"default_metric": "value_count" | |
} | |
} | |
} | |
} | |
PUT sales_per_day/_doc/january | |
{ | |
"agg_metric": { | |
"min": 13, | |
"max": 34, | |
"sum" : 4653.00, | |
"value_count": 234 | |
} | |
} | |
PUT sales_per_day/_doc/february?refresh | |
{ | |
"agg_metric": { | |
"min": 5, | |
"max": 28, | |
"sum" : 3124.34, | |
"value_count": 260 | |
} | |
} | |
POST sales_per_day/_search?size=0 | |
{ | |
"aggs": { | |
"metric_min": { "min": { "field": "agg_metric" } }, | |
"metric_max": { "max": { "field": "agg_metric" } }, | |
"metric_value_count": { "value_count": { "field": "agg_metric" } }, | |
"metric_avg": { "avg": { "field": "agg_metric" } } | |
} | |
} | |
############################# | |
## dense_vector | |
## | |
## https://www.elastic.co/guide/en/elasticsearch/reference/7.11/dense-vector.html | |
############################# | |
DELETE books | |
# add vector field | |
PUT books | |
{ | |
"mappings": { | |
"properties": { | |
"vector_recommendation": { | |
"type": "dense_vector", | |
"dims": 3 | |
} | |
} | |
} | |
} | |
# Add a vector for each document | |
# Features are the following | |
# First dimension is the number of pages divided by 100 (0-10) | |
# Second dimension is the rating (0-5) | |
# Third dimension is the price (0-10) 0 means cost > 100 , 10 cost < 10 | |
PUT books/_bulk | |
{ "index" : { "_id" : "database-internals" } } | |
{ "vector_recommendation" : [3.5, 4.5, 5.2] } | |
{ "index" : { "_id" : "designing-data-intensive-applications" } } | |
{ "vector_recommendation" : [5.9, 4.4, 6.8] } | |
{ "index" : { "_id" : "kafka-the-definitive-guide" } } | |
{ "vector_recommendation" : [2.97, 3.9, 6.2] } | |
{ "index" : { "_id" : "effective-java" } } | |
{ "vector_recommendation" : [4.12, 4.2, 7.2] } | |
{ "index" : { "_id" : "daemon" } } | |
{ "vector_recommendation" : [4.48, 4.0, 8.7] } | |
{ "index" : { "_id" : "cryptonomicon" } } | |
{ "vector_recommendation" : [10.0, 4.0, 9.3] } | |
{ "index" : { "_id" : "garbage-collection-handbook" } } | |
{ "vector_recommendation" : [5.1, 5.0, 1.3] } | |
{ "index" : { "_id" : "radical-candor" } } | |
{ "vector_recommendation" : [4.0, 4.0, 9.2] } | |
{ "index" : { "_id" : "never-split-the-difference" } } | |
{ "vector_recommendation" : [4.0, 4.3, 8.9] } | |
{ "index" : { "_id" : "not-giving-a-fsck" } } | |
{ "vector_recommendation" : [2.8, 4.4, 8.9] } | |
{ "index" : { "_id" : "permanent-record" } } | |
{ "vector_recommendation" : [3.3, 4.7, 8.7] } | |
GET books/_search | |
{ | |
"query": { | |
"exists": { "field": "vector_recommendation"} | |
} | |
} | |
# short, good rating, cheap | |
GET books/_search | |
{ | |
"query": { | |
"script_score": { | |
"query" : { | |
"match_all" : {} | |
}, | |
"script": { | |
"source": "cosineSimilarity(params.query_vector, 'vector_recommendation') + 1.0", | |
"params": { | |
"query_vector": [1.0, 5.0, 10.0] | |
} | |
} | |
} | |
} | |
} | |
# long, good rating, any price | |
GET books/_search | |
{ | |
"query": { | |
"script_score": { | |
"query" : { | |
"match_all" : {} | |
}, | |
"script": { | |
"source": "cosineSimilarity(params.query_vector, 'vector_recommendation') + 1.0", | |
"params": { | |
"query_vector": [10.0, 5.0, 5.0] | |
} | |
} | |
} | |
} | |
} | |
############################# | |
## Do you want to know more? | |
## | |
## https://www.elastic.co/blog/aggregate-all-the-things-new-aggregations-in-elasticsearch-7 | |
############################# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment