Skip to content

Instantly share code, notes, and snippets.

@matepaavo
Created January 9, 2019 09:52
Show Gist options
  • Save matepaavo/216298ac85a749322a0181d0bf154377 to your computer and use it in GitHub Desktop.
Save matepaavo/216298ac85a749322a0181d0bf154377 to your computer and use it in GitHub Desktop.
#Get indeces
GET _cat/indices
#Create ads index with mappings
PUT ads
{
"settings": {
"index": {
"number_of_shards": 1,
"number_of_replicas": 0
}
},
"mappings": {
"_doc": {
"properties": {
"id": {
"type": "integer"
},
"status_id": {
"type": "integer"
},
"listing_type_id": {
"type": "integer"
},
"property_type_id": {
"type": "integer"
},
"price_huf": {
"type": "long"
},
"area_size": {
"type": "float"
},
"room_count": {
"type": "float"
},
"city": {
"type": "keyword"
},
"district": {
"type": "keyword"
},
"country": {
"type": "keyword"
},
"county": {
"type": "keyword"
},
"photo_count": {
"type": "integer"
},
"listing_date": {
"type": "date"
},
"zipcode": {
"type": "keyword"
},
"year_built": {
"type": "integer"
},
"query": {
"type": "percolator"
}
}
}
}
}
#Add a query
PUT ads/_doc/1
{
"query": {
"query_string": {
"default_operator": "and",
"default_field": "id",
"query": """(listing_type_id:"1" AND property_type_id:"1" AND (city:"Budapest"))"""
}
},
"property_type_id": 1,
"listing_type_id": 1,
"meta": {
"search_id": "cf370764-8197-4044-be0b-33bdeed31dc0"
}
}
#Add a few more
PUT ads/_doc/2
{
"query": {
"query_string": {
"default_operator": "and",
"default_field": "id",
"query": """(listing_type_id:"1" AND property_type_id:"1" AND (city:"Budapest") AND price_huf:[29000000 TO 34000000])"""
}
},
"property_type_id": 1,
"listing_type_id": 1,
"meta": {
"search_id": "37a0c805-811e-441f-8d4c-4dfdb7a7b9e8"
}
}
PUT ads/_doc/3
{
"query": {
"query_string": {
"default_operator": "and",
"default_field": "id",
"query": """(listing_type_id:"1" AND property_type_id:"1" AND city:"Debrecen" AND area_size:[40 TO 60] AND price_huf:[20000000 TO 25000000])"""
}
},
"property_type_id": 1,
"listing_type_id": 1,
"meta": {
"search_id": "463b57f9-a8a0-449e-be09-3078d6e87ee4"
}
}
PUT /ads/_doc/4
{
"query": {
"query_string": {
"default_operator": "and",
"default_field": "id",
"query": """(listing_type_id:"1" AND property_type_id:"2" AND city:"Debrecen" AND area_size:[100 TO 150] AND price_huf:[40000000 TO 60000000])"""
}
},
"property_type_id": 2,
"listing_type_id": 1,
"meta": {
"search_id": "9b8fdc39-39a9-4a30-927f-ef6c25197c8a"
}
}
POST ads/_doc/5
{
"query": {
"bool": {
"must": [
{
"term": {
"listing_type_id": 1
}
},
{
"term": {
"property_type_id": 1
}
},
{
"term": {
"city": "Budapest"
}
}
]
}
}
}
#Peak into index
POST ads/_search
{
"size": 20
}
#Percolate basic
POST ads/_search/
{
"query": {
"percolate": {
"field": "query",
"document": {
"id": 1234567,
"price_huf": 33000000,
"listing_date": "2018-11-11T11:29:28Z",
"listing_type_id": 1,
"property_type_id": 1,
"room_count": 2,
"zipcode": "1134",
"city": "Budapest",
"county": "Budapest",
"district": "11",
"country": "Magyarország",
"area_size": 50,
"year_built": 2010,
"photo_count": 6
}
}
}
}
#Percolate with filter
POST ads/_search
{
"query": {
"constant_score": {
"filter": {
"bool": {
"must": [
{
"term": {
"listing_type_id": 1
}
},
{
"term": {
"property_type_id": 1
}
},
{
"percolate": {
"field": "query",
"document": {
"id": 1234567,
"price_huf": 35000000,
"listing_date": "2018-11-11T11:29:28Z",
"listing_type_id": 1,
"property_type_id": 1,
"room_count": 2,
"zipcode": "1134",
"city": "Budapest",
"county": "Budapest",
"district": "11",
"country": "Magyarország",
"area_size": 50,
"year_built": 2010,
"photo_count": 6
}
}
}
]
}
}
}
}
}
POST ads/_search
{
"query": {
"constant_score": {
"filter": {
"bool": {
"must": [
{
"term": {
"listing_type_id": 1
}
},
{
"term": {
"property_type_id": 2
}
},
{
"percolate": {
"field": "query",
"document": {
"id": 1234567,
"price_huf": 50000000,
"listing_date": "2018-11-11T11:29:28Z",
"listing_type_id": 1,
"property_type_id": 2,
"room_count": 4,
"zipcode": "4032",
"city": "Debrecen",
"county": "Hajdú-Bihar",
"country": "Magyarország",
"area_size": 120,
"year_built": 2010,
"photo_count": 6
}
}
}
]
}
}
}
}
}
#Truncate index
POST ads/_delete_by_query
{
"query": {
"match_all": {}
}
}
#Delete index
DELETE ads
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment