4

Elastica を使用し、ElasticSearch からデータをクエリするのはこれが初めてです

私にとっては、初心者として、Elastica を使用して以下のコードを照会する方法について質問があります。

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

ここで誰かが私に腕を貸してくれることを願っています。

ありがとう、

4

2 に答える 2

8

これは行う必要があります:

// 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());

クエリを実行するには、ESサーバーに接続する必要があります

// 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),
    ),
));

そして、クエリを実行するインデックスとタイプを指定します

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

これで、クエリを実行できます

$type->search($query);

編集:名前空間環境とElasticaの現在のバージョンを使用している場合は、それに応じて新しいオブジェクトが作成されるすべての行を次のように変更します。

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

等々

于 2012-11-13T14:55:22.497 に答える
1

次のようにクエリを実行することもできます: (優れた記事を提供してくれたhttp://tech.vg.no/2012/07/03/using-elastica-to-query-elasticsearch/に感謝します)

<?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) {
    // ...
}
于 2015-01-13T03:26:35.547 に答える