SOLR-602 -- add method chaining to SolrQuery

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@671036 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Ryan McKinley 2008-06-24 06:06:07 +00:00
parent 0953aa3e6a
commit 0737e7469d
2 changed files with 73 additions and 37 deletions

View File

@ -55,12 +55,14 @@ public class SolrQuery extends ModifiableSolrParams
/** add a field for facet computation /** add a field for facet computation
* *
* @param fields the field name from the IndexSchema * @param fields the field name from the IndexSchema
* @return this
*/ */
public void addFacetField(String ... fields) { public SolrQuery addFacetField(String ... fields) {
for( String f : fields ) { for( String f : fields ) {
this.add(FacetParams.FACET_FIELD, f); this.add(FacetParams.FACET_FIELD, f);
} }
this.set(FacetParams.FACET, true); this.set(FacetParams.FACET, true);
return this;
} }
/** get the facet fields /** get the facet fields
@ -85,8 +87,9 @@ public class SolrQuery extends ModifiableSolrParams
/** enable/disable faceting. /** enable/disable faceting.
* *
* @param b flag to indicate faceting should be enabled. if b==false removes all other faceting parameters * @param b flag to indicate faceting should be enabled. if b==false removes all other faceting parameters
* @return this
*/ */
public void setFacet(boolean b) { public SolrQuery setFacet(boolean b) {
if (b) { if (b) {
this.set(FacetParams.FACET, true); this.set(FacetParams.FACET, true);
} else { } else {
@ -102,24 +105,28 @@ public class SolrQuery extends ModifiableSolrParams
this.remove(FacetParams.FACET_ZEROS); this.remove(FacetParams.FACET_ZEROS);
this.remove(FacetParams.FACET_PREFIX); // does not include the individual fields... this.remove(FacetParams.FACET_PREFIX); // does not include the individual fields...
} }
return this;
} }
public void setFacetPrefix( String prefix ) public SolrQuery setFacetPrefix( String prefix )
{ {
this.set( FacetParams.FACET_PREFIX, prefix ); this.set( FacetParams.FACET_PREFIX, prefix );
return this;
} }
public void setFacetPrefix( String field, String prefix ) public SolrQuery setFacetPrefix( String field, String prefix )
{ {
this.set( "f."+field+"."+FacetParams.FACET_PREFIX, prefix ); this.set( "f."+field+"."+FacetParams.FACET_PREFIX, prefix );
return this;
} }
/** add a faceting query /** add a faceting query
* *
* @param f facet query * @param f facet query
*/ */
public void addFacetQuery(String f) { public SolrQuery addFacetQuery(String f) {
this.add(FacetParams.FACET_QUERY, f); this.add(FacetParams.FACET_QUERY, f);
return this;
} }
/** get facet queries /** get facet queries
@ -147,8 +154,9 @@ public class SolrQuery extends ModifiableSolrParams
* *
* @param lim number facet items to return * @param lim number facet items to return
*/ */
public void setFacetLimit(int lim) { public SolrQuery setFacetLimit(int lim) {
this.set(FacetParams.FACET_LIMIT, lim); this.set(FacetParams.FACET_LIMIT, lim);
return this;
} }
/** get current facet limit /** get current facet limit
@ -163,8 +171,9 @@ public class SolrQuery extends ModifiableSolrParams
* *
* @param cnt facets having less that cnt hits will be excluded from teh facet list * @param cnt facets having less that cnt hits will be excluded from teh facet list
*/ */
public void setFacetMinCount(int cnt) { public SolrQuery setFacetMinCount(int cnt) {
this.set(FacetParams.FACET_MINCOUNT, cnt); this.set(FacetParams.FACET_MINCOUNT, cnt);
return this;
} }
/** get facet minimum count /** get facet minimum count
@ -175,8 +184,9 @@ public class SolrQuery extends ModifiableSolrParams
return this.getInt(FacetParams.FACET_LIMIT, 1); return this.getInt(FacetParams.FACET_LIMIT, 1);
} }
public void setMissing(String fld) { public SolrQuery setMissing(String fld) {
this.set(FacetParams.FACET_MISSING, fld); this.set(FacetParams.FACET_MISSING, fld);
return this;
} }
/** get facet sort /** get facet sort
@ -190,18 +200,21 @@ public class SolrQuery extends ModifiableSolrParams
/** set facet sort /** set facet sort
* *
* @param sort sort facets * @param sort sort facets
* @return this
*/ */
public void setFacetSort(Boolean sort) { public SolrQuery setFacetSort(Boolean sort) {
this.set(FacetParams.FACET_SORT, sort); this.set(FacetParams.FACET_SORT, sort);
return this;
} }
/** add highlight field /** add highlight field
* *
* @param f field to enable for highlighting * @param f field to enable for highlighting
*/ */
public void addHighlightField(String f) { public SolrQuery addHighlightField(String f) {
this.add(HighlightParams.FIELDS, f); this.add(HighlightParams.FIELDS, f);
this.set(HighlightParams.HIGHLIGHT, true); this.set(HighlightParams.HIGHLIGHT, true);
return this;
} }
/** remove a field for highlighting /** remove a field for highlighting
@ -225,56 +238,62 @@ public class SolrQuery extends ModifiableSolrParams
return this.getParams(HighlightParams.FIELDS); return this.getParams(HighlightParams.FIELDS);
} }
public void setHighlightSnippets(int num) { public SolrQuery setHighlightSnippets(int num) {
this.set(HighlightParams.SNIPPETS, num); this.set(HighlightParams.SNIPPETS, num);
return this;
} }
public int getHighlightSnippets() { public int getHighlightSnippets() {
return this.getInt(HighlightParams.SNIPPETS, 1); return this.getInt(HighlightParams.SNIPPETS, 1);
} }
public void setHighlightFragsize(int num) { public SolrQuery setHighlightFragsize(int num) {
this.set(HighlightParams.FRAGSIZE, num); this.set(HighlightParams.FRAGSIZE, num);
return this;
} }
public int getHighlightFragsize() { public int getHighlightFragsize() {
return this.getInt(HighlightParams.FRAGSIZE, 100); return this.getInt(HighlightParams.FRAGSIZE, 100);
} }
public void setHighlightRequireFieldMatch(boolean flag) { public SolrQuery setHighlightRequireFieldMatch(boolean flag) {
this.set(HighlightParams.FIELD_MATCH, flag); this.set(HighlightParams.FIELD_MATCH, flag);
return this;
} }
public boolean setHighlightRequireFieldMatch() { public boolean setHighlightRequireFieldMatch() {
return this.getBool(HighlightParams.FIELD_MATCH, false); return this.getBool(HighlightParams.FIELD_MATCH, false);
} }
public void setHighlightSimplePre(String f) { public SolrQuery setHighlightSimplePre(String f) {
this.set(HighlightParams.SIMPLE_PRE, f); this.set(HighlightParams.SIMPLE_PRE, f);
return this;
} }
public String getHighlightSimplePre() { public String getHighlightSimplePre() {
return this.get(HighlightParams.SIMPLE_PRE, ""); return this.get(HighlightParams.SIMPLE_PRE, "");
} }
public void setHighlightSimplePost(String f) { public SolrQuery setHighlightSimplePost(String f) {
this.set(HighlightParams.SIMPLE_POST, f); this.set(HighlightParams.SIMPLE_POST, f);
return this;
} }
public String getHighlightSimplePost() { public String getHighlightSimplePost() {
return this.get(HighlightParams.SIMPLE_POST, ""); return this.get(HighlightParams.SIMPLE_POST, "");
} }
public void setSortField(String field, ORDER order) { public SolrQuery setSortField(String field, ORDER order) {
this.remove(CommonParams.SORT); this.remove(CommonParams.SORT);
addValueToParam(CommonParams.SORT, toSortString(field, order)); addValueToParam(CommonParams.SORT, toSortString(field, order));
return this;
} }
public void addSortField(String field, ORDER order) { public SolrQuery addSortField(String field, ORDER order) {
addValueToParam(CommonParams.SORT, toSortString(field, order)); return addValueToParam(CommonParams.SORT, toSortString(field, order));
} }
public void removeSortField(String field, ORDER order) { public SolrQuery removeSortField(String field, ORDER order) {
String s = this.get(CommonParams.SORT); String s = this.get(CommonParams.SORT);
String removeSort = toSortString(field, order); String removeSort = toSortString(field, order);
if (s != null) { if (s != null) {
@ -283,6 +302,7 @@ public class SolrQuery extends ModifiableSolrParams
if (s.length()==0) s=null; if (s.length()==0) s=null;
this.set(CommonParams.SORT, s); this.set(CommonParams.SORT, s);
} }
return this;
} }
public String[] getSortFields() { public String[] getSortFields() {
@ -295,12 +315,14 @@ public class SolrQuery extends ModifiableSolrParams
return this.get(CommonParams.SORT); return this.get(CommonParams.SORT);
} }
public void setFilterQueries(String ... fq) { public SolrQuery setFilterQueries(String ... fq) {
this.set(CommonParams.FQ, fq); this.set(CommonParams.FQ, fq);
return this;
} }
public void addFilterQuery(String ... fq) { public SolrQuery addFilterQuery(String ... fq) {
this.add(CommonParams.FQ, fq); this.add(CommonParams.FQ, fq);
return this;
} }
public boolean removeFilterQuery(String fq) { public boolean removeFilterQuery(String fq) {
@ -315,7 +337,7 @@ public class SolrQuery extends ModifiableSolrParams
return this.getBool(HighlightParams.HIGHLIGHT, false); return this.getBool(HighlightParams.HIGHLIGHT, false);
} }
public void setHighlight(boolean b) { public SolrQuery setHighlight(boolean b) {
if (b) { if (b) {
this.set(HighlightParams.HIGHLIGHT, true); this.set(HighlightParams.HIGHLIGHT, true);
} else { } else {
@ -328,9 +350,10 @@ public class SolrQuery extends ModifiableSolrParams
this.remove(HighlightParams.SIMPLE_PRE); this.remove(HighlightParams.SIMPLE_PRE);
this.remove(HighlightParams.SNIPPETS); this.remove(HighlightParams.SNIPPETS);
} }
return this;
} }
public void setFields(String ... fields) { public SolrQuery setFields(String ... fields) {
if( fields == null || fields.length == 0 ) { if( fields == null || fields.length == 0 ) {
this.remove( CommonParams.FL ); this.remove( CommonParams.FL );
} }
@ -341,10 +364,11 @@ public class SolrQuery extends ModifiableSolrParams
sb.append( fields[i] ); sb.append( fields[i] );
} }
this.set(CommonParams.FL, sb.toString() ); this.set(CommonParams.FL, sb.toString() );
return this;
} }
public void addField(String field) { public SolrQuery addField(String field) {
addValueToParam(CommonParams.FL, field); return addValueToParam(CommonParams.FL, field);
} }
public String getFields() { public String getFields() {
@ -355,29 +379,32 @@ public class SolrQuery extends ModifiableSolrParams
return fields; return fields;
} }
public void setIncludeScore(boolean includeScore) { public SolrQuery setIncludeScore(boolean includeScore) {
if (includeScore) { if (includeScore) {
this.add(CommonParams.FL, "score"); this.add(CommonParams.FL, "score");
} else { } else {
this.remove(CommonParams.FL, "score"); this.remove(CommonParams.FL, "score");
} }
return this;
} }
public void setQuery(String query) { public SolrQuery setQuery(String query) {
this.set(CommonParams.Q, query); this.set(CommonParams.Q, query);
return this;
} }
public String getQuery() { public String getQuery() {
return this.get(CommonParams.Q); return this.get(CommonParams.Q);
} }
public void setRows(Integer rows) { public SolrQuery setRows(Integer rows) {
if( rows == null ) { if( rows == null ) {
this.remove( CommonParams.ROWS ); this.remove( CommonParams.ROWS );
} }
else { else {
this.set(CommonParams.ROWS, rows); this.set(CommonParams.ROWS, rows);
} }
return this;
} }
public Integer getRows() public Integer getRows()
@ -394,13 +421,14 @@ public class SolrQuery extends ModifiableSolrParams
// this.set(CommonParams.SORT, sort); // this.set(CommonParams.SORT, sort);
// } // }
public void setStart(Integer start) { public SolrQuery setStart(Integer start) {
if( start == null ) { if( start == null ) {
this.remove( CommonParams.START ); this.remove( CommonParams.START );
} }
else { else {
this.set(CommonParams.START, start); this.set(CommonParams.START, start);
} }
return this;
} }
public Integer getStart() public Integer getStart()
@ -408,20 +436,23 @@ public class SolrQuery extends ModifiableSolrParams
return this.getInt(CommonParams.START); return this.getInt(CommonParams.START);
} }
public void setQueryType(String qt) { public SolrQuery setQueryType(String qt) {
this.set(CommonParams.QT, qt); this.set(CommonParams.QT, qt);
return this;
} }
public String getQueryType() { public String getQueryType() {
return this.get(CommonParams.QT); return this.get(CommonParams.QT);
} }
public void setParam(String name, String ... values) { public SolrQuery setParam(String name, String ... values) {
this.set(name, values); this.set(name, values);
return this;
} }
public void setParam(String name, boolean value) { public SolrQuery setParam(String name, boolean value) {
this.set(name, value); this.set(name, value);
return this;
} }
/** get a deep copy of this object * */ /** get a deep copy of this object * */
@ -453,10 +484,11 @@ public class SolrQuery extends ModifiableSolrParams
return sb.toString().trim(); return sb.toString().trim();
} }
private void addValueToParam(String name, String value) { private SolrQuery addValueToParam(String name, String value) {
String tmp = this.get(name); String tmp = this.get(name);
tmp = join(tmp, value, ","); tmp = join(tmp, value, ",");
this.set(name, tmp); this.set(name, tmp);
return this;
} }
private String join(String[] vals, String sep, String removeVal) { private String join(String[] vals, String sep, String removeVal) {

View File

@ -63,26 +63,29 @@ public class ModifiableSolrParams extends SolrParams
/** /**
* Replace any existing parameter with the given name. if val==null remove key from params completely. * Replace any existing parameter with the given name. if val==null remove key from params completely.
*/ */
public void set( String name, String ... val ) { public ModifiableSolrParams set( String name, String ... val ) {
if (val==null || (val.length==1 && val[0]==null)) { if (val==null || (val.length==1 && val[0]==null)) {
vals.remove(name); vals.remove(name);
} else { } else {
vals.put( name, val ); vals.put( name, val );
} }
return this;
} }
public void set( String name, int val ) { public ModifiableSolrParams set( String name, int val ) {
set( name, String.valueOf(val) ); set( name, String.valueOf(val) );
return this;
} }
public void set( String name, boolean val ) { public ModifiableSolrParams set( String name, boolean val ) {
set( name, String.valueOf(val) ); set( name, String.valueOf(val) );
return this;
} }
/** /**
* Add the given values to any existing name * Add the given values to any existing name
*/ */
public void add( String name, String ... val ) { public ModifiableSolrParams add( String name, String ... val ) {
String[] old = vals.put(name, val); String[] old = vals.put(name, val);
if( old != null ) { if( old != null ) {
int i =0; int i =0;
@ -105,6 +108,7 @@ public class ModifiableSolrParams extends SolrParams
vals.put( name, both ); vals.put( name, both );
} }
} }
return this;
} }
public void add(SolrParams params) public void add(SolrParams params)