basemkhirat/elasticsearch

model extension for supporting highlight

Opened this issue · 0 comments

Hi,

This is an improvement feature i find useful in case of needing highlight support for defined models. Do you plan to add such capability in the future?

For example a starting point could be to add in the Query->getAll/getFirst methods a mapping logic for the $row["highlight"] to some special attribute of the model (could be something like $model->highlight)
Example:


--added in Query->getAll method
    //highlight extension support in model
    $model->setHighlight($row["highlight"] ?? null);

--added in Query->getFirst method
    //highlight extension support in model
    $model->setHighlight($data[0]["highlight"] ?? null);

--added to Model
    protected $_highlight;
    public function setHighlight($highlight){
        $this->highlight = $highlight;
    } 
    
    public function hasHighlightAttribute($name)
    {
        return $this->highlight && array_key_exists($name, $this->highlight);
    }

    public function getHighlightAttribute($name, $position = null)
    {
        if ($this->hasHighlightAttribute($name)) {
            if (!is_null($position)) {
                return $this->highlight[$name][$position];
            }
            return $this->highlight[$name];
        }
        return null;
    }

This can be very usefull in cases you set using the model->Body method the highlight raw request (or later on you can add dedicated method for instructing the EL to return highlights) and using the new mapping (above) the highlight can be accessible via a model specific getter method.

Still the highlight internal attribute must be removed from serialization of the model when saving:

--added in Model->save method
$fields = array_except($this->attributes, ["_index", "_type", "_id", "_score", "highlight"]);

Thx,
Lucian