Skip to content

Instantly share code, notes, and snippets.

@aaizemberg
Last active May 29, 2019 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaizemberg/6345c36534904c3fa1f4ef906e0f2024 to your computer and use it in GitHub Desktop.
Save aaizemberg/6345c36534904c3fa1f4ef906e0f2024 to your computer and use it in GitHub Desktop.
modifique el script que hizo kimchy para su charla de París en el 2011
# delete all test data
curl -XDELETE localhost:9200/test
# index some data
curl -XPUT localhost:9200/test/_doc/1 -H 'Content-Type: application/json' -d '{
"tags" : ["scala", "functional"],
"count" : 10,
"price" : 12.5,
"date" : "2011-05-25"
}'
curl -XPUT localhost:9200/test/_doc/2 -H 'Content-Type: application/json' -d '{
"tags" : ["clojure", "lisp", "functional"],
"count" : 5,
"price" : 15.7,
"date" : "2011-05-26"
}'
curl -XPUT localhost:9200/test/_doc/3 -H 'Content-Type: application/json' -d '{
"tags" : ["java", "scala"],
"count" : 5,
"price" : 10.7,
"date" : "2011-05-27"
}'
# Using Query String
curl 'localhost:9200/test/_search?q=tags:scala&pretty=true'
# Same as:
curl 'localhost:9200/test/_search?pretty=true' -H 'Content-Type: application/json' -d '{
"query" : {
"query_string" : {
"query" : "tags:scala"
}
}
}'
# Basic (non analyzed queries)
# -> term (non analyzed single term)
curl 'localhost:9200/test/_search?pretty=true' -H 'Content-Type: application/json' -d '{
"query" : {
"term" : {
"tags" : "scala"
}
}
}'
# -> term, this won't match, since its not analyzed
curl 'localhost:9200/test/_search?pretty=true' -H 'Content-Type: application/json' -d '{
"query" : {
"term" : {
"tags" : "Scala"
}
}
}'
# -> prefix (non analyzed single term)
curl 'localhost:9200/test/_search?pretty=true' -H 'Content-Type: application/json' -d '{
"query" : {
"prefix" : {
"tags" : "sca"
}
}
}'
# -> wildcard (non analyzed single term)
curl 'localhost:9200/test/_search?pretty=true' -H 'Content-Type: application/json' -d '{
"query" : {
"wildcard" : {
"tags" : "sca*a"
}
}
}'
# -> fuzzy (non analyzed single term)
curl 'localhost:9200/test/_search?pretty=true' -H 'Content-Type: application/json' -d '{
"query" : {
"fuzzy" : {
"tags" : "scalra"
}
}
}'
# Text Queries (Analyzed)
# -> text (analyzed and expanded to bool query)
curl 'localhost:9200/test/_search?pretty=true' -H 'Content-Type: application/json' -d '{
"query" : {
"text" : {
"tags" : "clojure java"
}
}
}'
# also includes text_phrase and text_phrase_prefix
# Range
# -> range on numeric values
curl 'localhost:9200/test/_search?pretty=true' -H 'Content-Type: application/json' -d '{
"query" : {
"range" : {
"price" : { "gt" : 15 }
}
}
}'
# -> and on dates
curl 'localhost:9200/test/_search?pretty=true' -H 'Content-Type: application/json' -d '{
"query" : {
"range" : {
"date" : { "gte" : "2011-05-26" }
}
}
}'
# Bool Query
# -> sample with must, also has must_not and should clauses
curl 'localhost:9200/test/_search?pretty=true' -H 'Content-Type: application/json' -d '{
"query" : {
"bool" : {
"must" : [
{ "text" : {
"tags" : "scala"
}
}, {
"range" : {
"price" : { "gt" : 15 }
}
}
]
}
}
}'
# describe 'news'
GET news
# muestro la cantidad de registros
# 2.365.984 registros (casi 2.5 millones de registros)
GET news/_count
GET news/_search
{
"query": {
"fuzzy": {
"title": "kichilof"
}
}
}
# LIMIT 3
GET news/_search?size=3
{
"query": {
"fuzzy": {
"title": "kichilof"
}
}
}
# ahora es ordenable, por el campo 'date'
PUT news/_mapping
{
"properties": {
"date": {
"type": "text",
"fielddata": true
}
}
}
GET /news/_search?size=3
{
"query": {
"fuzzy": {
"title": "cristina"
}
},
"sort": {
"date": {
"order": "desc"
}
}
}
GET /news/_search?size=3
{
"query": {
"term": {
"title": "duran"
}
},
"sort": {
"date": {
"order": "desc"
}
}
}
GET news/_search?size=1
GET news/_doc/1
# documentos con fechas invalidas
DELETE /news/_doc/0
DELETE /news/_doc/2365983
PUT news
{
"mappings": {
"properties": {
"date": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss Z"
}
}
}
}
# error --> resource_already_exists_exception"
# tendria que reindexar con este nuevo mapping
GET news/_search
{
"query": {
"range": {
"date": {
"gte": "2018-07-16",
"lte": "2018-07-17"
}
}
}
}
GET twitter
# post_date" : "2009-11-15T13:12:00",
DELETE test
GET test/_search
PUT /test/_doc/1
{
"text": "este es el 1er documento",
"count": 12,
"price": 12.12,
"date": "2019-05-27T17:10:12"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment