/** * @group unit */ public function testToArray() { $query = new BoolQuery(); $idsQuery1 = new Ids(); $idsQuery1->setIds(1); $idsQuery2 = new Ids(); $idsQuery2->setIds(2); $idsQuery3 = new Ids(); $idsQuery3->setIds(3); $filter1 = new TermFilter(); $filter1->setTerm('test', '1'); $filter2 = new TermFilter(); $filter2->setTerm('username', 'ruth'); $boost = 1.2; $minMatch = 2; $query->setBoost($boost); $query->setMinimumNumberShouldMatch($minMatch); $query->addMust($idsQuery1); $query->addMustNot($idsQuery2); $query->addShould($idsQuery3->toArray()); $query->addFilter($filter1); $query->addFilter($filter2); $expectedArray = array('bool' => array('must' => array($idsQuery1->toArray()), 'should' => array($idsQuery3->toArray()), 'filter' => array($filter1->toArray(), $filter2->toArray()), 'minimum_number_should_match' => $minMatch, 'must_not' => array($idsQuery2->toArray()), 'boost' => $boost)); $this->assertEquals($expectedArray, $query->toArray()); }
/** * @group functional */ public function testGeoProvided() { $index = $this->_createIndex(); $indexName = $index->getName(); $type = $index->getType('type'); $otherType = $index->getType('other_type'); // create mapping $mapping = new \Elastica\Type\Mapping($type, array('location' => array('type' => 'geo_shape'))); $type->setMapping($mapping); // create other type mapping $otherMapping = new \Elastica\Type\Mapping($type, array('location' => array('type' => 'geo_shape'))); $otherType->setMapping($otherMapping); // add type docs $type->addDocument(new \Elastica\Document('1', array('location' => array('type' => 'envelope', 'coordinates' => array(array(0.0, 50.0), array(50.0, 0.0)))))); // add other type docs $otherType->addDocument(new \Elastica\Document('2', array('location' => array('type' => 'envelope', 'coordinates' => array(array(25.0, 75.0), array(75.0, 25.0)))))); $index->optimize(); $index->refresh(); $gsp = new GeoShapePreIndexed('location', '1', 'type', $indexName, 'location'); $gsp->setRelation(AbstractGeoShape::RELATION_INTERSECT); $expected = array('geo_shape' => array('location' => array('indexed_shape' => array('id' => '1', 'type' => 'type', 'index' => $indexName, 'path' => 'location'), 'relation' => $gsp->getRelation()))); $this->assertEquals($expected, $gsp->toArray()); $query = new BoolQuery(); $query->addFilter($gsp); $results = $index->getType('type')->search($query); $this->assertEquals(1, $results->count()); $index->delete(); }
/** * @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 $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); // Return just the visible similar $filter = new BoolFilter(); $filterTerm = new Term(); $filterTerm->setTerm('visible', true); $filter->addMust($filterTerm); $query->addFilter($filter); $resultSet = $type->search($query); $this->assertEquals(2, $resultSet->count()); }
/** * @group functional */ public function testConstructEnvelope() { $index = $this->_createIndex(); $type = $index->getType('test'); // create mapping $mapping = new Mapping($type, array('location' => array('type' => 'geo_shape'))); $type->setMapping($mapping); // add docs $type->addDocument(new Document(1, array('location' => array('type' => 'envelope', 'coordinates' => array(array(-50.0, 50.0), array(50.0, -50.0)))))); $index->optimize(); $index->refresh(); $envelope = array(array(25.0, 75.0), array(75.0, 25.0)); $gsp = new GeoShapeProvided('location', $envelope); $expected = array('geo_shape' => array('location' => array('shape' => array('type' => GeoShapeProvided::TYPE_ENVELOPE, 'coordinates' => $envelope, 'relation' => AbstractGeoShape::RELATION_INTERSECT)))); $this->assertEquals($expected, $gsp->toArray()); $query = new BoolQuery(); $query->addFilter($gsp); $results = $type->search($query); $this->assertEquals(1, $results->count()); }
protected function searchDocuments() { dump(__METHOD__); $client = $this->getClient(); $index = $client->getIndex($this->getIndexName()); $type = $index->getType($this->getTypeName()); if (true) { $query = json_decode('{"query":{"bool":{"must":[{"term":{"uid":19246}},{"term":{"name":"XXXXXXXXXX"}},{"term":{"op":30}}],"filter":[{"range":{"level":{"from":10,"to":300}}},{"range":{"addtime":{"gte":"20150706T145200+0800","lte":"20150707T145203+0800"}}}]}}}', true); dump($query); $path = $index->getName() . '/' . $type->getName() . '/_search'; $response = $client->request($path, Request::GET, $query); $this->assertTrue($response->isOk()); // dump($response->getEngineTime()); // dump($response->getQueryTime()); // dump($response->getShardsStatistics()); // dump($response->getStatus()); // http status code // dump($response->getTransferInfo()); dump($response->getData()['hits']['hits']); } if (false) { $search = new Search($client); $search->addIndex($index)->addType($type); $query = new Query\BoolQuery(); // $query->setFrom(0); // $query->setSize(10); // $query->setSort(['uid' => 'asc']); // $query->setFields(['snsid', 'uid']); // $query->setHighlight(['fields' => 'uid']); // $query->setExplain(true); // $term = new Query\Term(['name' => 'XXXXXXXXXX']); // $query->setQuery($term); $query->addMust(new Term(['uid' => 19246])); $query->addMust(new Term(['name' => 'XXXXXXXXXX'])); $query->addMust(new Term(['op' => 30])); // $query->addMustNot(new Term(['country' => 'CN'])); $range = new Query\Range('level', ['from' => 10, 'to' => 300]); $query->addFilter($range); $range = new Query\Range(); $range->addField('addtime', ['gte' => '20150706T145200+0800', 'lte' => '20150707T145203+0800']); $query->addFilter($range); $search->setQuery($query); $resultSet = $search->search(); dump('Hit: ' . $resultSet->count()); $queryArray = $resultSet->getQuery()->toArray(); dump(json_encode($queryArray)); dump($resultSet->getResponse()->getData()['hits']['hits']); dump('query time: ' . \PHP_Timer::secondsToTimeString($resultSet->getResponse()->getQueryTime())); } }