Created
October 12, 2016 22:28
-
-
Save chrisciampoli/7913678b84f229934ce2289efb26454e to your computer and use it in GitHub Desktop.
Querying elasticsearch with Elastica
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" | |
}' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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) { | |
// ... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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) { | |
// ... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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) { | |
// ... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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"} | |
} | |
} | |
} | |
} | |
} | |
}' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
curl 'http://localhost:9200/blog/posts/_search?q=phpunit' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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