--- /dev/null
+package com.liferay.portal.kernel.search;
+
+import com.liferay.portal.kernel.util.StringPool;
+
+public class SolrQueryUtil {
+
+ public static final String AND = "AND";
+
+ public static final String OR = "OR";
+
+ /**
+ * Build a value query from a string
+ * Fuzzy search is an approximative search which is costly (performance killer) before SOLR 4.0 (example => keyword : bol, results : bol, bal, bob, sol...)
+ * @param keywords string that contains a list of keywords separated with spaces
+ * @param fuzzySearch boolean which activate fuzzysearch
+ * @return a query with the keywords
+ */
+ public static final String getQueryFromKeywords(String keywords, boolean fuzzySearch) {
+
+ StringBuilder query = new StringBuilder("");
+
+ if(!keywords.isEmpty()){
+
+ String[] words = keywords.split(" ");
+ query.append("( ");
+
+ for(int i = 0 ; i < words.length ; ++i){
+ String word = words[i];
+
+ query.append("(" + word + StringPool.STAR);
+
+ query.append(" " + SolrQueryUtil.OR + " ");
+ if (fuzzySearch) {
+ query.append(word + StringPool.TILDE + "0.8");
+ query.append(" " + SolrQueryUtil.OR + " ");
+ }
+
+ query.append(word + ")");
+
+ if (i < (words.length - 1)) {
+ query.append(" " + SolrQueryUtil.AND + " ");
+ }
+ }
+
+ query.append(" )");
+ }
+
+ return query.toString();
+ }
+
+}