Skip to content

Instantly share code, notes, and snippets.

@imotov
Forked from haswalt/create_nested_index.sh
Created October 31, 2012 12:01
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 imotov/3e9a2daf3e6f2b12412b to your computer and use it in GitHub Desktop.
Save imotov/3e9a2daf3e6f2b12412b to your computer and use it in GitHub Desktop.
Search articles by tag slug
#!/usr/bin/env bash
curl -XPUT http://localhost:9200/cars?pretty=true -d'{
"settings": {
"number_of_shard": 1,
"number_of_replicas": 1
},
"mappings": {
"documents": {
"properties": {
"title": {
"type": "string",
"store": "yes"
},
"tags": {
"type": "nested",
"properties": {
"title": {
"type": "string",
"store": "yes"
},
"slug": {
"type": "string",
"store": "yes"
}
}
}
}
}
}
}'
!/usr/bin/env bash
curl -XPOST http://localhost:9200/cars/documents/1?pretty=true -d'{
"title": "Test Document 1",
"slug": "test-document-1",
"content": "<p>Some content</p>",
"published": "2",
"version": "2",
"tags": [
{
"name": "Cars",
"slug": "cars",
"type": 101
},
{
"name": "News",
"slug": "news",
"type": 102
}
]
}'
curl -XPOST http://localhost:9200/cars/documents/2?pretty=true -d'{
"title": "Test Document 2",
"slug": "test-document-2",
"content": "<p>Some content</p>",
"published": "2",
"version": "2",
"tags": [
{
"name": "Cars",
"slug": "cars",
"type": 101
},
{
"name": "Reviews",
"slug": "reviews",
"type": 102
}
]
}'
curl -XPOST http://localhost:9200/cars/documents/3?pretty=true -d'{
"title": "Test Document 3",
"slug": "test-document-3",
"content": "<p>Some content</p>",
"published": "2",
"version": "2",
"tags": [
{
"name": "Cars",
"slug": "cars",
"type": 101
},
{
"name": "Reviews",
"slug": "reviews",
"type": 102
}
]
}'
curl -XPOST http://localhost:9200/cars/documents/4?pretty=true -d'{
"title": "Test Document 4",
"slug": "test-document-4",
"content": "<p>Some content</p>",
"published": 0,
"version": 2,
"tags": [
{
"name": "Cars",
"slug": "cars",
"type": 101
},
{
"name": "Reviews",
"slug": "reviews",
"type": 102
}
]
}'
curl -XPOST http://localhost:9200/cars/documents/5?pretty=true -d'{
"title": "Test Document 5",
"slug": "test-document-5",
"content": "<p>Some content</p>",
"published": "2",
"version": "2",
"tags": [
{
"name": "Cars",
"slug": "cars",
"type": 101
},
{
"name": "Reviews",
"slug": "reviews",
"type": 102
}
]
}'
#!/usr/bin/env bash
curl -XGET http://localhost:9200/cars/documents/_search?pretty=true -d'{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"bool": {
"must": [
{
"range": { "published": { "gt": 0 } }
},
{
"nested": {
"path": "tags",
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"and": [
{
"term": {
"tags.slug": "reviews"
}
}
]
}
}
}
}
}
]
}
}
}
},
"from": 0,
"size": 10
}'
#!/usr/bin/env bash
curl -XGET http://localhost:9200/cars/documents/_search?pretty=true -d'{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"nested": {
"path": "tags",
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"and": [
{
"term": {
"tags.slug": "reviews"
}
}
]
}
}
}
}
}
}
},
"from": 0,
"size": 1
}'
#!/usr/bin/env bash
curl -XGET http://localhost:9200/cars/documents/_search?pretty=true -d'{
"fields": ["title", "slug", "tags"],
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"and": [{
"nested": {
"path": "tags",
"filter": {
"term": {
"tags.slug": ["cars"]
}
}
}
}, {
"nested": {
"path": "tags",
"filter": {
"term": {
"tags.slug": ["news"]
}
}
}
}]
}
}
}
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment