diff src/goodjava/lucene/queryparser/SynonymParser.java @ 1458:6b6c11c9164e

goodjava.lucene
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 20 Mar 2020 10:58:53 -0600
parents src/goodjava/queryparser/SynonymParser.java@27efb1fcbcb5
children b04b8fc5f4f4
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/goodjava/lucene/queryparser/SynonymParser.java	Fri Mar 20 10:58:53 2020 -0600
@@ -0,0 +1,43 @@
+package goodjava.lucene.queryparser;
+
+import java.util.Map;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.BooleanQuery;
+import org.apache.lucene.search.BooleanClause;
+import org.apache.lucene.search.SortField;
+import goodjava.parser.ParseException;
+
+
+public class SynonymParser implements FieldParser {
+	private final FieldParser fp;
+	private final Map<String,String[]> synonymMap;
+
+	public SynonymParser(FieldParser fp,Map<String,String[]> synonymMap) {
+		this.fp = fp;
+		this.synonymMap = synonymMap;
+	}
+
+	protected String[] getSynonyms(String query) {
+		return synonymMap.get(query);
+	}
+
+	public Query getQuery(SaneQueryParser qp,String field,String query) throws ParseException {
+		String[] synonyms = getSynonyms(query);
+		if( synonyms == null )
+			return fp.getQuery(qp,field,query);
+		BooleanQuery bq = new BooleanQuery();
+		bq.add( fp.getQuery(qp,field,query), BooleanClause.Occur.SHOULD );
+		for( String s : synonyms ) {
+			bq.add( fp.getQuery(qp,field,s), BooleanClause.Occur.SHOULD );
+		}
+		return bq;
+	}
+
+	public Query getRangeQuery(SaneQueryParser qp,String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax) throws ParseException {
+		return fp.getRangeQuery(qp,field,minQuery,maxQuery,includeMin,includeMax);
+	}
+
+	public SortField getSortField(SaneQueryParser qp,String field,boolean reverse) throws ParseException {
+		return fp.getSortField(qp,field,reverse);
+	}
+}