/** * Register the service provider. * * @return void */ public function register() { $this->app['sherlock'] = $this->app->share(function ($app) { $sherlock = new Sherlock(); foreach (Config::get('sherlock::nodes') as $node) { $sherlock->addNode($node['host'], $node['port']); } return $sherlock; }); }
public function testIllegalJson() { $sherlock = $this->object; $req = $this->object->search(); $req->index("testqueries")->type("test"); $query = Sherlock\Sherlock::queryBuilder()->Raw('Illegal JSON'); $req->query($query); $this->assertThrowsException('\\Sherlock\\common\\exceptions\\SearchPhaseExecutionException', function () use($req) { $req->execute(); }); }
/** * @inheritdoc */ public function search($indexName, $indexType, $querystring, $json = false, $from = null, $size = null) { if ($json) { $request = $this->sherlock->raw(); $request->uri($indexName . "/" . $indexType . "/_search")->method("post")->body($querystring); } else { $request = $this->sherlock->search(); $titleQuery = Sherlock::queryBuilder()->Match()->field("title")->query($querystring)->fuzziness(0.7); $contentQuery = Sherlock::queryBuilder()->Match()->field("content")->query($querystring)->fuzziness(0.7); $query = Sherlock::queryBuilder()->Bool()->should($titleQuery, $contentQuery)->minimum_number_should_match(1); $highlight = Sherlock::highlightBuilder()->Highlight()->pre_tags(array("<strong>"))->post_tags(array("</strong>"))->fields(array("content" => array("fragment_size" => 150, "number_of_fragments" => 1))); $request->highlight($highlight); $request->index($indexName)->type($indexType)->query($query); if ($from) { $request->from($from); } if ($size) { $request->size($size); } } $response = $request->execute(); return $response->responseData; }
/** * @covers sherlock\Sherlock\components\facets\TermsFacet::fields * @covers sherlock\Sherlock\components\facets\TermsFacet::facetname * @covers sherlock\Sherlock\components\facets\TermsFacet::exclude * @covers sherlock\Sherlock\components\facets\TermsFacet::order * @covers sherlock\Sherlock\components\facets\TermsFacet::regex * @covers sherlock\Sherlock\components\facets\TermsFacet::regex_flags * @covers sherlock\Sherlock\components\facets\TermsFacet::script * @covers sherlock\Sherlock\components\facets\TermsFacet::script_field * @covers sherlock\Sherlock\requests\SearchRequest::facet */ public function testTermsFacet() { $req = $this->object->search(); $req->index("testfacets")->type("test"); $query = Sherlock::queryBuilder()->MatchAll(); $req->query($query); //no parameter test, should throw an exception because Fields is not set $facet = Sherlock::facetBuilder()->Terms(); $this->assertThrowsException('\\Sherlock\\common\\exceptions\\RuntimeException', function () use($facet) { $data = $facet->toJSON(); }); //Set Fields, but not facetname - they should be the same $facet = Sherlock::facetBuilder()->Terms()->fields("testfield"); $req->facets($facet); $data = $req->toJSON(); $expectedData = '{"query":{"match_all":[]},"facets":{"testfield":{"terms":{"fields":["testfield"],"order":"count","all_terms":false,"size":null,"exclude":null,"regex":null,"regex_flags":null,"script":null,"script_field":null,"params":null,"lang":null},"facet_filter":null,"nested":null}}}'; $this->assertEquals($expectedData, $data); $resp = $req->execute(); //Set both fields and facetname $facet = Sherlock::facetBuilder()->Terms()->fields("testfield")->facetname("testfield1"); $req->facets($facet); $data = $req->toJSON(); $expectedData = '{"query":{"match_all":[]},"facets":{"testfield1":{"terms":{"fields":["testfield"],"order":"count","all_terms":false,"size":null,"exclude":null,"regex":null,"regex_flags":null,"script":null,"script_field":null,"params":null,"lang":null},"facet_filter":null,"nested":null}}}'; $this->assertEquals($expectedData, $data); $resp = $req->execute(); //Set multiple fields (arguments), make sure facetname stays as the first $facet = Sherlock::facetBuilder()->Terms()->fields("testfield", "testfield1", "testfield2"); $req->facets($facet); $data = $req->toJSON(); $expectedData = '{"query":{"match_all":[]},"facets":{"testfield":{"terms":{"fields":["testfield","testfield1","testfield2"],"order":"count","all_terms":false,"size":null,"exclude":null,"regex":null,"regex_flags":null,"script":null,"script_field":null,"params":null,"lang":null},"facet_filter":null,"nested":null}}}'; $this->assertEquals($expectedData, $data); $resp = $req->execute(); //Set multiple fields (array) , make sure facetname stays as the first $facet = Sherlock::facetBuilder()->Terms()->fields(array("testfield", "testfield1", "testfield2")); $req->facets($facet); $data = $req->toJSON(); $expectedData = '{"query":{"match_all":[]},"facets":{"testfield":{"terms":{"fields":["testfield","testfield1","testfield2"],"order":"count","all_terms":false,"size":null,"exclude":null,"regex":null,"regex_flags":null,"script":null,"script_field":null,"params":null,"lang":null},"facet_filter":null,"nested":null}}}'; $this->assertEquals($expectedData, $data); $resp = $req->execute(); //Set all fields just to make sure they wrok $facet = Sherlock::facetBuilder()->Terms()->fields("testfield")->facetname("testfield1")->all_terms(true)->exclude(array("term1", "term2"))->order('count')->regex("/./")->regex_flags("DOTALL")->script("_score")->script_field("_source.testfield"); $req->facets($facet); $data = $req->toJSON(); $expectedData = '{"query":{"match_all":[]},"facets":{"testfield1":{"terms":{"fields":["testfield"],"order":"count","all_terms":true,"size":null,"exclude":["term1","term2"],"regex":"\\/.\\/","regex_flags":"DOTALL","script":"_score","script_field":"_source.testfield","params":null,"lang":null},"facet_filter":null,"nested":null}}}'; $this->assertEquals($expectedData, $data); $resp = $req->execute(); }
/** * @covers sherlock\Sherlock\components\filters\Type::value * @covers sherlock\Sherlock\requests\SearchRequest::query * @covers sherlock\Sherlock\requests\SearchRequest::toJSON */ public function testType() { $req = $this->object->search(); $req->index("testfilters")->type("test"); $filter = Sherlock::filterBuilder()->Type()->value("testString"); $query = Sherlock::queryBuilder()->MatchAll(); $req->query($query); $req->filter($filter); $data = $req->toJSON(); $expectedData = '{"query":{"match_all":[]},"filter":{"type":{"value":"testString"}}}'; $this->assertEquals($expectedData, $data); $resp = $req->execute(); }
public function testRawQueryBuilding() { $req = $this->object->search(); $req->index("test3")->type("benchmark"); $expectedData = array("query" => array("term" => array("field1" => array("value" => "town")))); $req->query(Sherlock::queryBuilder()->Raw($expectedData['query'])); $data = $req->toJSON(); $expectedData = json_encode($expectedData); $this->assertEquals($expectedData, $data); }
/** * @covers sherlock\Sherlock::index * @todo make this test actually assert things */ public function testIndexOperations() { $sherlock = $this->object; //Create the index $index = $sherlock->index('testnewindex'); $this->assertInstanceOf('\\sherlock\\requests\\IndexRequest', $index); $response = $index->create(); $this->assertInstanceOf('\\sherlock\\responses\\IndexResponse', $response); $this->assertEquals(true, $response->ok); //set a setting $index->settings(Sherlock::indexSettingsBuilder()->refresh_interval("1s")); $this->assertInstanceOf('\\sherlock\\requests\\IndexRequest', $index); $response = $index->updateSettings(); $this->assertInstanceOf('\\sherlock\\responses\\IndexResponse', $response); $this->assertEquals(true, $response->ok); //Delete the index first $response = $sherlock->index('testnewindex')->delete(); $this->assertInstanceOf('\\sherlock\\responses\\IndexResponse', $response); $this->assertEquals(true, $response->ok); }
public function testAnalyzerMapping() { $sherlock = $this->object; //Set the index $index = $sherlock->index('testanalyzermapping'); //no path, expect error $this->assertThrowsException('\\sherlock\\common\\exceptions\\BadMethodCallException', function () { $mapping = Sherlock::mappingBuilder()->Analyzer(); }); //type, field, format $mapping = Sherlock::mappingBuilder('testType')->Analyzer()->path('testField'); $data = $mapping->toJSON(); $expected = '{"_analyzer":{"path":"testField"}}'; $this->assertEquals($expected, $data); $type = 'data'; $index->mappings(Sherlock::mappingBuilder($type)->String()->field('parents')->analyzer('keyword'), Sherlock::mappingBuilder($type)->String()->field('ancestors')->analyzer('keyword'), Sherlock::mappingBuilder($type)->String()->field('tags')->analyzer('keyword'), Sherlock::mappingBuilder($type)->String()->field('type')->analyzer('keyword'), Sherlock::mappingBuilder($type)->String()->field('slug')->analyzer('keyword'), Sherlock::mappingBuilder($type)->Analyzer()->path("contentanalyzer")); $index->create(); $response = $index->delete(); $this->assertEquals(true, $response->ok); }
/** * @covers sherlock\Sherlock\components\queries\CustomFiltersScore::filter * @covers sherlock\Sherlock\requests\SearchRequest::query * @covers sherlock\Sherlock\requests\SearchRequest::toJSON */ public function testMultipleFilter() { $req = $this->object->search(); $req->index("testqueries")->type("test"); $filter = Sherlock::filterBuilder()->Term()->field("auxillary")->term("auxillary"); $query = Sherlock::queryBuilder()->CustomFiltersScore()->query(Sherlock::queryBuilder()->Term()->field("auxillary")->term("auxillary"))->filter($filter, 2)->filter($filter, 3)->filter($filter, 4)->score_mode("first")->max_boost(0.5); $req->query($query); $data = $req->toJSON(); $expectedData = '{"query":{"custom_filters_score":{"query":{"term":{"auxillary":{"value":"auxillary"}}},"filters":[{"filter":{"term":{"auxillary":"auxillary","_cache":true}},"boost":2},{"filter":{"term":{"auxillary":"auxillary","_cache":true}},"boost":3},{"filter":{"term":{"auxillary":"auxillary","_cache":true}},"boost":4}],"score_mode":"first","max_boost":0.5}}}'; $this->assertEquals($expectedData, $data); $resp = $req->execute(); }