public function testSearch() { $client = new Elastica_Client(); $index = new Elastica_Index($client, 'test'); $index->create(array(), true); $index->getSettings()->setNumberOfReplicas(0); //$index->getSettings()->setNumberOfShards(1); $type = new Elastica_Type($index, 'helloworldfuzzy'); $mapping = new Elastica_Type_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 Elastica_Document(1000, array('email' => '*****@*****.**', 'content' => 'This is a sample post. Hello World Fuzzy Like This!')); $type->addDocument($doc); // Refresh index $index->refresh(); $fltQuery = new Elastica_Query_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 testNoSource() { // Creates a new index 'xodoa' and a type 'user' inside this index $client = new Elastica_Client(); $index = new Elastica_Index($client, 'xodoa'); $index->create(array(), true); $type = new Elastica_Type($index, 'user'); $mapping = new Elastica_Type_Mapping($type, array('id' => array('type' => 'integer', 'store' => 'yes'), 'username' => array('type' => 'string', 'store' => 'no'))); $mapping->setSource(array('enabled' => false)); $type->setMapping($mapping); // Adds 1 document to the index $doc1 = new Elastica_Document(1, array('username' => 'hans', 'test' => array('2', '3', '5'))); $type->addDocument($doc1); // Adds a list of documents with _bulk upload to the index $docs = array(); $docs[] = new Elastica_Document(2, array('username' => 'john', 'test' => array('1', '3', '6'))); $docs[] = new Elastica_Document(3, array('username' => 'rolf', 'test' => array('2', '3', '7'))); $type->addDocuments($docs); // To update index $index->refresh(); $resultSet = $type->search('rolf'); $this->assertEquals(1, $resultSet->count()); // Tests if no source is in response except id $result = $resultSet->current(); $this->assertEquals(3, $result->getId()); $this->assertEmpty($result->getData()); }
public function testGetIdNoSource() { // Creates a new index 'xodoa' and a type 'user' inside this index $indexName = 'xodoa'; $typeName = 'user'; $client = new Elastica_Client(); $index = $client->getIndex($indexName); $index->create(array(), true); $type = $index->getType($typeName); $mapping = new Elastica_Type_Mapping($type); $mapping->disableSource(); $mapping->send(); // Adds 1 document to the index $docId = 3; $doc1 = new Elastica_Document($docId, array('username' => 'hans')); $type->addDocument($doc1); // Refreshes index $index->refresh(); sleep(2); $resultSet = $type->search('hans'); $this->assertEquals(1, $resultSet->count()); $result = $resultSet->current(); $this->assertEquals(array(), $result->getSource()); $this->assertInstanceOf('Elastica_Result', $result); $this->assertEquals($indexName, $result->getIndex()); $this->assertEquals($typeName, $result->getType()); $this->assertEquals($docId, $result->getId()); $this->assertGreaterThan(0, $result->getScore()); $this->assertInternalType('array', $result->getData()); }
public function testSearch() { $client = new Elastica_Client(); $index = new Elastica_Index($client, 'test'); $index->create(array(), true); $index->getSettings()->setNumberOfReplicas(0); //$index->getSettings()->setNumberOfShards(1); $type = new Elastica_Type($index, 'helloworldmlt'); $mapping = new Elastica_Type_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 Elastica_Document(1000, array('email' => '*****@*****.**', 'content' => 'This is a sample post. Hello World Fuzzy Like This!')); $type->addDocument($doc); $doc = new Elastica_Document(1001, array('email' => '*****@*****.**', 'content' => 'This is a fake nospam email address for gmail')); $type->addDocument($doc); // Refresh index $index->refresh(); $mltQuery = new Elastica_Query_MoreLikeThis(); $mltQuery->setLikeText('fake gmail sample'); $mltQuery->setFields(array('email', 'content')); $mltQuery->setMaxQueryTerms(1); $mltQuery->setMinDocFrequency(1); $mltQuery->setMinTermFrequency(1); $query = new Elastica_Query(); $query->setFields(array('email', 'content')); $query->setQuery($mltQuery); $resultSet = $type->search($query); $resultSet->getResponse()->getData(); $this->assertEquals(2, $resultSet->count()); }
public function testParent() { $client = new Elastica_Client(); $index = new Elastica_Index($client, 'parentchild'); $index->create(array(), true); $typeBlog = new Elastica_Type($index, 'blog'); $typeComment = new Elastica_Type($index, 'comment'); $mapping = new Elastica_Type_Mapping(); $mapping->setParam('_parent', array('type' => 'blog')); $typeComment->setMapping($mapping); $entry1 = new Elastica_Document(1); $entry1->add('title', 'Hello world'); $typeBlog->addDocument($entry1); $entry2 = new Elastica_Document(2); $entry2->add('title', 'Foo bar'); $typeBlog->addDocument($entry2); $entry3 = new Elastica_Document(3); $entry3->add('title', 'Till dawn'); $typeBlog->addDocument($entry3); $comment = new Elastica_Document(1); $comment->add('author', 'Max'); $comment->setParent(2); // Entry Foo bar $typeComment->addDocument($comment); $index->optimize(); $query = new Elastica_Query_HasChild('Max', 'comment'); $resultSet = $typeBlog->search($query); $this->assertEquals(1, $resultSet->count()); $this->assertEquals(array('title' => 'Foo bar'), $resultSet->current()->getData()); }
/** * Creates the index and sets the mapping for this type * * @param bool $recreate OPTIONAL Recreates the index if true (default = false) */ public function create($recreate = false) { $this->getIndex()->create($this->_indexParams, $recreate); $mapping = new Elastica_Type_Mapping($this->getType()); $mapping->setProperties($this->_mapping); $mapping->setSource(array('enabled' => $this->_source)); $mapping->send(); }
public function testParentMapping() { $index = $this->_createIndex(); $parenttype = new Elastica_Type($index, 'parenttype'); $parentmapping = new Elastica_Type_Mapping($parenttype, array('name' => array('type' => 'string', 'store' => 'yes'))); $parenttype->setMapping($parentmapping); $childtype = new Elastica_Type($index, 'childtype'); $childmapping = new Elastica_Type_Mapping($childtype, array('name' => array('type' => 'string', 'store' => 'yes'))); $childmapping->setParam('_parent', array('type' => 'parenttype')); $childtype->setMapping($childmapping); }
public function setUp() { $client = new Elastica_Client(); $index = $client->getIndex('elastica_test_filter_nested'); $index->create(array(), true); $type = $index->getType('user'); $mapping = new Elastica_Type_Mapping(); $mapping->setProperties( array( 'firstname' => array('type' => 'string', 'store' => 'yes'), // default is store => no expected 'lastname' => array('type' => 'string'), 'hobbies' => array( 'type' => 'nested', 'include_in_parent' => true, 'properties' => array('hobby' => array('type' => 'string')) ) ) ); $type->setMapping($mapping); // Adds a list of documents with _bulk upload to the index $docs = array(); $docs[] = new Elastica_Document(1, array( 'firstname' => 'Nicolas', 'lastname' => 'Ruflin', 'hobbies' => array( array('hobby' => 'opensource') ) ) ); $docs[] = new Elastica_Document(2, array( 'firstname' => 'Nicolas', 'lastname' => 'Ippolito', 'hobbies' => array( array('hobby' => 'opensource'), array('hobby' => 'guitar'), ) ) ); $response = $type->addDocuments($docs); // Refresh index $index->refresh(); }
public function testExcludeFileSource() { $indexMapping = array('file' => array('type' => 'attachment', 'store' => 'yes'), 'text' => array('type' => 'string', 'store' => 'yes'), 'title' => array('type' => 'string', 'store' => 'yes'),); $indexParams = array('index' => array('number_of_shards' => 1, 'number_of_replicas' => 0),); $index = $this->_createIndex(); $type = new Elastica_Type($index, 'content'); $mapping = Elastica_Type_Mapping::create($indexMapping); $mapping->setSource(array('excludes' => array('file'))); $mapping->setType($type); $index->create($indexParams, true); $type->setMapping($mapping); $docId = 1; $text = 'Basel World'; $title = 'No Title'; $doc1 = new Elastica_Document($docId); $doc1->addFile('file', BASE_PATH . '/data/test.docx'); $doc1->add('text', $text); $doc1->add('title', $title); $type->addDocument($doc1); // Optimization necessary, as otherwise source still in realtime get $index->optimize(); $data = $type->getDocument($docId)->getData(); $this->assertEquals($data['title'], $title); $this->assertEquals($data['text'], $text); $this->assertFalse(isset($data['file'])); }
public static function createType($handle) { self::init(); $local_type = self::getTypeByHandle($handle); $mapping = json_decode($local_type->mapping_json, TRUE); $type = new Elastica_Type(self::getIndex(), $handle); $type_mapping = new Elastica_Type_Mapping($type); foreach ($mapping[$handle] as $key => $value) { $type_mapping->setParam($key, $value); } $type->setMapping($type_mapping); self::getIndex()->refresh(); }
/** * Sets value type mapping for this type * * @param Elastica_Type_Mapping|array $mapping Elastica_Type_Mapping object or property array with all mappings */ public function setMapping($mapping) { $mapping = Elastica_Type_Mapping::create($mapping); $mapping->setType($this); return $mapping->send(); }
/** * Creates a mapping object * * @param array|Elastica_Type_Mapping $mapping Mapping object or properties array * @return Elastica_Type_Mapping Mapping object * @throws Elastica_Exception_Invalid If invalid type */ public static function create($mapping) { if (is_array($mapping)) { $mappingObject = new Elastica_Type_Mapping(); $mappingObject->setProperties($mapping); } else { $mappingObject = $mapping; } if (!$mappingObject instanceof Elastica_Type_Mapping) { throw new Elastica_Exception_Invalid('Invalid object type'); } return $mappingObject; }
/** * Index all images. */ public function images() { $mapping = \Elastica_Type_Mapping::create(array('title' => array('type' => 'string', 'index' => 'not_analyzed'), 'name' => array('type' => 'string', 'index' => 'not_analyzed'), 'name_la' => array('type' => 'string', 'index' => 'not_analyzed'), 'position' => array('type' => 'geo_point'), 'class' => array('type' => 'string', 'index' => 'not_analyzed'), 'family' => array('type' => 'string', 'index' => 'not_analyzed'), 'genus' => array('type' => 'string', 'index' => 'not_analyzed'), 'user' => array('type' => 'string', 'index' => 'not_analyzed'), 'redlist' => array('type' => 'string', 'index' => 'not_analyzed'), 'user' => array('type' => 'string', 'index' => 'not_analyzed'), 'date' => array('type' => 'date', 'format' => 'yyyy-MM-dd'))); $sql = ' SELECT gallery_image.id, gallery_image.title, gallery_image.item_type AS image_type, gallery_image.item_id AS image_type_id, gallery_image.created_date AS date, fauna_organism.name_de AS name, ARRAY_TO_STRING(ARRAY[fauna_organism.genus, fauna_organism.species], \' \') AS name_la, NULL AS centroid, fauna_class.name_de AS class, fauna_organism.family AS family, fauna_organism.genus AS genus, fauna_organism.redlist AS redlist, ARRAY_TO_STRING(ARRAY[ua.field_address_first_name, ua.field_address_last_name], \' \') AS user, \'organism/\' || gallery_image.item_id AS url FROM gallery_image INNER JOIN file_managed ON file_managed.fid = gallery_image.fid LEFT JOIN organism ON gallery_image.item_id = organism.id INNER JOIN fauna_organism ON fauna_organism.id = organism.organism_id INNER JOIN fauna_class ON fauna_class.id = fauna_organism.fauna_class_id INNER JOIN users ON users.uid = gallery_image.owner_id LEFT JOIN field_data_field_address ua ON ua.entity_id = users.uid WHERE file_managed.filemime = \'image/jpeg\' AND gallery_image.item_type = \'organism\' AND organism.organism_type = 1 UNION SELECT gallery_image.id, gallery_image.title, gallery_image.item_type AS image_type, gallery_image.item_id AS image_type_id, gallery_image.created_date AS date, flora_organism.name_de AS name, ARRAY_TO_STRING(ARRAY[flora_organism."Gattung", flora_organism."Art"], \' \') AS name_la, NULL AS centroid, \'Pflanzen\' AS class, flora_organism."Familie" AS family, flora_organism."Gattung" AS genus, NULL AS redlist, ARRAY_TO_STRING(ARRAY[ua.field_address_first_name, ua.field_address_last_name], \' \') AS user, \'organism/\' || gallery_image.item_id AS url FROM gallery_image INNER JOIN file_managed ON file_managed.fid = gallery_image.fid LEFT JOIN organism ON gallery_image.item_id = organism.id INNER JOIN flora_organism ON flora_organism.id = organism.organism_id INNER JOIN users ON users.uid = gallery_image.owner_id LEFT JOIN field_data_field_address ua ON ua.entity_id = users.uid WHERE file_managed.filemime = \'image/jpeg\' AND gallery_image.item_type = \'organism\' AND organism.organism_type = 2 UNION SELECT gallery_image.id, gallery_image.title, gallery_image.item_type AS image_type, gallery_image.item_id AS image_type_id, gallery_image.created_date AS date, habitat.name_de AS name, habitat.name_lt AS name_la, NULL AS centroid, NULL AS class, NULL AS family, NULL AS genus, NULL AS redlist, ARRAY_TO_STRING(ARRAY[ua.field_address_first_name, ua.field_address_last_name], \' \') AS user, \'habitat/\' || gallery_image.item_id AS url FROM gallery_image INNER JOIN file_managed ON file_managed.fid = gallery_image.fid LEFT JOIN habitat ON habitat.id = gallery_image.item_id INNER JOIN users ON users.uid = gallery_image.owner_id LEFT JOIN field_data_field_address ua ON ua.entity_id = users.uid WHERE file_managed.filemime = \'image/jpeg\' AND gallery_image.item_type = \'habitat\' UNION SELECT gallery_image.id, gallery_image.title, gallery_image.item_type AS image_type, gallery_image.item_id AS image_type_id, gallery_image.created_date AS date, head_inventory.name AS name, NULL AS name_la, ST_AsGeoJSON(ST_Centroid(area.geom)) AS centroid, NULL AS class, NULL AS family, NULL AS genus, NULL AS redlist, ARRAY_TO_STRING(ARRAY[ua.field_address_first_name, ua.field_address_last_name], \' \') AS user, \'inventory/\' || gallery_image.item_id || \'/gallery\' AS url FROM gallery_image INNER JOIN file_managed ON file_managed.fid = gallery_image.fid LEFT JOIN head_inventory ON head_inventory.id = gallery_image.item_id INNER JOIN area ON area.id = head_inventory.area_id INNER JOIN users ON users.uid = gallery_image.owner_id LEFT JOIN field_data_field_address ua ON ua.entity_id = users.uid WHERE file_managed.filemime = \'image/jpeg\' AND gallery_image.item_type = \'head_inventory\' UNION SELECT gallery_image.id, gallery_image.title, gallery_image.item_type AS image_type, gallery_image.item_id AS image_type_id, gallery_image.created_date AS date, head_inventory.name AS name, NULL AS name_la, ST_AsGeoJSON(ST_Centroid(area.geom)) AS centroid, NULL AS class, NULL AS family, NULL AS genus, NULL AS redlist, ARRAY_TO_STRING(ARRAY[ua.field_address_first_name, ua.field_address_last_name], \' \') AS user, \'inventory/\' || head_inventory.id || \'/gallery\' AS url FROM gallery_image INNER JOIN file_managed ON file_managed.fid = gallery_image.fid LEFT JOIN inventory_entry ON inventory_entry.id = gallery_image.item_id INNER JOIN inventory ON inventory_entry.inventory_id = inventory.id INNER JOIN head_inventory ON head_inventory.id = inventory.head_inventory_id INNER JOIN area ON area.id = head_inventory.area_id INNER JOIN users ON users.uid = gallery_image.owner_id LEFT JOIN field_data_field_address ua ON ua.entity_id = users.uid WHERE file_managed.filemime = \'image/jpeg\' AND gallery_image.item_type = \'inventory_entry\' UNION SELECT gallery_image.id, gallery_image.title, gallery_image.item_type AS image_type, gallery_image.item_id AS image_type_id, gallery_image.created_date AS date, area.field_name AS name, NULL AS name_la, ST_AsGeoJSON(ST_Centroid(area.geom)) AS centroid, NULL AS class, NULL AS family, NULL AS genus, NULL AS redlist, ARRAY_TO_STRING(ARRAY[ua.field_address_first_name, ua.field_address_last_name], \' \') AS user, \'area/\' || area.id AS url FROM gallery_image INNER JOIN file_managed ON file_managed.fid = gallery_image.fid LEFT JOIN area ON area.id = gallery_image.item_id INNER JOIN users ON users.uid = gallery_image.owner_id LEFT JOIN field_data_field_address ua ON ua.entity_id = users.uid WHERE file_managed.filemime = \'image/jpeg\' AND gallery_image.item_type = \'area\''; $this->sql('image', $sql, $mapping); }
protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) { $table = new Console\Helper\TableHelper(); $indexArgument = $input->getArgument('index'); $typeArgument = $input->getArgument('type'); $dropIndexOption = $input->getOption('dropIndex'); $dropTypeOption = $input->getOption('dropType'); if ($indexArgument === 'all') { $indices = $this->indices; } else { $indices = explode(' ', $indexArgument); } $dropIndex = (bool) $dropIndexOption; $dropType = (bool) $dropTypeOption; if ($typeArgument === 'all') { $types = $this->types; $dropIndex = true; } else { $types = explode(' ', $typeArgument); } foreach ($indices as $indexName => $indexData) { dump($indexName, $indexData); exit; $index = $this->elastica->getIndex($indexName); if ($dropIndex === true) { try { $output->writeln(sprintf('dropping index %s', $indexName)); //$response = $index->delete(); $output->writeln(sprintf('index %s dropped', $indexName)); } catch (\Elastica_Exception_Response $e) { $output->writeln(sprintf('index %s didnt exist', $indexName)); } $output->writeln(''); $output->writeln(sprintf('creating index %s', $indexName)); $output->writeln(''); } try { $index->create(['analysis' => ['analyzer' => ['default_index' => ['type' => 'custom', 'tokenizer' => 'lowercase', 'filter' => ['standard', 'lowercase', 'asciifolding', 'indexNGram', 'trim', 'unique', 'stopwordsFilter']], 'default_search' => ['type' => 'custom', 'tokenizer' => 'lowercase', 'filter' => ['standard', 'lowercase', 'asciifolding', 'searchNGram', 'trim', 'unique']]], 'filter' => ['indexNGram' => ["type" => "nGram", "min_gram" => 4, "max_gram" => 50], 'searchNGram' => ["type" => "nGram", "min_gram" => 4, "max_gram" => 50], 'stopwordsFilter' => ['type' => 'stop', 'stopwords' => $stopwords]]]], $dropIndex); } catch (\Elastica_Exception_Response $e) { $output->writeln(sprintf('<error>%s</error>', $e->getMessage())); } //} $typesToCreate = array_intersect($types, $this->indicesTypes[$indexName]); foreach ($typesToCreate as $typeName) { $output->writeln(sprintf('creating mapping for type: <info>%s</info> in index: <info>%s</info>', $typeName, $indexName)); $output->writeln(''); if (!isset($this->mappings[$typeName])) { $output->writeln(sprintf('<error>Mappings for type: %s are not defined.</error>', $typeName)); $output->writeln(''); continue; } $elasticaType = $index->getType($typeName); if ($dropType === true) { try { $output->writeln(sprintf('deleting type <info>%s</info> from index: <info>%s</info>', $typeName, $indexName)); $elasticaType->delete(); } catch (\Elastica_Exception_Response $e) { $output->writeln(sprintf('<error>%s</error>', $e->getMessage())); } $output->writeln(''); } $mapping = new \Elastica_Type_Mapping(); $mapping->setType($elasticaType); // Set mapping $mapping->setProperties($this->mappings[$typeName]); // Send mapping to type $mapping->send(); } } }
/** * Creates or updates Elasticsearch index. * * @link http://www.elasticsearch.org/guide/reference/mapping/core-types.html * @link http://www.elasticsearch.org/guide/reference/mapping/multi-field-type.html * @return JR_Search_Model_Resource_Engine_Elasticsearch_Client * @throws Exception */ protected function _prepareIndex() { try { $indexSettings = $this->_getIndexSettings(); $index = $this->getIndex($this->_index); if (!$index->exists()) { $indexSettings['number_of_shards'] = (int) $this->getConfig('number_of_shards'); $index->create($indexSettings); } else { $index->setSettings($indexSettings); } $mapping = new Elastica_Type_Mapping(); $mapping->setType($index->getType('product')); $mapping->setProperties($this->_getIndexProperties()); $mapping->send(); } catch (Exception $e) { Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); throw $e; } return $this; }