Skip to content

SearchBoxOperations

Tharanga edited this page Dec 29, 2016 · 1 revision
@RestController
@RequestMapping("/movies")
public class MovieController {
	
	@Autowired
	private SearchBoxOperations searchBoxOperations;
	
	...
	

insert,update and save

<T> void insert(T object) throws SearchBoxOperationsException
<T> void update(T object) throws SearchBoxOperationsException
<T> void save(T object) throws SearchBoxOperationsException
  • insert Check id is exists. Not allow for duplicate ids.
  • update Check id is exists. Then modify perticular model. Otherwise raised Id is not found exception.
  • save Check id is exists. If Id is exists update the model. Otherwise insert a new model.

search by field value

<T> SearchResult<T> searchByField(Class<T> cls, String field, Object searchValue, Page page) throws SearchBoxOperationsException;
  • example searchBoxOperations.searchByField(Movie.class, "id", id)

search by perfix value

<T> SearchResult<T> searchByFieldPerfix(Class<T> cls, String field, Object searchPrefix, Page page, boolean allWords) throws SearchBoxOperationsException;
  • example searchBoxOperations.searchByFieldPerfix(Movie.class, "title", "av") => ["Avatar", "Avengers", ...]

search by text pattern

<T> SearchResult<T> searchByFieldPattern(Class<T> cls, String field, String pattern, Page page) throws SearchBoxOperationsException;
  • example searchBoxOperations.searchByFieldPattern(Movie.class, "title", "*nic") => ["Titanic", ...]

Query Functions

@QueryController
public class QueryFunctions {

	@QueryFunction("query1")
	public QueryHolder query1() throws Exception{
		
		Criteria c = Criteria
				.where("@parm_key").is("@parm_value");
		
		Query query = new Query(c);
		
		return new QueryHolder(Movie.class, query);
		
	}
	...
  • QueryFunctions are used PlaceHolders with '@' prefix to pass runtime values into query. Ex: @parm_key, @parm_value

  • Calling QueryFunctions by using SearchBoxOperations.

<T> SearchResult<T> search(String queryName, Map<String, Object> inputParms, Page page) throws SearchBoxOperationsException;
  • example
String queryName = "query1";
		
Map<String, Object> parms = new HashMap<>();
parms.put("parm_key", "title");
parms.put("parm_value", "Avatar");
	
searchBoxOperations.search("query1", parms);

search with Query

<T> SearchResult<T> search(Class<T> cls, Query query, Page page) throws SearchBoxOperationsException;
  • example searchBoxOperations.search(Movie.class, new Query(Criteria.where("id").is(7)))
  • note : QueryFunctions are load when server boot. Always recommended use QueryFunctions insted of using search with Query because of QueryFunctions are faster than search with Query.