001/*
002 *  Copyright 2019 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.query;
017
018import java.util.Map;
019import java.util.Objects;
020import java.util.Optional;
021
022import org.apache.commons.lang3.StringUtils;
023import org.apache.solr.client.solrj.util.ClientUtils;
024
025/**
026 * Wraps another {@link Query}, but giving to each matching document a boosted score (scores are multiplied). 
027 */
028public class BoostedQuery implements Query
029{
030    private Query _query;
031    private float _boost;
032
033    /**
034     * Build a BoostedQuery object.
035     * @param query The wrapped query
036     * @param boost The boost
037     */
038    public BoostedQuery(Query query, float boost)
039    {
040        _query = query;
041        _boost = boost;
042    }
043    
044    @Override
045    public String build() throws QuerySyntaxException
046    {
047        StringBuilder sb = new StringBuilder()
048                .append("{!boost b=")
049                .append(_boost)
050                .append(" v=\"")
051                .append(ClientUtils.escapeQueryChars(_query.build()))
052                .append("\"}");
053        return sb.toString();
054    }
055    
056    public Optional<Object> buildAsJson() throws QuerySyntaxException
057    {
058        return _query.buildAsJson()
059                .map(q -> Map.of("boost", Map.of("query", q, "b", _boost)));
060    }
061    
062    @Override
063    public String toString(int indent)
064    {
065        final String thisLineIndent = StringUtils.repeat(' ', indent);
066        final int subIndent = indent + 2;
067        final String subLineIndent = StringUtils.repeat(' ', subIndent);
068        final String boost = subLineIndent + "[BOOST]" + _boost + "[/BOOST]";
069        final String q = subLineIndent + "[Q]\n" + _query.toString(subIndent + 2) + "\n" + subLineIndent + "[/Q]";
070        return thisLineIndent + "[BOOSTED]\n" + boost + "\n" + q + "\n" + thisLineIndent + "[/BOOSTED]";
071    }
072
073    @Override
074    public int hashCode()
075    {
076        return Objects.hash(_boost, _query);
077    }
078
079    @Override
080    public boolean equals(Object obj)
081    {
082        if (this == obj)
083        {
084            return true;
085        }
086        
087        if (obj == null || getClass() != obj.getClass())
088        {
089            return false;
090        }
091        
092        BoostedQuery other = (BoostedQuery) obj;
093        return Float.floatToIntBits(_boost) == Float.floatToIntBits(other._boost) && Objects.equals(_query, other._query);
094    }
095}