001/*
002 *  Copyright 2015 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.content.indexing.solr;
017
018import org.ametys.cms.contenttype.MetadataType;
019
020/**
021 * Helper for getting Solr field names.
022 */
023public final class SolrFieldHelper
024{
025    
026//    private static final int __SORT_MAX_CHARS = 50;
027    
028    private SolrFieldHelper()
029    {
030        // Hide the default constructor.
031    }
032    
033    /**
034     * Get the Solr field name (without analyzing)
035     * @param fieldPath The path to metadata
036     * @param language The query language.
037     * @param fullText <code>true</code> for a full-text query, false otherwise.
038     * @return the Solr field name
039     */
040    public static String getStringIndexingFieldName(String fieldPath, String language, boolean fullText)
041    {
042        return getIndexingFieldName(MetadataType.STRING, fieldPath, language, fullText);
043    }
044    
045    /**
046     * Get the Solr field name (without analyzing)
047     * @param type The type
048     * @param fieldPath The path to metadata
049     * @return the Solr field name
050     */
051    public static String getIndexingFieldName(MetadataType type, String fieldPath)
052    {
053        return getIndexingFieldName(type, fieldPath, null, false);
054    }
055    
056    /**
057     * Get the Solr field's name (without analyzing)
058     * @param type The type
059     * @param fieldPath The path to metadata
060     * @param language The query language.
061     * @return the Solr field's name
062     */
063    public static String getIndexingFieldName(MetadataType type, String fieldPath, String language)
064    {
065        return getIndexingFieldName(type, fieldPath, language, false);
066    }
067    
068    /**
069     * Get the Solr field name
070     * @param type The type
071     * @param fieldPath The path to metadata
072     * @param language The query language.
073     * @param fullText true if field should be analyzed.
074     * @return the Solr field name
075     */
076    public static String getIndexingFieldName(MetadataType type, String fieldPath, String language, boolean fullText)
077    {
078        return fieldPath + getFieldSuffix(type, language, fullText);
079    }
080    
081    /**
082     * Get a field suffix from its type.
083     * @param language The query language.
084     * @param fullText <code>true</code> for a full-text query, false otherwise.
085     * @return the field name suffix.
086     */
087    public static String getStringFieldSuffix(String language, boolean fullText)
088    {
089        return getFieldSuffix(MetadataType.STRING, language, fullText);
090    }
091    
092    /**
093     * Get the wildcard field suffix
094     * @return the field name suffix.
095     */
096    public static String getWildcardFieldSuffix()
097    {
098        return "_s_lower";
099    }
100    
101    /**
102     * Get the wildcard field suffix
103     * @param language The query language.
104     * @return the field name suffix.
105     */
106    public static String getFulltextFieldSuffix(String language)
107    {
108        return "_txt_" + language;
109    }
110    
111    /**
112     * Get a field suffix from its type.
113     * @param type the field type.
114     * @return the field name suffix.
115     */
116    public static String getFieldSuffix(MetadataType type)
117    {
118        return getFieldSuffix(type, null, false);
119    }
120    
121    /**
122     * Get a field suffix from its type.
123     * @param type the field type.
124     * @param language The search language.
125     * @param fullText true if the field is indexed full-text, false otherwise.
126     * @return the field name suffix.
127     */
128    public static String getFieldSuffix(MetadataType type, String language, boolean fullText)
129    {
130        switch (type)
131        {
132            case STRING:
133            case MULTILINGUAL_STRING:
134            case CONTENT:
135            case SUB_CONTENT:
136            case USER:
137                return fullText ? ("_txt_stemmed_" + language) : "_s";
138            case RICH_TEXT:
139                return "_txt_stemmed_" + language;
140            case LONG:
141                return "_l";
142            case DOUBLE:
143                return "_d";
144            case BOOLEAN:
145                return "_b";
146            case DATE:
147            case DATETIME:
148                return "_dt";
149            case GEOCODE:
150                return "_geo";
151            case REFERENCE:
152                // TODO reference -> to be indexed? https://issues.ametys.org/browse/CMS-8623
153            default:
154                // TODO
155                break;
156        }
157        
158        return "";
159    }
160    
161    /**
162     * Get the Solr field name for sorting
163     * @param type The type
164     * @param metadataPath The path to metadata
165     * @return the Solr field name
166     */
167    public static String getMetadataSortFieldName(MetadataType type, String metadataPath)
168    {
169        return metadataPath + getSortFieldSuffix(type);
170    }
171    
172    
173    /**
174     * Get the suffix for sorting
175     * @param metaType The type
176     * @return The sort field suffix.
177     */
178    public static String getSortFieldSuffix(MetadataType metaType)
179    {
180        switch (metaType)
181        {
182            case STRING:
183            case MULTILINGUAL_STRING:
184            case CONTENT:
185            case SUB_CONTENT:
186            case USER:
187                return "_s_sort";
188            case LONG:
189                return "_l_sort";
190            case DOUBLE:
191                return "_d_sort";
192            case BOOLEAN:
193                return "_b_sort";
194            case DATE:
195            case DATETIME:
196                return "_dt_sort";
197            case GEOCODE:
198                // TODO?
199                break;
200            case REFERENCE:
201                // TODO reference ?
202                break;
203            default:
204                // TODO
205                break;
206        }
207        
208        return "";
209    }
210    
211    /**
212     * Get the field name for join
213     * @param metadataPath The metadata path, separated by '/'
214     * @return the join field name
215     */
216    public static String getJoinFieldName(String metadataPath)
217    {
218        return metadataPath;
219    }
220    
221    /**
222     * Get the sort value for a string.
223     * @param string the string to index in a sort field.
224     * @return the sort value.
225     */
226    public static String getSortValue(String string)
227    {
228        return string == null ? "" : string;
229//        if (string != null)
230//        {
231//            int endIndex = Math.min(string.length(), __SORT_MAX_CHARS);
232//            return string.substring(0, endIndex);
233//        }
234//        else
235//        {
236//            return "";
237//        }
238    }
239    
240}