Question

I am trying to add FullTextFilters to my FullTextQuery in hibernate and there is only the method FullTextFilter.setParameter(String name, Object value)

I am trying to make a flexible, generic function to add filters to the query based on the entity its searching for, some have one parameter, some have two for their filters, so I would like to add a method to FullTextFilterImpl; setParameters(String[] names, String[] value) where I can pass in the names of all the parameters and probably a multidimensional array of the values for each parameter to transform my current code of

If( "checking which entity it is"){
  fullTextQuery.enableFullTextFilter("FilterName").setParameter("firstFilter", "val1").setParameter("secondFilter", "val2");
}
else if("this entity's filter only has one parameter"){
     fullTextQuery.enableFullTextFilter("FilterName").setParameter("firstFilter", "val1");
}

I tried creating a subclass of FullTextFilterImpl and putting a setParameters function in it, but the way this code is set up I'm not sure how to utilize it as FullTextQuery.enableFullTextFilter(filterName) returns a FullTextFilter object and then you call the setParameter() on that object. I'm not sure how I would get in the middle of that to do a setParameters

EDIT: I have downloaded the hibernate-search source code and added the following method to FullTextFilterImpl which I think will do what I want, but when I go to build it (even just the out-of-the-box project) I get all these checkstyle Only one new line is allowed at the end of a file errors. Is there something I'm missing from the hibernate quick-build guide.

public FullTextFilter setParameters(Map<String, List<String>> params){
    for (String key : params.keySet()) {
        List<String> values = params.get(key);
        for(int i=0; i< values.size() ; i++){
            parameters.put(key, values.get(i));
        }
    }
    return this;
}
Was it helpful?

Solution

You can easily pass a Map of attributes to your custom Filter, the signature is:

FullTextFilter setParameter(String name, Object value);

so you could do

filter.setParameter( "myMap", properties );

where properties is an hashmap.

About the compilation error message:

Only one new line is allowed at the end of a file

is a message from checkstyle, it verifies code style is conforming to the Hibernate code style. It's very simple to fix: there are multiple empty lines at the end of the source file, delete them. The error message should tell you what file needs to be polished.

OTHER TIPS

if i correctly understand you question you need Builder pattern

here an example you could use :

public class FullTextFilter {

    String[] keys;
    Object[] objects;

    private FullTextFilter(String[] keys, Object[] objects) {
    }

    public static FullTextFilterBuilder builder(){
        return new FullTextFilterBuilder();
    }

    public static class FullTextFilterBuilder {

        private Map<String, Object> parameters = new HashMap<String, Object>();

        public FullTextFilterBuilder setParameter(String key, Object value){
            parameters.put(key, value);
            return this;
        }

        public FullTextFilter build(){
            return new FullTextFilter(parameters.keySet().toArray(new String[0]), parameters.values().toArray(new Object[0]));
        }
    }

}

and then using it like this :

FullTextFilter filter = FullTextFilter.builder().setParameter("", new Object()).setParameter("", new Object()).build();

tell if that's what you are looking for. if not i'll delete my answer

I presume you want this:

fullTextQuery.enableFullTextFilter("FilterName").setParameter("firstFilter", "val1").setParameter("secondFilter", "val2");

fullTextQuery{ name:"FilterName" ,parameters:["filter1":"value1", "filter2":"value2"] }

static FullTextQuery enableFullTextFilter(String name){...}
FullTextQuery setParameter(String key, String value){
   parameters.put(key, value);
   return this;
}

assuming a parameters hashmap.


seeing as I was a little off base.. cant you do something like this?

setFilters (HashMap<String, String> filters) {
  FullTTextFilter fl = FullTextQuery.enableFullTextFilter("filtername");
  for (String key : filters.keySet()) {
    fl.setParameter(key, filters.get(key));
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top