示例#1
0
 public function testAddFieldMethodChaining()
 {
     $document = new Document();
     $this->assertTrue($document->addField(Document\Field::Text('title', 'Title')) instanceof Document);
     $document = new Document();
     $document->addField(Document\Field::Text('title', 'Title'))->addField(Document\Field::Text('annotation', 'Annotation'))->addField(Document\Field::Text('body', 'Document body, document body, document body...'));
 }
示例#2
0
    /**
     * Export an object recursivly, using associated metadata
     *
     * @param ClassMetadata $metadata
     * @param stdClass $object
     * @return array object
     */
    protected function exportObject(ClassMetadata $metadata, $object)
    {
        $document = new Document();

        $idGetter = 'get' . self::camelize($metadata->getIndex()->getIdProperty());

        $document->addField(Field::unIndexed('_id', $object->$idGetter()));

        foreach ($metadata->getFields() as $property => $field) {

            $getter = 'get' . self::camelize($property);

            $value = $object->$getter();

            if ($value && 'date' === $field->getType()) {
                $value = $value->format($field->getFormat());
            }

            $type = $field->getType();

            $encoding = mb_detect_encoding($value);

            $isStored = $field->getStore() === 'yes';

            $isIndexed = $field->getIndex() != "no";

            $isTokenized = $field->getIndex() == "analyzed";

            $field = new Field($field->getIndexName(), $value, $encoding, $isStored, $isIndexed, $isTokenized);

            $document->addField($field);

            $doc[$property] = $value;
        }

        $document->addField(Field::unIndexed('_doc', json_encode($doc)));

        return $document;
    }
 private function buildNews(SearchIndexInterface $index)
 {
     $repository = $this->get('universibo_legacy.repository.news.newsitem');
     foreach ($repository->findAll() as $news) {
         $doc = new Document();
         $doc->addField(Field::unStored('title', $news->getTitolo()));
         $doc->addField(Field::unStored('username', $news->getUsername()));
         $doc->addField(Field::unStored('content', $news->getNotizia()));
         $doc->addField(Field::unIndexed('dbId', $news->getIdNotizia()));
         $doc->addField(Field::unIndexed('type', 'news'));
         $index->addDocument($doc);
     }
 }
示例#4
0
文件: IndexTest.php 项目: rikaix/zf2
 /**
  * @group ZF-9680
  */
 public function testIsDeletedWithoutExplicitCommit()
 {
     $index = Lucene\Lucene::create(__DIR__ . '/_index/_files');
     $document = new Document();
     $document->addField(Document\Field::Keyword('_id', 'myId'));
     $document->addField(Document\Field::Keyword('bla', 'blubb'));
     $index->addDocument($document);
     $this->assertFalse($index->isDeleted(0));
 }
