|
| 1 | +Attachments Handling |
| 2 | +======================= |
| 3 | + |
| 4 | +This is just a quick overview of how to handle attachment mappings, searching, and highlighting. |
| 5 | + |
| 6 | +Mappings |
| 7 | +----------------------------- |
| 8 | + |
| 9 | +You can set up the FOSElasticaBundle to use attachments in the mappings. An overview of the ElasticSearch attachment plugin |
| 10 | +can be viewed here - https://github.com/elastic/elasticsearch-mapper-attachments. Installation instructions can be found |
| 11 | +on the github page. If you want to highlight content from the document, then you will need to add `"store": true` AND |
| 12 | +`"term_vector":"with_positions_offsets"` to the attachment field. |
| 13 | + |
| 14 | +> *Note*: Metadata mappings can be added as needed to the mappings file for attachments. More information can be seen |
| 15 | +> at the Github page for Elasicsearch mapper attachments. |
| 16 | +
|
| 17 | + |
| 18 | +```yaml |
| 19 | +fos_elastica: |
| 20 | + indexes: |
| 21 | + app: |
| 22 | + types: |
| 23 | + user: |
| 24 | + mappings: |
| 25 | + id: ~ |
| 26 | + content: |
| 27 | + type: attachment |
| 28 | + path: full |
| 29 | + fields: |
| 30 | + name: { store: yes } |
| 31 | + title: { store : yes } |
| 32 | + date: { store : yes } |
| 33 | + content : { term_vector: with_positions_offsets, store: yes } |
| 34 | + ... |
| 35 | +``` |
| 36 | + |
| 37 | + |
| 38 | +Attachment Searching |
| 39 | +----------------------------- |
| 40 | + |
| 41 | +Here is an example query that can be ran on attachments. This also includes an example of how to use the highlights functionality |
| 42 | + for attachments. |
| 43 | + |
| 44 | +```php |
| 45 | +$keywordQuery = new QueryString(); |
| 46 | +$keywordQuery->addParam(NULL, array('fuzziness' => 1)); |
| 47 | +$keywordQuery->setQuery("$term~"); |
| 48 | +$keywordQuery->setDefaultOperator('AND'); |
| 49 | + |
| 50 | +$query = new Query($keywordQuery); |
| 51 | +$query->setFields(array("id", "...")); |
| 52 | +$query->setHighlight(array( |
| 53 | + 'fields' => array( |
| 54 | + 'content' => new \stdClass() |
| 55 | + ) |
| 56 | +)); |
| 57 | +``` |
| 58 | + |
| 59 | +Converting Attachments |
| 60 | +----------------------------- |
| 61 | + |
| 62 | +This is an example of indexing documents in the required base64 encoding. You will need to specify the upload directory of all |
| 63 | + the attachments under the method getUploadDir(). The method getContent() contains the functionality to convert the file to |
| 64 | + base64. |
| 65 | + |
| 66 | +```php |
| 67 | +public function getContent() |
| 68 | +{ |
| 69 | + //Upload directory set at /web/uploads/library |
| 70 | + return base64_encode(file_get_contents($this->getUploadRootDir() . '/' . $this->filename, 'r')); |
| 71 | +} |
| 72 | + |
| 73 | +protected function getUploadRootDir() |
| 74 | +{ |
| 75 | + // the absolute directory path where uploaded |
| 76 | + // documents should be saved |
| 77 | + return __DIR__.'/../../../../web/'.$this->getUploadDir(); |
| 78 | +} |
| 79 | + |
| 80 | +protected function getUploadDir() |
| 81 | +{ |
| 82 | + // get rid of the __DIR__ so it doesn't screw up |
| 83 | + // when displaying uploaded doc/image in the view. |
| 84 | + return 'uploads/library'; |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +Handling Highlights |
| 89 | +----------------------------- |
| 90 | + |
| 91 | +If you want to grab highlights from your search query, it can be achieved by implementing the HighlightableModelInterface. |
| 92 | +The interface requires the getId() and the setElasticHighlights() method. You will also need the getElasticHighlights |
| 93 | +methods to view the output. An example entity is displayed below. |
| 94 | + |
| 95 | +```php |
| 96 | +use FOS\ElasticaBundle\Transformer\HighlightableModelInterface; |
| 97 | + |
| 98 | +class Library implements HighlightableModelInterface { |
| 99 | + |
| 100 | + private $id |
| 101 | + |
| 102 | + private $highlights; |
| 103 | + |
| 104 | + //Needs this method for HighlightableModelInterface |
| 105 | + public function getId() |
| 106 | + { |
| 107 | + return $this->id; |
| 108 | + } |
| 109 | + |
| 110 | + //Needs this method for HighlightableModelInterface |
| 111 | + public function setElasticHighlights(array $highlights) |
| 112 | + { |
| 113 | + $this->highlights = $highlights; |
| 114 | + |
| 115 | + return $this; |
| 116 | + } |
| 117 | + |
| 118 | + public function getElasticHighlights() |
| 119 | + { |
| 120 | + return $this->highlights; |
| 121 | + } |
| 122 | + |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +Viewing Highlights in Twig |
| 127 | +----------------------------- |
| 128 | + |
| 129 | +This is just a quick reference to obtaining the highlighted text returned by the query in a TWIG file. |
| 130 | + |
| 131 | +```php |
| 132 | +{% for highlights in reference.ElasticHighlights %} |
| 133 | + {% for highlight in highlights %} |
| 134 | + <tr class="alert alert-info"> |
| 135 | + <td></td> |
| 136 | + <td></td> |
| 137 | + <td>{{ highlight|raw }}</td> |
| 138 | + </tr> |
| 139 | + {% endfor %} |
| 140 | +{% endfor %} |
| 141 | +``` |
0 commit comments