本文整理汇总了PHP中Elastica\Type::search方法的典型用法代码示例。如果您正苦于以下问题:PHP Type::search方法的具体用法?PHP Type::search怎么用?PHP Type::search使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Elastica\Type
的用法示例。
在下文中一共展示了Type::search方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testSearch
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' => 'hans@test.com', 'username' => 'hans', 'test' => array('2', '3', '5')));
$type->addDocument($doc);
$doc = new Document(2, array('id' => 2, 'email' => 'emil@test.com', 'username' => 'emil', 'test' => array('1', '3', '6')));
$type->addDocument($doc);
$doc = new Document(3, array('id' => 3, 'email' => 'ruth@test.com', 'username' => 'ruth', 'test' => array('2', '3', '7')));
$type->addDocument($doc);
// Refresh index
$index->refresh();
$boolQuery = new Bool();
$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());
}
示例2: testSearchByDocument
/**
* @group functional
*/
public function testSearchByDocument()
{
$client = $this->_getClient(array('persistent' => false));
$index = $client->getIndex('elastica_test');
$index->create(array('index' => array('number_of_shards' => 1, 'number_of_replicas' => 0)), true);
$type = new Type($index, 'mlt_test');
$type->addDocuments(array(new Document(1, array('visible' => true, 'name' => 'bruce wayne batman')), new Document(2, array('visible' => true, 'name' => 'bruce wayne')), new Document(3, array('visible' => false, 'name' => 'bruce wayne')), new Document(4, array('visible' => true, 'name' => 'batman')), new Document(5, array('visible' => false, 'name' => 'batman')), new Document(6, array('visible' => true, 'name' => 'superman')), new Document(7, array('visible' => true, 'name' => 'spiderman'))));
$index->refresh();
$doc = $type->getDocument(1);
// Return all similar from id
$mltQuery = new MoreLikeThis();
$mltQuery->setMinTermFrequency(1);
$mltQuery->setMinDocFrequency(1);
$mltQuery->setLike($doc);
$query = new Query($mltQuery);
$resultSet = $type->search($query);
$this->assertEquals(4, $resultSet->count());
$mltQuery = new MoreLikeThis();
$mltQuery->setMinTermFrequency(1);
$mltQuery->setMinDocFrequency(1);
$mltQuery->setLike($doc);
$query = new Query\BoolQuery();
$query->addMust($mltQuery);
$this->hideDeprecated();
// Return just the visible similar from id
$filter = new Query\BoolQuery();
$filterTerm = new Query\Term();
$filterTerm->setTerm('visible', true);
$filter->addMust($filterTerm);
$query->addFilter($filter);
$this->showDeprecated();
$resultSet = $type->search($query);
$this->assertEquals(2, $resultSet->count());
// Return all similar from source
$mltQuery = new MoreLikeThis();
$mltQuery->setMinTermFrequency(1);
$mltQuery->setMinDocFrequency(1);
$mltQuery->setMinimumShouldMatch(90);
$mltQuery->setLike($type->getDocument(1)->setId(''));
$query = new Query($mltQuery);
$resultSet = $type->search($query);
$this->assertEquals(1, $resultSet->count());
// Legacy test with filter
$mltQuery = new MoreLikeThis();
$mltQuery->setMinTermFrequency(1);
$mltQuery->setMinDocFrequency(1);
$mltQuery->setLike($doc);
$query = new Query\BoolQuery();
$query->addMust($mltQuery);
$this->hideDeprecated();
// Return just the visible similar
$filter = new BoolFilter();
$filterTerm = new Term();
$filterTerm->setTerm('visible', true);
$filter->addMust($filterTerm);
$query->addFilter($filter);
$this->showDeprecated();
$resultSet = $type->search($query);
$this->assertEquals(2, $resultSet->count());
}
示例3: testSearch
/**
* @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' => 'testemail@gmail.com', 'content' => 'This is a sample post. Hello World Fuzzy Like This!'));
$type->addDocument($doc);
$doc = new Document(1001, array('email' => 'nospam@gmail.com', 'content' => 'This is a fake nospam email address for gmail'));
$type->addDocument($doc);
// Refresh index
$index->refresh();
$mltQuery = new MoreLikeThis();
$mltQuery->setLikeText('fake gmail sample');
$mltQuery->setFields(array('email', 'content'));
$mltQuery->setMaxQueryTerms(1);
$mltQuery->setMinDocFrequency(1);
$mltQuery->setMinTermFrequency(1);
$query = new Query();
$query->setFields(array('email', 'content'));
$query->setQuery($mltQuery);
$resultSet = $type->search($query);
$resultSet->getResponse()->getData();
$this->assertEquals(2, $resultSet->count());
}
示例4: testQuery
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' => 'hans@test.com', 'username' => 'hans'));
$type->addDocument($doc);
$doc = new Document(2, array('id' => 2, 'email' => 'emil@test.com', 'username' => 'emil'));
$type->addDocument($doc);
$doc = new Document(3, array('id' => 3, 'email' => 'ruth@test.com', '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);
}
示例5: testNegativeBoost
public function testNegativeBoost()
{
$keyword = "vital";
$negativeKeyword = "mercury";
$query = new Boosting();
$positiveQuery = new \Elastica\Query\Term(array('name' => $keyword));
$negativeQuery = new \Elastica\Query\Term(array('name' => $negativeKeyword));
$query->setPositiveQuery($positiveQuery);
$query->setNegativeQuery($negativeQuery);
$query->setNegativeBoost(0.2);
$response = $this->type->search($query);
$results = $response->getResults();
$this->assertEquals($response->getTotalHits(), 4);
$lastResult = $results[3]->getData();
$this->assertEquals($lastResult['name'], $this->sampleData[2]['name']);
}
示例6: testSearch
/**
* @group functional
*/
public function testSearch()
{
$index = $this->_createIndex();
$index->getSettings()->setNumberOfReplicas(0);
$type = new Type($index, 'helloworld');
$doc = new Document(1, array('email' => 'test@test.com', 'username' => 'test 7/6 123', 'test' => array('2', '3', '5')));
$type->addDocument($doc);
// Refresh index
$index->refresh();
$queryString = new QueryString(Util::escapeTerm('test 7/6'));
$resultSet = $type->search($queryString);
$this->assertEquals(1, $resultSet->count());
}
示例7: testScriptScore
public function testScriptScore()
{
$scriptString = "_score * doc['price'].value";
$script = new Script($scriptString);
$query = new FunctionScore();
$query->addScriptScoreFunction($script);
$expected = array('function_score' => array('functions' => array(array('script_score' => array('script' => $scriptString)))));
$this->assertEquals($expected, $query->toArray());
$response = $this->type->search($query);
$results = $response->getResults();
// the document the highest price should be scored highest
$result0 = $results[0]->getData();
$this->assertEquals("Miller's Field", $result0['name']);
}
示例8: testSearch
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' => 'test@test.com', '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());
}
示例9: testSearch
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' => 'testemail@gmail.com', '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());
}
示例10: testAddWordxFile
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 Type($index, 'content');
$index->create($indexParams, true);
$type->setMapping($indexMapping);
$doc1 = new Document(1);
$doc1->addFile('file', BASE_PATH . '/data/test.docx');
$doc1->set('text', 'basel world');
$type->addDocument($doc1);
$doc2 = new Document(2);
$doc2->set('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());
}
示例11: reindexInternal
private function reindexInternal(Type $type, Type $oldType, $children, $childNumber, $chunkSize, $retryAttempts)
{
$filter = null;
$messagePrefix = "";
if ($childNumber === 1 && $children === 1) {
$this->outputIndented("\t\tStarting single process reindex\n");
} else {
if ($childNumber >= $children) {
$this->error("Invalid parameters - childNumber >= children ({$childNumber} >= {$children}) ", 1);
}
$messagePrefix = "\t\t[{$childNumber}] ";
$this->outputIndented($messagePrefix . "Starting child process reindex\n");
// Note that it is not ok to abs(_uid.hashCode) because hashCode(Integer.MIN_VALUE) == Integer.MIN_VALUE
$filter = new \CirrusSearch\Extra\Filter\IdHashMod($children, $childNumber);
}
$properties = $this->mappingConfig[$oldType->getName()]['properties'];
try {
$query = new Query();
$query->setFields(array('_id', '_source'));
if ($filter) {
$query->setQuery(new \Elastica\Query\Filtered(new \Elastica\Query\MatchAll(), $filter));
}
// Note here we dump from the current index (using the alias) so we can use Connection::getPageType
$result = $oldType->search($query, array('search_type' => 'scan', 'scroll' => '1h', 'size' => $chunkSize));
$totalDocsToReindex = $result->getResponse()->getData();
$totalDocsToReindex = $totalDocsToReindex['hits']['total'];
$this->outputIndented($messagePrefix . "About to reindex {$totalDocsToReindex} documents\n");
$operationStartTime = microtime(true);
$completed = 0;
$self = $this;
Util::iterateOverScroll($this->index, $result->getResponse()->getScrollId(), '1h', function ($results) use($properties, $retryAttempts, $messagePrefix, $self, $type, &$completed, $totalDocsToReindex, $operationStartTime) {
$documents = array();
foreach ($results as $result) {
$documents[] = $self->buildNewDocument($result, $properties);
}
$self->withRetry($retryAttempts, $messagePrefix, 'retrying as singles', function () use($self, $type, $messagePrefix, $documents) {
$self->sendDocuments($type, $messagePrefix, $documents);
});
$completed += sizeof($results);
$rate = round($completed / (microtime(true) - $operationStartTime));
$this->outputIndented($messagePrefix . "Reindexed {$completed}/{$totalDocsToReindex} documents at {$rate}/second\n");
}, 0, $retryAttempts, function ($e, $errors) use($self, $messagePrefix) {
$self->sleepOnRetry($e, $errors, $messagePrefix, 'fetching documents to reindex');
});
$this->outputIndented($messagePrefix . "All done\n");
} catch (ExceptionInterface $e) {
// Note that we can't fail the master here, we have to check how many documents are in the new index in the master.
$type = get_class($e);
$message = ElasticsearchIntermediary::extractMessage($e);
LoggerFactory::getInstance('CirrusSearch')->warning("Search backend error during reindex. Error type is '{type}' and message is: {message}", array('type' => $type, 'message' => $message));
die(1);
}
}
示例12: testOldObject
/**
* @group functional
*/
public function testOldObject()
{
if (version_compare(phpversion(), 7, '>=')) {
self::markTestSkipped('These objects are not supported in PHP 7');
}
$index = $this->_createIndex();
$type = new Type($index, 'test');
$docNumber = 3;
for ($i = 0; $i < $docNumber; ++$i) {
$doc = new Document($i, array('email' => 'test@test.com'));
$type->addDocument($doc);
}
$index->refresh();
$this->hideDeprecated();
$boolQuery = new \Elastica\Query\Bool();
$this->showDeprecated();
$resultSet = $type->search($boolQuery);
$this->assertEquals($resultSet->count(), $docNumber);
}
示例13: testAddObject
/**
* @group functional
*/
public function testAddObject()
{
$index = $this->_createIndex();
$type = new Type($index, 'user');
$type->setSerializer('get_object_vars');
$userObject = new \stdClass();
$userObject->username = 'hans';
$userObject->test = array('2', '3', '5');
$type->addObject($userObject);
$index->refresh();
$resultSet = $type->search('hans');
$this->assertEquals(1, $resultSet->count());
// Test if source is returned
$result = $resultSet->current();
$data = $result->getData();
$this->assertEquals('hans', $data['username']);
}
示例14: testSearchSetAnalyzer
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' => 'testemail@gmail.com', 'content' => 'The Fuzzy Test!'));
$type->addDocument($doc);
$doc = new Document(1001, array('email' => 'testemail@gmail.com', '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());
}
示例15: testCount
/**
* @Testing count
*/
public function testCount()
{
$index = $this->_createIndex();
$type = new Type($index, 'user');
// Adds a list of documents with _bulk upload to the index
$docs = array();
$docs[] = new Document(2, array('username' => 'rolf', 'test' => array('1', '3', '6')));
$docs[] = new Document(3, array('username' => 'rolf', 'test' => array('2', '3', '7')));
$type->addDocuments($docs);
$index->refresh();
$resultSet = $type->search('rolf');
$this->assertEquals(2, $resultSet->count());
$count = $type->count('rolf');
$this->assertEquals(2, $count);
$resultSet = $type->count('rolf', true);
$this->assertEquals(2, $resultSet->count());
// Test if source is returned
$result = $resultSet->current();
$data = $result->getData();
$this->assertEquals('rolf', $data['username']);
}