Пример #1
0
	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());
	}
Пример #2
0
 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());
 }
Пример #3
0
 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());
 }
Пример #4
0
 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);
 }
Пример #5
0
	public function testAddWordxFile() {
		$indexMapping = array('file' => array('type' => 'attachment'), 'text' => array('type' => 'string', 'store' => 'no'),);

		$indexParams = array('index' => array('number_of_shards' => 1, 'number_of_replicas' => 0),);

		$index = $this->_createIndex();
		$type = new Elastica_Type($index, 'content');

		$index->create($indexParams, true);
		$type->setMapping($indexMapping);

		$doc1 = new Elastica_Document(1);
		$doc1->addFile('file', BASE_PATH . '/data/test.docx');
		$doc1->add('text', 'basel world');
		$type->addDocument($doc1);

		$doc2 = new Elastica_Document(2);
		$doc2->add('text', 'running in basel');
		$type->addDocument($doc2);

		$index->optimize();

		$resultSet = $type->search('xodoa');
		$this->assertEquals(1, $resultSet->count());

		$resultSet = $type->search('basel');
		$this->assertEquals(2, $resultSet->count());

		$resultSet = $type->search('ruflin');
		$this->assertEquals(0, $resultSet->count());
	}
 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();
 }