001/*
002 *  Copyright 2014 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 org.ametys.cms.content.indexing.solr.SolrFieldNames;
019import org.ametys.cms.search.QueryBuilder;
020
021/**
022 * A {@link Query} realizing a full-text search on contents (search on all properties).
023 * Available operators:<ul>
024 * <li>SEARCH (default)</li>
025 * <li>SEARCH_STEMMED</li>
026 * <li>LIKE</li>
027 * <li>EQ</li>
028 * </ul>
029 */
030public class FullTextQuery implements Query
031{
032    
033    /** The value to test. */
034    protected String _value;
035    /** The language. */
036    protected String _language;
037    /** The operator. */
038    protected Operator _operator;
039    
040    /**
041     * Build a FullTextQuery.
042     * @param value the value.
043     */
044    public FullTextQuery(String value)
045    {
046        this(value, Operator.SEARCH);
047    }
048    
049    /**
050     * Build a FullTextQuery.
051     * @param value the value.
052     * @param operator The operator to use.
053     */
054    public FullTextQuery(String value, Operator operator)
055    {
056        this(value, null, operator);
057    }
058    
059    /**
060     * Build a FullTextQuery.
061     * @param value the value.
062     * @param language the language.
063     */
064    public FullTextQuery(String value, String language)
065    {
066        this(value, language, Operator.SEARCH);
067    }
068    
069    /**
070     * Build a FullTextQuery.
071     * @param value the value.
072     * @param language the language.
073     * @param operator The operator to use.
074     */
075    public FullTextQuery(String value, String language, Operator operator)
076    {
077        _value = value;
078        _language = language;
079        _operator = operator;
080    }
081    
082    /**
083     * Get the value.
084     * @return the value.
085     */
086    public String getValue()
087    {
088        return _value;
089    }
090    
091    /**
092     * Get the language.
093     * @return the language.
094     */
095    public String getLanguage()
096    {
097        return _language;
098    }
099    
100    /**
101     * Get the operator to use.
102     * @return the operator.
103     */
104    public Operator getOperator()
105    {
106        return _operator;
107    }
108    
109    @Override
110    public String build() throws QuerySyntaxException
111    {
112        // TODO params?
113        String language = _language != null ? _language : QueryBuilder.DEFAULT_LANGUAGE;
114        
115        StringQuery.checkStringValue(_value);
116        
117        String escapedValue = StringQuery.escapeStringValue(_value);
118        
119        StringBuilder solrQuery = new StringBuilder();
120        
121        switch (_operator)
122        {
123            case EQ:
124                solrQuery.append(SolrFieldNames.FULL_EXACT_WS)
125                         .append(":\"").append(escapedValue).append('"');
126                break;
127            case LIKE:
128                solrQuery.append(SolrFieldNames.FULL_GENERAL)
129                         .append(":(").append(escapedValue).append(')');
130                break;
131            case SEARCH_STEMMED:
132                solrQuery.append(SolrFieldNames.FULL_STEMMED_PREFIX).append(language)
133                         .append(":(").append(escapedValue).append(')');
134                break;
135            case SEARCH:
136            default:
137                solrQuery.append(SolrFieldNames.FULL_PREFIX).append(language)
138                         .append(":(").append(escapedValue).append(')');
139                break;
140        }
141        
142        return solrQuery.toString();
143    }
144    
145}