Wondering Coder
Wondering Coder

Reputation: 1702

How to query using Elastica

This is my first time using Elastica and querying data from the ElasticSearch

For me, as starter I have a question on how to query the code below using Elastica?:

curl 'http://localhost:9200/myindex/_search?pretty=true' -d '{     
  "query" : {
    "term": {
      "click": "true"
    }   },   "facets" : {
    "matches" : {
      "terms" : {
          "field" : "pubid",
          "all_terms" : true,
          "size": 200 
      }
    }   
  } 
}'

Hope someone can lend me an arm here.

Thanks,

Upvotes: 4

Views: 9843

Answers (2)

Silas Palmer
Silas Palmer

Reputation: 2985

You can also query like this: (Thanks to http://tech.vg.no/2012/07/03/using-elastica-to-query-elasticsearch/ for the excellent article)

<?php
$query = new Elastica_Query_Builder('{     
    "query" : {
        "term": {
            "click": "true"
            }   
        },
    "facets" : {
        "matches" : {
            "terms" : {
                "field" : "pubid",
                "all_terms" : true,
                "size": 200 
                }
            }   
        } 
    }');

// 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) {
    // ...
}

Upvotes: 1

Thorsten
Thorsten

Reputation: 5644

This should do:

// Create a "global" query
$query = new Elastica_Query;

// Create the term query
$term = new Elastica_Query_Term;
$term->setTerm('click', 'true');

// Add term query to "global" query
$query->setQuery($term);

// Create the facet
$facet = new Elastica_Facet_Terms('matches');
$facet->setField('pubid')
      ->setAllTerms(true)
      ->setSize(200);

// Add facet to "global" query
$query->addFacet($facet);

// Output query
echo json_encode($query->toArray());

To run the query, you need to conntect to your ES servers

// Connect to your ES servers
$client = new Elastica_Client(array(
    'servers' => array(
        array('host' => 'localhost', 'port' => 9200),
        array('host' => 'localhost', 'port' => 9201),
        array('host' => 'localhost', 'port' => 9202),
        array('host' => 'localhost', 'port' => 9203),
        array('host' => 'localhost', 'port' => 9204),
    ),
));

And specify which index and type you want to run your query against

// Get index
$index = $client->getIndex('myindex');
$type = $index->getType('typename');

Now you can run your query

$type->search($query);

Edit: If you are using a namespaced enviroment and a current version of Elastica, change all the lines where new objects are created accordingly to

$query = new \Elastica\Query;
$facet = new \Elastica\Facet\Terms

and so on

Upvotes: 8

Related Questions