Skip to content

Instantly share code, notes, and snippets.

@christeredvartsen
Created June 26, 2012 12:17
Show Gist options
  • Save christeredvartsen/2995512 to your computer and use it in GitHub Desktop.
Save christeredvartsen/2995512 to your computer and use it in GitHub Desktop.
Querying elasticsearch with Elastica
#!/bin/bash
curl -XPOST 'http://localhost:9200/blog/posts/_search' -d '{
"query": {
"filtered": {
"query": {
"query_string": {
"query":"php zend framework",
"default_operator": "OR",
"fields": ["title", "content"]
}
},
"filter": {
"range": {
"published": {
"from": "2012-01-01 00:00:00",
"to": "2013-01-01 00:00:00"
}
}
}
}
},
"facets": {
"categories": {
"terms": {
"field": "categories.na"
}
},
"months": {
"date_histogram": {
"field": "published",
"interval": "month"
}
}
},
"sort":{
"published": {
"order": "desc"
},
"title.na": "asc"
},
"from": "0",
"size": "25"
}'
<?php
$query = new Elastica_Query_Builder();
$query
->query()
->filteredQuery()
->query()
->queryString()
->field('query', 'php zend framework')
->defaultOperator('OR')
->fields(array('title', 'content'))
->queryStringClose()
->queryClose()
->filter()
->range()
->fieldOpen('published')
->field('from', '2012-01-01 00:00:00')
->field('to', '2013-01-01 00:00:00')
->fieldClose()
->rangeClose()
->filterClose()
->filteredQueryClose()
->queryClose()
->facets()
->fieldOpen('categories')
->fieldOpen('terms')
->field('field', 'categories.na')
->fieldClose()
->fieldClose()
->fieldOpen('months')
->fieldOpen('date_histogram')
->field('field', 'published')
->field('interval', 'month')
->fieldClose()
->fieldClose()
->facetsClose()
->sort()
->fieldOpen('published')
->field('order', 'desc')
->fieldClose()
->field('title.na', 'asc')
->sortClose()
->from(0)
->size(25);
// Create a raw query since the query above can't be passed directly to the search method used below
$query = new Elastica_Query($query->toArray());
// Create the search object and inject the client
$search = new Elastica_Search(new Elastica_Client());
// Configure and execute the search
$resultSet = $search->addIndex('blog')
->addType('posts')
->search($query);
// Loop through the results
foreach ($resultSet as $hit) {
// ...
}
<?php
$query = new Elastica_Query_Builder('{
"query": {
"filtered": {
"query": {
"query_string": {
"query":"php zend framework",
"default_operator": "OR",
"fields": ["title", "content"]
}
},
"filter": {
"range": {
"published": {
"from": "2012-01-01 00:00:00",
"to": "2013-01-01 00:00:00"
}
}
}
}
},
"facets": {
"categories": {
"terms": {
"field": "categories.na"
}
},
"months": {
"date_histogram": {
"field": "published",
"interval": "month"
}
}
},
"sort":{
"published": {
"order": "desc"
},
"title.na": "asc"
},
"from": "0",
"size": "25"
}');
// Create a raw query since the query above can't be passed directly to the search method used below
$query = new Elastica_Query($query->toArray());
// Create the search object and inject the client
$search = new Elastica_Search(new Elastica_Client());
// Configure and execute the search
$resultSet = $search->addIndex('blog')
->addType('posts')
->search($query);
// Loop through the results
foreach ($resultSet as $hit) {
// ...
}
<?php
// Query string
$queryString = new Elastica_Query_QueryString('php zend framework');
$queryString->setDefaultOperator('OR')
->setFields(array('title', 'content'));
// Filtered query using the query string and a filter
$filteredQuery = new Elastica_Query_Filtered(
$queryString,
new Elastica_Filter_Range('published', array(
'from' => '2012-01-01 00:00:00',
'to' => '2013-01-01 00:00:00',
))
);
// Facets
$categoryFacet = new Elastica_Facet_Terms('categories');
$categoryFacet->setField('categories.na');
$monthsFacet = new Elastica_Facet_DateHistogram('months');
$monthsFacet->setField('published')
->setInterval('month');
// Create the main query object
$query = new Elastica_Query($filteredQuery);
$query->setFacets(array($categoryFacet, $monthsFacet))
->setSort(array(
'published' => array('order' => 'desc'),
'title.na' => 'asc'
))
->setFrom(0)
->setLimit(25);
// Create the search object and inject the client
$search = new Elastica_Search(new Elastica_Client());
// Configure and execute the search
$resultSet = $search->addIndex('blog')
->addType('posts')
->search($query);
// Loop through the results
foreach ($resultSet as $hit) {
// ...
}
#!/bin/bash
curl -XPOST 'http://localhost:9200/blog' -d '{
"mappings": {
"posts": {
"properties": {
"id": {"type": "integer"},
"title": {
"type": "multi_field",
"fields": {
"title": {"type": "string"},
"na": {"type": "string", "index": "not_analyzed"}
}
},
"content": {"type": "string"},
"published": {"type": "date", "format": "YYYY-MM-dd HH:mm:ss"},
"user": {
"type": "multi_field",
"fields": {
"user": {"type": "string"},
"na": {"type": "string", "index": "not_analyzed"}
}
},
"categories": {
"type": "multi_field",
"fields": {
"categories": {"type": "string", "index_name": "category"},
"na": {"type": "string", "index": "not_analyzed"}
}
},
"tags": {
"type": "multi_field",
"fields": {
"tags": {"type": "string", "index_name": "tag"},
"na": {"type": "string", "index": "not_analyzed"}
}
}
}
}
}
}'
#!/bin/bash
curl 'http://localhost:9200/blog/posts/_search?q=phpunit'
<?php
// Create the search object and inject the client
$search = new Elastica_Search(new Elastica_Client());
// Configure and execute the search
$resultSet = $search->addIndex('blog')
->addType('posts')
->search('phpunit');
// Loop through the results
foreach ($resultSet as $hit) {
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment