function find(Search_Query_Interface $query, $resultStart, $resultCount) { $builder = new Search_Elastic_QueryBuilder(); $queryPart = $builder->build($query->getExpr()); $builder = new Search_Elastic_OrderBuilder(); $orderPart = $builder->build($query->getSortOrder()); $builder = new Search_Elastic_FacetBuilder(); $facetPart = $builder->build($query->getFacets()); $fullQuery = array_merge($queryPart, $orderPart, $facetPart, array("from" => $resultStart, "size" => $resultCount, "highlight" => array("fields" => array('contents' => array("number_of_fragments" => 5))))); $result = $this->connection->search($this->index, $fullQuery, $resultStart, $resultCount); $hits = $result->hits; $entries = array_map(function ($entry) { $data = (array) $entry->_source; if (isset($entry->highlight->contents)) { $data['_highlight'] = implode('...', $entry->highlight->contents); } else { $data['_highlight'] = ''; } return $data; }, $hits->hits); $resultSet = new Search_Elastic_ResultSet($entries, $hits->total, $resultStart, $resultCount); $reader = new Search_Elastic_FacetReader($result); foreach ($query->getFacets() as $facet) { if ($filter = $reader->getFacetFilter($facet)) { $resultSet->addFacetFilter($filter); } } return $resultSet; }
function testFlattenAnd() { $builder = new QueryBuilder(); $query = $builder->build(new AndX(array(new OrX(array(new Token('Hello', 'plaintext', 'contents', 1.5), new Token('World', 'plaintext', 'contents', 1.0))), new AndX(array(new Token('Hello', 'plaintext', 'contents', 1.5), new Token('World', 'plaintext', 'contents', 1.0))), new Token('Test', 'plaintext', 'contents', 1.0)))); $this->assertEquals(array("bool" => array("must" => array(array("bool" => array("should" => array(array("match" => array("contents" => array("query" => "hello", "boost" => 1.5))), array("match" => array("contents" => array("query" => "world", "boost" => 1.0)))), "minimum_number_should_match" => 1)), array("match" => array("contents" => array("query" => "hello", "boost" => 1.5))), array("match" => array("contents" => array("query" => "world", "boost" => 1.0))), array("match" => array("contents" => array("query" => "test", "boost" => 1.0)))))), $query['query']); }
function testMoreLikeThisThroughAbstraction() { $builder = new QueryBuilder(); $builder->setDocumentReader(function ($type, $object) { return array('object_type' => $type, 'object_id' => $object, 'contents' => 'hello world'); }); $q = new Search_Query(); $q->filterSimilar('wiki page', 'A'); $query = $builder->build($q->getExpr()); $this->assertEquals(array('bool' => array('must' => array(array('more_like_this' => array('fields' => array('contents'), 'like_text' => 'hello world', 'boost' => 1.0))), 'must_not' => array(array('bool' => array('must' => array(array("match" => array("object_type" => array("query" => "wiki page"))), array("match" => array("object_id" => array("query" => "A"))))))))), $query['query']); }
function store($name, Search_Expr_Interface $expr) { $builder = new Search_Elastic_QueryBuilder(); $builder->setDocumentReader($this->createDocumentReader()); $doc = $builder->build($expr); $this->connection->storeQuery($this->index, $name, $doc); }