comparison src/goodjava/queryparser/SynonymParser.java @ 1402:27efb1fcbcb5

move luan.lib to goodjava
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 17 Sep 2019 01:35:01 -0400
parents src/luan/lib/queryparser/SynonymParser.java@dc2af9d5463b
children
comparison
equal deleted inserted replaced
1401:ef1620aa99cb 1402:27efb1fcbcb5
1 package goodjava.queryparser;
2
3 import java.util.Map;
4 import org.apache.lucene.search.Query;
5 import org.apache.lucene.search.BooleanQuery;
6 import org.apache.lucene.search.BooleanClause;
7 import org.apache.lucene.search.SortField;
8 import goodjava.parser.ParseException;
9
10
11 public class SynonymParser implements FieldParser {
12 private final FieldParser fp;
13 private final Map<String,String[]> synonymMap;
14
15 public SynonymParser(FieldParser fp,Map<String,String[]> synonymMap) {
16 this.fp = fp;
17 this.synonymMap = synonymMap;
18 }
19
20 protected String[] getSynonyms(String query) {
21 return synonymMap.get(query);
22 }
23
24 public Query getQuery(SaneQueryParser qp,String field,String query) throws ParseException {
25 String[] synonyms = getSynonyms(query);
26 if( synonyms == null )
27 return fp.getQuery(qp,field,query);
28 BooleanQuery bq = new BooleanQuery();
29 bq.add( fp.getQuery(qp,field,query), BooleanClause.Occur.SHOULD );
30 for( String s : synonyms ) {
31 bq.add( fp.getQuery(qp,field,s), BooleanClause.Occur.SHOULD );
32 }
33 return bq;
34 }
35
36 public Query getRangeQuery(SaneQueryParser qp,String field,String minQuery,String maxQuery,boolean includeMin,boolean includeMax) throws ParseException {
37 return fp.getRangeQuery(qp,field,minQuery,maxQuery,includeMin,includeMax);
38 }
39
40 public SortField getSortField(SaneQueryParser qp,String field,boolean reverse) throws ParseException {
41 return fp.getSortField(qp,field,reverse);
42 }
43 }