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