示例#5
0
 /**
  * Adds a document to this segment.
  *
  * @param \Zend\Search\Lucene\Document $document
  * @throws LuceneException\UnsupportedMethodCallException
  */
 public function addDocument(Document $document)
 {
     $storedFields = array();
     $docNorms = array();
     $similarity = AbstractSimilarity::getDefault();
     foreach ($document->getFieldNames() as $fieldName) {
         $field = $document->getField($fieldName);
         if ($field->storeTermVector) {
             /**
              * @todo term vector storing support
              */
             throw new LuceneException\UnsupportedMethodCallException('Store term vector functionality is not supported yet.');
         }
         if ($field->isIndexed) {
             if ($field->isTokenized) {
                 $analyzer = Analyzer\Analyzer::getDefault();
                 $analyzer->setInput($field->value, $field->encoding);
                 $position = 0;
                 $tokenCounter = 0;
                 while (($token = $analyzer->nextToken()) !== null) {
                     $tokenCounter++;
                     $term = new Index\Term($token->getTermText(), $field->name);
                     $termKey = $term->key();
                     if (!isset($this->_termDictionary[$termKey])) {
                         // New term
                         $this->_termDictionary[$termKey] = $term;
                         $this->_termDocs[$termKey] = array();
                         $this->_termDocs[$termKey][$this->_docCount] = array();
                     } else {
                         if (!isset($this->_termDocs[$termKey][$this->_docCount])) {
                             // Existing term, but new term entry
                             $this->_termDocs[$termKey][$this->_docCount] = array();
                         }
                     }
                     $position += $token->getPositionIncrement();
                     $this->_termDocs[$termKey][$this->_docCount][] = $position;
                 }
                 if ($tokenCounter == 0) {
                     // Field contains empty value. Treat it as non-indexed and non-tokenized
                     $field = clone $field;
                     $field->isIndexed = $field->isTokenized = false;
                 } else {
                     $docNorms[$field->name] = chr($similarity->encodeNorm($similarity->lengthNorm($field->name, $tokenCounter) * $document->boost * $field->boost));
                 }
             } else {
                 if (($fieldUtf8Value = $field->getUtf8Value()) == '') {
                     // Field contains empty value. Treat it as non-indexed and non-tokenized
                     $field = clone $field;
                     $field->isIndexed = $field->isTokenized = false;
                 } else {
                     $term = new Index\Term($fieldUtf8Value, $field->name);
                     $termKey = $term->key();
                     if (!isset($this->_termDictionary[$termKey])) {
                         // New term
                         $this->_termDictionary[$termKey] = $term;
                         $this->_termDocs[$termKey] = array();
                         $this->_termDocs[$termKey][$this->_docCount] = array();
                     } else {
                         if (!isset($this->_termDocs[$termKey][$this->_docCount])) {
                             // Existing term, but new term entry
                             $this->_termDocs[$termKey][$this->_docCount] = array();
                         }
                     }
                     $this->_termDocs[$termKey][$this->_docCount][] = 0;
                     // position
                     $docNorms[$field->name] = chr($similarity->encodeNorm($similarity->lengthNorm($field->name, 1) * $document->boost * $field->boost));
                 }
             }
         }
         if ($field->isStored) {
             $storedFields[] = $field;
         }
         $this->addField($field);
     }
     foreach ($this->_fields as $fieldName => $field) {
         if (!$field->isIndexed) {
             continue;
         }
         if (!isset($this->_norms[$fieldName])) {
             $this->_norms[$fieldName] = str_repeat(chr($similarity->encodeNorm($similarity->lengthNorm($fieldName, 0))), $this->_docCount);
         }
         if (isset($docNorms[$fieldName])) {
             $this->_norms[$fieldName] .= $docNorms[$fieldName];
         } else {
             $this->_norms[$fieldName] .= chr($similarity->encodeNorm($similarity->lengthNorm($fieldName, 0)));
         }
     }
     $this->addStoredFields($storedFields);
 }
示例#6
0
文件: Index.php 项目: nresni/Ariadne
    /**
     * Returns a Zend_Search_Lucene_Document object for the document
     * number $id in this index.
     *
     * @param integer|\Zend\Search\Lucene\Search\QueryHit $id
     * @return \Zend\Search\Lucene\Document
     * @throws \Zend\Search\Lucene\OutOfRangeException    is thrown if $id is out of the range
     */
    public function getDocument($id)
    {
        if ($id instanceof Search\QueryHit) {
            /* @var $id Zend\Search\Lucene\Search\QueryHit */
            $id = $id->id;
        }

        if ($id >= $this->_docCount) {
            throw new OutOfRangeException('Document id is out of the range.');
        }

        $segmentStartId = 0;
        foreach ($this->_segmentInfos as $segmentInfo) {
            if ($segmentStartId + $segmentInfo->count() > $id) {
                break;
            }

            $segmentStartId += $segmentInfo->count();
        }

        $fdxFile = $segmentInfo->openCompoundFile('.fdx');
        $fdxFile->seek(($id-$segmentStartId)*8, SEEK_CUR);
        $fieldValuesPosition = $fdxFile->readLong();

        $fdtFile = $segmentInfo->openCompoundFile('.fdt');
        $fdtFile->seek($fieldValuesPosition, SEEK_CUR);
        $fieldCount = $fdtFile->readVInt();

        $doc = new Document();
        for ($count = 0; $count < $fieldCount; $count++) {
            $fieldNum = $fdtFile->readVInt();
            $bits = $fdtFile->readByte();

            $fieldInfo = $segmentInfo->getField($fieldNum);

            if (!($bits & 2)) { // Text data
                $field = new Document\Field($fieldInfo->name,
                                            $fdtFile->readString(),
                                            'UTF-8',
                                            true,
                                            $fieldInfo->isIndexed,
                                            $bits & 1 );
            } else {            // Binary data
                $field = new Document\Field($fieldInfo->name,
                                            $fdtFile->readBinary(),
                                            '',
                                            true,
                                            $fieldInfo->isIndexed,
                                            $bits & 1,
                                            true );
            }

            $doc->addField($field);
        }

        return $doc;
    }