Skip to content

Instantly share code, notes, and snippets.

@nagitsu
Last active May 11, 2018 22:54
Show Gist options
  • Save nagitsu/b8e22c58f3d5965878f1 to your computer and use it in GitHub Desktop.
Save nagitsu/b8e22c58f3d5965878f1 to your computer and use it in GitHub Desktop.
Elasticsearch Intro Queries
# Indexando un documento.
POST /newyork/article
{
"title": "'Doctor Who' Versus 'Sherlock'? Keep Dreaming, Comic-Con",
"keywords": ["doctor-who", "sherlock-holmes", "comic-con"],
"pub_date": "2015-07-09T21:56:18Z",
"print_page": 7,
"section_name": {
"display_name": "U.S.",
"content": "us",
"url": "http://www.nytimes.com/pages/national/index.html"
},
"web_url": "http://www.nytimes.com/aponline/2015/07/09/us/ap-us-comic-con-doctor-who-sherlock.html",
"content": "..."
}
# Indexando un documento, cambiando el tipo de un campo.
POST /newyork/article/
{
"title": "'Doctor Who' Versus 'Sherlock'? Keep Dreaming, Comic-Con",
"pub_date": "nueve de julio de dos mil quince",
"print_page": 7,
"content": "..."
}
# Obtenemos el mapping de un tipo.
GET /newyork/article/_mapping
# Creamos un nuevo tipo con un mapping específico.
PUT /newyork/article2/_mapping
# Salida de lo de arriba, bajo el campo "article".
{
}
# Indexamos el documento con el nuevo tipo, ahora funciona.
POST /newyork/article2/
{
"title": "'Doctor Who' Versus 'Sherlock'? Keep Dreaming, Comic-Con",
"pub_date": "nueve de julio de dos mil quince",
"print_page": 7,
"content": "..."
}
# Hacemos una query por todos los documentos.
GET /newyork/article/_search
{
"query": {
"match_all": {}
}
}
# Sólo documentos con la palabra "sherlock".
GET /nyt/article/_search
{
"query": {
"match": {
"headline.main": "sherlock"
}
}
}
# Misma query, pero matcheando dos palabras simultáneamente.
GET /nyt/article/_search
{
"query": {
"match_phrase": {
"headline.main": "doctor who"
}
}
}
# Dos queries, juntadas por una bool query.
GET /nyt/article/_search
{
"query": {
"bool": {
"should": [
{"match_phrase": {"headline.main": "doctor who"}},
{"match": {"headline.main": "sherlock"}}
]
}
}
}
# Phrase match tiene que mantener posiciones relativas.
# minimum_number_should_match especifica cuántas cláusulas should deberían matchear.
GET /nyt/article/_search
{
"query": {
"bool": {
"should": [
{"match_phrase": {"headline.main": "who doctor"}},
{"match": {"headline.main": "sherlock"}}
],
"minimum_number_should_match": 2
}
}
}
# Para usar un filtro, usamos una filtered query.
# Filtramos acorde al valor del campo document_type.
GET /nyt/article/_search
{
"query": {
"filtered": {
"query": {
"bool": {
"should": [
{"match_phrase": {"headline.main": "doctor who"}},
{"match": {"headline.main": "sherlock"}}
],
"minimum_number_should_match": 1
}
},
"filter": {
"term": {"document_type": "article"}}
}
}
}
}
# Se pueden especificar múltiples filters, agrupándolos con un and.
GET /nyt/article/_search
{
"query": {
"filtered": {
"query": {
"bool": {
"should": [
{"match_phrase": {"headline.main": "doctor who"}},
{"match": {"headline.main": "sherlock"}}
],
"minimum_number_should_match": 1
}
},
"filter": {
"and": [
{"range": {"pub_date": {"gte": "now-2M"}}},
{"term": {"document_type": "article"}}
]
}
}
}
}
# Ejemplo de agregación.
GET /nyt/article/_search?search_type=count
{
"query": {
"match": {
"headline.main": "Uruguay"
}
},
"aggs": {
"over_time": {
"date_histogram": {
"field": "pub_date",
"format": "yyyy",
"interval": "1y"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment