001/*
002 *  Copyright 2016 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.cms.search.solr;
017
018import org.apache.solr.common.SolrDocument;
019
020import org.ametys.cms.content.indexing.solr.SolrFieldNames;
021import org.ametys.cms.search.SearchResult;
022import org.ametys.plugins.repository.AmetysObject;
023import org.ametys.plugins.repository.AmetysObjectResolver;
024
025class SolrDocumentResult<AO extends AmetysObject> implements SearchResult<AO>
026{
027    private SolrDocument _document;
028    private AmetysObjectResolver _resolver;
029    
030    public SolrDocumentResult(SolrDocument document, AmetysObjectResolver resolver)
031    {
032        _document = document;
033        _resolver = resolver;
034    }
035    
036    @Override
037    public AO getObject()
038    {
039        String id = (String) _document.getFieldValue(SolrFieldNames.ID);
040        return _resolver.resolveById(id);
041    }
042    
043    @Override
044    public float getScore()
045    {
046        Float score = (Float) _document.getFieldValue("score");
047        return score != null ? score.floatValue() : 0;
048    }
049}