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.Objects; 019 020import org.apache.commons.lang3.StringUtils; 021 022/** 023 * Wraps another {@link Query}, but giving to each matching document a nil score (score with a value of 0). 024 * <br>Thus, it will act as in a fq (filter query), and the given query will be cached in the filter cache. 025 */ 026public class ConstantNilScoreQuery implements Query 027{ 028 private Query _query; 029 030 /** 031 * Build a ConstantNilScoreQuery object. 032 * @param query The wrapped query 033 */ 034 public ConstantNilScoreQuery(Query query) 035 { 036 _query = query; 037 } 038 039 @Override 040 public String build() throws QuerySyntaxException 041 { 042 String clause = _query.build(); 043 StringBuilder sb = new StringBuilder() 044 .append("filter(") 045 .append(clause) 046 .append(")"); 047 return sb.toString(); 048 } 049 050 @Override 051 public String toString(int indent) 052 { 053 final String thisLineIndent = StringUtils.repeat(' ', indent); 054 final int subIndent = indent + 2; 055 final String subLineIndent = StringUtils.repeat(' ', subIndent); 056 final String q = subLineIndent + "[Q]\n" + _query.toString(subIndent + 2) + "\n" + subLineIndent + "[/Q]"; 057 return thisLineIndent + "[CONSTANT_NIL_SCORE]\n" + q + "\n" + thisLineIndent + "[/CONSTANT_NIL_SCORE]"; 058 } 059 060 @Override 061 public int hashCode() 062 { 063 return Objects.hash(_query); 064 } 065 066 @Override 067 public boolean equals(Object obj) 068 { 069 if (this == obj) 070 { 071 return true; 072 } 073 if (obj == null) 074 { 075 return false; 076 } 077 if (getClass() != obj.getClass()) 078 { 079 return false; 080 } 081 ConstantNilScoreQuery other = (ConstantNilScoreQuery) obj; 082 return Objects.equals(_query, other._query); 083 } 084} 085