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 CONTENT:
134            case SUB_CONTENT:
135            case USER:
136                return fullText ? ("_txt_stemmed_" + language) : "_s";
137            case RICH_TEXT:
138                return "_txt_stemmed_" + language;
139            case LONG:
140                return "_l";
141            case DOUBLE:
142                return "_d";
143            case BOOLEAN:
144                return "_b";
145            case DATE:
146            case DATETIME:
147                return "_dt";
148            case GEOCODE:
149                return "_geo";
150            case REFERENCE:
151                // TODO reference -> to be indexed?
152            default:
153                // TODO
154                break;
155        }
156        
157        return "";
158    }
159    
160    /**
161     * Get the Solr field name for sorting
162     * @param type The type
163     * @param metadataPath The path to metadata
164     * @return the Solr field name
165     */
166    public static String getMetadataSortFieldName(MetadataType type, String metadataPath)
167    {
168        return metadataPath + getSortFieldSuffix(type);
169    }
170    
171    
172    /**
173     * Get the suffix for sorting
174     * @param metaType The type
175     * @return The sort field suffix.
176     */
177    public static String getSortFieldSuffix(MetadataType metaType)
178    {
179        switch (metaType)
180        {
181            case STRING:
182            case CONTENT:
183            case SUB_CONTENT:
184            case USER:
185                return "_s_sort";
186            case LONG:
187                return "_l_sort";
188            case DOUBLE:
189                return "_d_sort";
190            case BOOLEAN:
191                return "_b_sort";
192            case DATE:
193            case DATETIME:
194                return "_dt_sort";
195            case GEOCODE:
196                // TODO?
197                break;
198            case REFERENCE:
199                // TODO reference ?
200                break;
201            default:
202                // TODO
203                break;
204        }
205        
206        return "";
207    }
208    
209    /**
210     * Get the field name for join
211     * @param metadataPath The metadata path, separated by '/'
212     * @return the join field name
213     */
214    public static String getJoinFieldName(String metadataPath)
215    {
216        return metadataPath;
217    }
218    
219    /**
220     * Get the sort value for a string.
221     * @param string the string to index in a sort field.
222     * @return the sort value.
223     */
224    public static String getSortValue(String string)
225    {
226        if (string != null)
227        {
228            int endIndex = Math.min(string.length(), __SORT_MAX_CHARS);
229            return string.substring(0, endIndex);
230        }
231        else
232        {
233            return "";
234        }
235    }
236    
237}