Creates a new index with the given arguments.
public create ( array $args = [], boolean | array $options = null ) : |
||
$args | array | OPTIONAL Arguments to use |
$options | boolean | array | OPTIONAL bool=> Deletes index first if already exists (default = false). array => Associative array of options (option=>value) |
Résultat | Server response |
/** * Create the test index before tests run. */ protected static function createIndex() { self::$index->create(['analysis' => ['analyzer' => MappingFactory::getCustomAnalyzers()]], true); $type = self::$index->getType('message'); $mapping = (new MappingFactory())->create(EmailMessage::schema(), 'english'); $mapping->setType($type); $mapping->send(); }
/** * @return Index */ private function getIndex() { if (null === $this->index) { $this->index = $this->client->getIndex($this->indexName); if (!$this->index->exists()) { $this->index->create(); } } return $this->index; }
/** * @group functional */ public function testSearch() { $client = $this->_getClient(); $index = new Index($client, 'test'); $index->create(array(), true); $index->getSettings()->setNumberOfReplicas(0); //$index->getSettings()->setNumberOfShards(1); $type = new Type($index, 'helloworldmlt'); $mapping = new Mapping($type, array('email' => array('store' => 'yes', 'type' => 'string', 'index' => 'analyzed'), 'content' => array('store' => 'yes', 'type' => 'string', 'index' => 'analyzed'))); $mapping->setSource(array('enabled' => false)); $type->setMapping($mapping); $doc = new Document(1000, array('email' => '*****@*****.**', 'content' => 'This is a sample post. Hello World Fuzzy Like This!')); $type->addDocument($doc); $doc = new Document(1001, array('email' => '*****@*****.**', 'content' => 'This is a fake nospam email address for gmail')); $type->addDocument($doc); // Refresh index $index->refresh(); $mltQuery = new MoreLikeThis(); $mltQuery->setLike('fake gmail sample'); $mltQuery->setFields(array('email', 'content')); $mltQuery->setMaxQueryTerms(3); $mltQuery->setMinDocFrequency(1); $mltQuery->setMinTermFrequency(1); $query = new Query(); $query->setQuery($mltQuery); $resultSet = $type->search($query); $resultSet->getResponse()->getData(); $this->assertEquals(2, $resultSet->count()); }
/** * @group functional */ public function testSearch() { $client = $this->_getClient(); $index = new Index($client, 'test'); $index->create(array(), true); $type = new Type($index, 'helloworld'); $doc = new Document(1, array('id' => 1, 'email' => '*****@*****.**', 'username' => 'hans', 'test' => array('2', '3', '5'))); $type->addDocument($doc); $doc = new Document(2, array('id' => 2, 'email' => '*****@*****.**', 'username' => 'emil', 'test' => array('1', '3', '6'))); $type->addDocument($doc); $doc = new Document(3, array('id' => 3, 'email' => '*****@*****.**', 'username' => 'ruth', 'test' => array('2', '3', '7'))); $type->addDocument($doc); // Refresh index $index->refresh(); $boolQuery = new BoolQuery(); $termQuery1 = new Term(array('test' => '2')); $boolQuery->addMust($termQuery1); $resultSet = $type->search($boolQuery); $this->assertEquals(2, $resultSet->count()); $termQuery2 = new Term(array('test' => '5')); $boolQuery->addMust($termQuery2); $resultSet = $type->search($boolQuery); $this->assertEquals(1, $resultSet->count()); $termQuery3 = new Term(array('username' => 'hans')); $boolQuery->addMust($termQuery3); $resultSet = $type->search($boolQuery); $this->assertEquals(1, $resultSet->count()); $termQuery4 = new Term(array('username' => 'emil')); $boolQuery->addMust($termQuery4); $resultSet = $type->search($boolQuery); $this->assertEquals(0, $resultSet->count()); }
public function testQuery() { $client = $this->_getClient(); $index = new Index($client, 'test'); $index->create(array(), true); $type = new Type($index, 'constant_score'); $doc = new Document(1, array('id' => 1, 'email' => '*****@*****.**', 'username' => 'hans')); $type->addDocument($doc); $doc = new Document(2, array('id' => 2, 'email' => '*****@*****.**', 'username' => 'emil')); $type->addDocument($doc); $doc = new Document(3, array('id' => 3, 'email' => '*****@*****.**', 'username' => 'ruth')); $type->addDocument($doc); // Refresh index $index->refresh(); $boost = 1.3; $query_match = new MatchAll(); $query = new ConstantScore(); $query->setQuery($query_match); $query->setBoost($boost); $expectedArray = array('constant_score' => array('query' => $query_match->toArray(), 'boost' => $boost)); $this->assertEquals($expectedArray, $query->toArray()); $resultSet = $type->search($query); $results = $resultSet->getResults(); $this->assertEquals($resultSet->count(), 3); $this->assertEquals($results[1]->getScore(), 1); }
/** * @param bool $rebuild */ private function createIndex($rebuild) { $maxShardsPerNode = $this->maxShardsPerNode === 'unlimited' ? -1 : $this->maxShardsPerNode; $args = array('settings' => array('number_of_shards' => $this->shardCount, 'auto_expand_replicas' => $this->replicaCount, 'analysis' => $this->analysisConfigBuilder->buildConfig(), 'refresh_interval' => $this->refreshInterval . 's', 'merge.policy' => $this->mergeSettings, 'routing.allocation.total_shards_per_node' => $maxShardsPerNode)); if ($this->searchAllFields) { // Use our weighted all field as the default rather than _all which is disabled. $args['settings']['index.query.default_field'] = 'all'; } $this->index->create($args, $rebuild); }
/** * Initialize the ElasticSearch client, and setup settings for the client. * * @return void */ public function initialize() { spl_autoload_register(array($this, 'autoLoad')); $this->_connectionOptions = array('url' => $this->modx->getOption('sisea.elastic.hostname', null, 'http://127.0.0.1') . ':' . $this->modx->getOption('sisea.elastic.port', null, 9200) . '/'); try { $this->client = new \Elastica\Client($this->_connectionOptions); $this->index = $this->client->getIndex(strtolower($this->modx->getOption('sisea.elastic.index', null, 'siplesearchindex'))); if (!$this->index->exists()) { $indexSetup = $this->modx->getObject('modSnippet', array('name' => 'SimpleSearchElasticIndexSetup')); if ($indexSetup) { $indexOptions = $this->modx->fromJSON($this->modx->runSnippet('SimpleSearchElasticIndexSetup')); } else { $indexOptions = $this->modx->fromJSON($this->modx->runSnippet('SimpleSearchElasticIndexSetup_default')); } $this->index->create($indexOptions, true); } } catch (Exception $e) { $this->modx->log(xPDO::LOG_LEVEL_ERROR, 'Error connecting to ElasticSearch server: ' . $e->getMessage()); } }
public function testSearch() { $client = $this->_getClient(); $index = new Index($client, 'test'); $index->create(array(), true); $index->getSettings()->setNumberOfReplicas(0); //$index->getSettings()->setNumberOfShards(1); $type = new Type($index, 'helloworld'); $doc = new Document(1, array('email' => '*****@*****.**', 'username' => 'hanswurst', 'test' => array('2', '3', '5'))); $type->addDocument($doc); // Refresh index $index->refresh(); $queryString = new QueryString('test*'); $resultSet = $type->search($queryString); $this->assertEquals(1, $resultSet->count()); }
public function testSearch() { $client = $this->_getClient(); $index = new Index($client, 'test'); $index->create(array(), true); $index->getSettings()->setNumberOfReplicas(0); //$index->getSettings()->setNumberOfShards(1); $type = new Type($index, 'helloworldfuzzy'); $mapping = new Mapping($type, array('email' => array('store' => 'yes', 'type' => 'string', 'index' => 'analyzed'), 'content' => array('store' => 'yes', 'type' => 'string', 'index' => 'analyzed'))); $mapping->setSource(array('enabled' => false)); $type->setMapping($mapping); $doc = new Document(1000, array('email' => '*****@*****.**', 'content' => 'This is a sample post. Hello World Fuzzy Like This!')); $type->addDocument($doc); // Refresh index $index->refresh(); $fltQuery = new FuzzyLikeThis(); $fltQuery->setLikeText("sample gmail"); $fltQuery->addFields(array("email", "content")); $fltQuery->setMinSimilarity(0.3); $fltQuery->setMaxQueryTerms(3); $resultSet = $type->search($fltQuery); $this->assertEquals(1, $resultSet->count()); }
public function testQuery() { $client = $this->_getClient(); $index = new Index($client, 'test'); $index->create(array(), true); $type = new Type($index, 'multi_match'); $doc = new Document(1, array('id' => 1, 'name' => 'Rodolfo', 'last_name' => 'Moraes')); $type->addDocument($doc); // Refresh index $index->refresh(); $multiMatch = new MultiMatch(); $query = new Query(); $multiMatch->setQuery('Rodolfo'); $multiMatch->setFields(array('name', 'last_name')); $query->setQuery($multiMatch); $resultSet = $index->search($query); $this->assertEquals(1, $resultSet->count()); $multiMatch->setQuery('Moraes'); $multiMatch->setFields(array('name', 'last_name')); $query->setQuery($multiMatch); $resultSet = $index->search($query); $this->assertEquals(1, $resultSet->count()); }
/** * Apply the analysis factory to the index * * @param Index $index * @param AnalysisFactoryInterface $analysis */ public function setAnalysis(Index $index, AnalysisFactoryInterface $analysis) { $index->create(array('number_of_shards' => 4, 'number_of_replicas' => 1, 'analysis' => $analysis->build())); }
/** * @group functional */ public function testSearchWithLegacyFilter() { $client = $this->_getClient(); $index = new Index($client, 'test'); $index->create(array(), true); $type = new Type($index, 'helloworld'); $doc = new Document(1, array('id' => 1, 'email' => '*****@*****.**', 'username' => 'hans', 'test' => array('2', '4', '5'))); $type->addDocument($doc); $doc = new Document(2, array('id' => 2, 'email' => '*****@*****.**', 'username' => 'emil', 'test' => array('1', '3', '6'))); $type->addDocument($doc); $doc = new Document(3, array('id' => 3, 'email' => '*****@*****.**', 'username' => 'ruth', 'test' => array('2', '3', '7'))); $type->addDocument($doc); $doc = new Document(4, array('id' => 4, 'email' => '*****@*****.**', 'username' => 'john', 'test' => array('2', '4', '8'))); $type->addDocument($doc); // Refresh index $index->refresh(); $boolQuery = new BoolQuery(); $termQuery1 = new TermQuery(array('test' => '2')); $boolQuery->addMust($termQuery1); $resultSet = $type->search($boolQuery); $this->assertEquals(3, $resultSet->count()); $this->hideDeprecated(); $termFilter = new TermFilter(array('test' => '4')); $boolQuery->addFilter($termFilter); $this->showDeprecated(); $resultSet = $type->search($boolQuery); $this->assertEquals(2, $resultSet->count()); }
private function createPlaylistIndex() { // Create the index new $this->trackIndex->create(['number_of_shards' => 4, 'number_of_replicas' => 1, 'analysis' => ['analyzer' => ['indexAnalyzer' => ['type' => 'snowball', 'tokenizer' => 'standard', 'filter' => ['lowercase', 'mySnowball'], 'language' => 'English', 'stopwords' => 'a, the, in'], 'searchAnalyzer' => ['type' => 'custom', 'tokenizer' => 'standard', 'filter' => ['standard', 'lowercase', 'mySnowball']]], 'filter' => ['mySnowball' => ['type' => 'snowball', 'language' => 'English']]]], true); }
public function testSearchSetAnalyzer() { $client = $this->_getClient(); $index = new Index($client, 'test'); $index->create(array('analysis' => array('analyzer' => array('searchAnalyzer' => array('type' => 'custom', 'tokenizer' => 'standard', 'filter' => array('myStopWords'))), 'filter' => array('myStopWords' => array('type' => 'stop', 'stopwords' => array('The'))))), true); $index->getSettings()->setNumberOfReplicas(0); //$index->getSettings()->setNumberOfShards(1); $type = new Type($index, 'helloworldfuzzy'); $mapping = new Mapping($type, array('email' => array('store' => 'yes', 'type' => 'string', 'index' => 'analyzed'), 'content' => array('store' => 'yes', 'type' => 'string', 'index' => 'analyzed'))); $mapping->setSource(array('enabled' => false)); $type->setMapping($mapping); $doc = new Document(1000, array('email' => '*****@*****.**', 'content' => 'The Fuzzy Test!')); $type->addDocument($doc); $doc = new Document(1001, array('email' => '*****@*****.**', 'content' => 'Elastica Fuzzy Test')); $type->addDocument($doc); // Refresh index $index->refresh(); $fltQuery = new FuzzyLikeThis(); $fltQuery->addFields(array("email", "content")); $fltQuery->setLikeText("The"); $fltQuery->setMinSimilarity(0.1); $fltQuery->setMaxQueryTerms(3); // Test before analyzer applied, should return 1 result $resultSet = $type->search($fltQuery); $this->assertEquals(1, $resultSet->count()); $fltQuery->setParam('analyzer', 'searchAnalyzer'); $resultSet = $type->search($fltQuery); $this->assertEquals(0, $resultSet->count()); }
protected function setUp() { /** @var ElasticaService elasticaService */ $elasticaService = $this->getContainer()->get("revinate_search.elasticsearch_service"); $this->elasticaClient = $elasticaService->getInstance(); $this->index = new \Elastica\Index($this->elasticaClient, View::INDEX_NAME); if (!$this->index->exists()) { $this->index->create(array("index.number_of_replicas" => "0", "index.number_of_shards" => "1")); $this->type = new \Elastica\Type($this->index, View::INDEX_TYPE); $mappingJson = json_decode(file_get_contents(__DIR__ . "/../data/es/mapping.json"), true); $mapping = new \Elastica\Type\Mapping($this->type, $mappingJson['properties']); $this->type->setMapping($mapping); } else { $this->type = new \Elastica\Type($this->index, View::INDEX_TYPE); } $this->timeSeriesIndex = new \Elastica\Index($this->elasticaClient, StatusLog::INDEX_NAME . self::$timeSeriesTestDateSuffix); if (!$this->timeSeriesIndex->exists()) { $this->timeSeriesIndex->create(array("index.number_of_replicas" => "0", "index.number_of_shards" => "1")); $this->timeSeriesType = new \Elastica\Type($this->timeSeriesIndex, StatusLog::INDEX_TYPE); $mappingJson = json_decode(file_get_contents(__DIR__ . "/../data/es/statusLogMapping.json"), true); $mapping = new \Elastica\Type\Mapping($this->timeSeriesType, $mappingJson['properties']); $this->timeSeriesType->setMapping($mapping); } else { $this->timeSeriesType = new \Elastica\Type($this->timeSeriesIndex, StatusLog::INDEX_TYPE); } }