FileDocCategorySizeDatePackage
QueryTranslator.javaAPI DocApache Lucene 2.1.05470Wed Feb 14 10:46:06 GMT 2007org.apache.lucene.gdata.search.query

QueryTranslator

public class QueryTranslator extends Object
Simple static methods to translate the http query to a lucene query string.
author
Simon Willnauer

Fields Summary
private static final Set
STANDARD_REQUEST_PARAMETER
private static final String
GDATA_DEFAULT_SEARCH_PARAMETER
private static final String
UPDATED_MIN
private static final String
UPDATED_MAX
Constructors Summary
Methods Summary
static java.lang.StringtranslateCategory(java.lang.StringBuilder builder, java.lang.String categoryQuery)

         return categoryQuery;
         //TODO Implement this
//         GDataCategoryQueryParser parser = new GDataCategoryQueryParser()
         
        
     
public static java.lang.StringtranslateHttpSearchRequest(org.apache.lucene.gdata.search.config.IndexSchema schema, java.util.Map parameterMap, java.lang.String categoryQuery)
This method does a little preprocessing of the query. Basically it will map the given request parameters to a lucene syntax. Each parameter matching a index field in the given schema will be translated into a grouped query string according to the lucene query syntax.

  1. title=foo bar AND "FooBar" will be title:(foo bar AND "FooBar)
  2. updated-min=2005-08-09T10:57:00-08:00 will be translated to updated:[1123613820000 TO 9223372036854775807] according to the gdata protocol

param
schema the index schema for the queried service
param
parameterMap - the http parameter map returning String[] instances as values
param
categoryQuery - the parsed category query from the request
return
- a lucene syntax query string

//    private static final String GDATA_CATEGORY_FIEL = 

    
    
    
        STANDARD_REQUEST_PARAMETER.add("max-results");
        STANDARD_REQUEST_PARAMETER.add("start-index");
        STANDARD_REQUEST_PARAMETER.add("alt");
    
        Set<String> searchableFieldNames = schema.getSearchableFieldNames();
        Set<String> parameterSet = parameterMap.keySet();
        StringBuilder translatedQuery = new StringBuilder();
        if(categoryQuery != null){
           translatedQuery.append(translateCategory(translatedQuery,categoryQuery));
        }
        String updateMin = null;
        String updateMax = null;
        for (String parameterName : parameterSet) {
            if (STANDARD_REQUEST_PARAMETER.contains(parameterName))
                continue;
            if (searchableFieldNames.contains(parameterName)) {
                translatedQuery.append(parameterName).append(":(");
                translatedQuery.append(parameterMap.get(parameterName)[0]);
                translatedQuery.append(") ");
                continue;
            }
            if(parameterName.equals(GDATA_DEFAULT_SEARCH_PARAMETER)){
                translatedQuery.append(schema.getDefaultSearchField());
                translatedQuery.append(":(");
                translatedQuery.append(parameterMap.get(parameterName)[0]);
                translatedQuery.append(") ");
                continue;
                
            }
            if(parameterName.endsWith("updated-min")){
                updateMin = parameterMap.get(parameterName)[0];
                continue;
            }
            if(parameterName.endsWith("updated-max")){
                updateMax = parameterMap.get(parameterName)[0];
                continue;
            }
            throw new RuntimeException("Can not apply parameter -- invalid -- "
                    + parameterName);
        }
        if(updateMax!=null || updateMin!= null)
            translatedQuery.append(translateUpdate(updateMin,updateMax));
            
        return translatedQuery.length() == 0?null:translatedQuery.toString();
    
static java.lang.StringtranslateUpdate(java.lang.String updateMin, java.lang.String updateMax)

        StringBuilder builder = new StringBuilder("updated:[");
        if(updateMin != null)
            builder.append(Long.toString(DateTime.parseDateTime(updateMin).getValue()));
        else
            builder.append(UPDATED_MIN);
        builder.append(" TO ");
        if(updateMax != null)
            builder.append(Long.toString(DateTime.parseDateTime(updateMax).getValue()-1));
        else
            builder.append(UPDATED_MAX);
        builder.append("]");
        return builder.toString();