【问题标题】:How to query using Elastica如何使用 Elastica 进行查询
【发布时间】:2012-11-08 08:33:02
【问题描述】:

这是我第一次使用 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 
      }
    }   
  } 
}'

希望有人可以在这里帮我一把。

谢谢,

【问题讨论】:

    标签: php elasticsearch elastica


    【解决方案1】:

    应该这样做:

    // 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
    

    等等

    【讨论】:

      【解决方案2】:

      你也可以这样查询: (感谢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) {
          // ...
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-10-25
        • 1970-01-01
        • 2014-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多