如何在ElasticSearch中搜索某些索引

时间:2015-05-21 09:16:35

标签: php elasticsearch elastica

我使用库弹性PHP。 在使用搜索之前,我需要加载索引:

// Load index
$elasticaIndex = $elasticaClient->getIndex('twit');

我可以查询后:

$elasticaResultSet = $elasticaIndex->search($elasticaQuery);

如何构建搜索某些索引,而不仅仅是twit

我使用下一个:

public function Query($value, $elasticaIndex)
    {
        $elasticaQueryString = new \Elastica\Query\QueryString();
        $elasticaQueryString->setQuery((string)$value);

        // Create the actual search object with some data.
        $elasticaQuery = new \Elastica\Query();
        $elasticaQuery->setQuery($elasticaQueryString);

        //Search on the index.
        $elasticaResultSet = $elasticaIndex->search($elasticaQuery);
        return $elasticaResultSet->getResults();
    }

添加文档

private function addDocument($data, $index)
    {
        $this->elasticaIndex = $this->elastic->getIndex($name);
        $this->elasticaIndex->create(array(), true);

        foreach ($data as $id => $val) {
            $documents[] = new \Elastica\Document(
                $id, $val
            );
        }

        $this->elasticaType->addDocuments($documents);
        $this->elasticaType->getIndex()->refresh();
    }

这是正确的代码吗?

创建索引和映射:

为了创建一些索引,我使用函数wuth param $name,这意味着索引的名称:

private function createIndex($name)
    {
        $this->elasticaIndex = $this->elastic->getIndex($name);
        $this->elasticaIndex->create(array(
            'number_of_shards' => 4,
            'number_of_replicas' => 1,
            'analysis' => array(
                'analyzer' => array(
                    $name.'Analyzer' => array(
                        'type' => 'custom',
                        'tokenizer' => 'standard',
                        'filter' => array('lowercase', 'mySnowball')
                    ),
                    $name.'Analyzer' => array(
                        'type' => 'custom',
                        'tokenizer' => 'standard',
                        'filter' => array('standard', 'lowercase', 'mySnowball')
                    )
                )
                /*'filter' => array(
                    'mySnowball' => array(
                        'type' => 'snowball',
                        'language' => 'German'
                    )
                )*/
            )
        ), true);

        $this->elasticaType = $this->elasticaIndex->getType($name);
    }

请注意'analyzer' => array()方法中对create()的关注。它需要映射。

我也有构造函数,它创建了映射。对于此构造函数,我从上面创建的索引传递了$elasticaType的名称index_analyzersearch_analyzer

 function __construct($elasticaType)
 {
    parent::__construct();
    $elasticaIndex = $this->elastic->getIndex($elasticaType);
    $elasticaType = $elasticaIndex->getType($elasticaType);

    $this->mapping = new \Elastica\Type\Mapping();
    $this->mapping->setType($elasticaType);
    $this->mapping->setParam('index_analyzer', $elasticaType.'Analyzer');
    $this->mapping->setParam('search_analyzer', $elasticaType.'Analyzer');
}

当我尝试创建映射时,我收到错误:

Fatal error: Uncaught exception 'Elastica\Exception\ResponseException' with message 'MapperParsingException[Analyzer [Analyzer] not found for index_analyzer setting on root type [users]]' in 

1 个答案:

答案 0 :(得分:1)

您可以使用\Elastica\Search类搜索多个索引。以下是您的代码更新版本:

public function Query($value, $elasticaClient)
{
    $elasticaQueryString = new \Elastica\Query\QueryString();
    $elasticaQueryString->setQuery((string)$value);

    // Create the actual search object with some data.
    $elasticaQuery = new \Elastica\Query();
    $elasticaQuery->setQuery($elasticaQueryString);

    //Search
    $search = new \Elastica\Search($elasticaClient);
    $search->addIndex('twit');
    $search->addIndex('an_other_index');
    $elasticaResultSet = $search->search($elasticaQuery);

    return $elasticaResultSet->getResults();
}

由于您要搜索多个索引,因此您不需要传递$elasticaIndex变量,而是传递$elasticaClient

如果您想保持$elasticaIndex方式(由于某些技术原因),您可以通过这种方式实现您想要的目标:

public function Query($value, $elasticaIndex)
{
    $elasticaQueryString = new \Elastica\Query\QueryString();
    $elasticaQueryString->setQuery((string)$value);

    // Create the actual search object with some data.
    $elasticaQuery = new \Elastica\Query();
    $elasticaQuery->setQuery($elasticaQueryString);

    //Search
    $search = $elasticaIndex->createSearch();
    $search->addIndex('an_other_index');
    $elasticaResultSet = $search->search($elasticaQuery);

    return $elasticaResultSet->getResults();
}

对于您使用分析器的上一个问题,可以在执行以下操作时覆盖var $elasticaType

$elasticaType = $elasticaIndex->getType($elasticaType);

你应该做这样的事情:

 function __construct($name)
 {
    parent::__construct();
    $elasticaIndex = $this->elastic->getIndex($name);
    $elasticaType = $elasticaIndex->getType($name);

    $this->mapping = new \Elastica\Type\Mapping();
    $this->mapping->setType($elasticaType);
    $this->mapping->setParam('index_analyzer', $name.'Analyzer');
    $this->mapping->setParam('search_analyzer', $name.'Analyzer');
}