Skip to content

Instantly share code, notes, and snippets.

@yvoronoy
Last active December 11, 2019 22:53
Show Gist options
  • Save yvoronoy/936b30519ecaa76dbcc1800d9ee5b0a1 to your computer and use it in GitHub Desktop.
Save yvoronoy/936b30519ecaa76dbcc1800d9ee5b0a1 to your computer and use it in GitHub Desktop.
Magento2 ElasticSearch Helper
Run this command first to get proper connection
SUP_ES_HOST=localhost:9200
SUP_ES_INDEX_NAME=magento2
vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php
$curlCmd = sprintf('curl -X%s -H \'%s\' %s%s -d \'%s\'',
$method,
'Content-type: application/json',
$this->host,
$this->getURI ($uri, $params),
$body
);
#debug
$data = array(
$curlCmd
);
ob_start();
var_dump($data);
$data = ob_get_clean();
$backtrace = ''; //\Magento\Framework\Debug::backtrace(true, false, true);
$message = sprintf("Data: %s %s", $data, $backtrace);
file_put_contents('/tmp/magento.support.debug.data.log', $message, FILE_APPEND);
#Debug end
#stats
curl -s $SUP_ES_HOST/_cat/indices | grep $SUP_ES_INDEX_NAME
#Get document by ID
curl $SUP_ES_HOST/$SUP_ES_INDEX_NAME/document/1
# Get document by search
curl $SUP_ES_HOST/$SUP_ES_INDEX_NAME/_search -d '
{
"query": {
"match": {
"name": "name"
}
}
}'
#log queries
curl -XPUT "$SUP_ES_HOST/$SUP_ES_INDEX_NAME/_settings" -d'
{
"index.search.slowlog.threshold.query.debug": "0s"
}'
curl $SUP_ES_HOST/$SUP_ES_INDEX_NAME/_search -d '
{
"from":"0",
"size":"10000",
"fields":[
"_id",
"_score"
],
"query":{
"bool":{
"must":[
{
"terms":{
"visibility":[
"3",
"4"
]
}
}
],
"should":[
{
"match":{
"sku":{
"query":"Chili",
"boost":2
}
}
},
{
"match":{
"_all":{
"query":"Chili",
"boost":2
}
}
},
{
"match":{
"name":{
"query":"Chili",
"boost":11
}
}
}
],
"minimum_should_match":1
}
}
}
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment