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()); }
$query = str_replace('\\', '', $_REQUEST['query']); $idx_type = $idx; if ('' !== $type) { $idx_type .= '/' . $type; } //OPTIONAL: uncomment to enable whitelisting //if ( ! in_array( $idx_type, array_keys( $whitelist_idx ) ) ) { // status_header( 403 ); //forbidden // die; //} try { $esclient = new Elastica_Client(array('servers' => $es_servers)); $esQ = new Elastica_Query(); $esQ->setRawQuery(get_object_vars(json_decode($query))); if (isset($whitelist_idx[$idx_type])) { $esQ->setHighlight(array('fields' => $whitelist_idx[$idx_type]['highlight'], 'pre_tags' => array('<b>'), 'post_tags' => array('</b>'))); $esQ->setFields($whitelist_idx[$idx_type]['fields']); } if ('' != $type) { $estype = $esclient->getIndex($idx)->getType($type); } else { $estype = $esclient->getIndex($idx); } $results = $estype->search($esQ); echo json_encode($results->getResponse()->getData()); } catch (Exception $e) { error_log($e->getMessage()); status_header(500); //server error echo json_encode(array('error' => 'query_error: ' . $e->getMessage())); }
public function testSetFields() { $query = new Elastica_Query(); $params = array('query' => 'test'); $query->setFields(array('firstname', 'lastname')); $data = $query->toArray(); $this->assertContains('firstname', $data['fields']); $this->assertContains('lastname', $data['fields']); $this->assertEquals(2, count($data['fields'])); }