ElasticSearch 如何在某些索引中进行搜索

How do search in some indexes in 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);
    }

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

我还有创建映射的构造函数。对于这个构造函数,我传递 $elasticaTypeindex_analyzer 的名称,search_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 

您可以使用 \Elastica\Search class 在多个索引中进行搜索。这是您的代码的更新版本:

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');
}