Note that this class is not thread-safe and cannot be used as a member -level property -- if this is required, use the {@link com.coherentlogic.fred.client.core.builders#RequestBuilderFactory QueryBuilderFactory} class.
In order to facilitate method-chaining each setter method returns a reference to this object.
For examples, refer to the {@link com.coherentlogic.fred.client.core.builders#QueryBuilderTest QueryBuilderTest} class. @author Support
Example usage:
QueryBuilder builder = new QueryBuilder(analyzer); Query a = builder.createBooleanQuery("body", "just a test"); Query b = builder.createPhraseQuery("body", "another test"); Query c = builder.createMinShouldMatchQuery("body", "another test", 0.5f);
This can also be used as a subclass for query parsers to make it easier to interact with the analysis chain. Factory methods such as {@code newTermQuery} are provided so that the generated queries can be customized.
This builder is stateful and therefore should only be used by one thread at a time. However, once a query has been built, the builder can be {@link #clear() cleared} and used to create another query.
The order in which the methods are called are (for the most part) important. Simply call the methods in the same order that would be most natural in a normal SQL query. For example, the following code creates a Query object that is equivalent to " SELECT * FROM table
":
QueryCommand query = builder.selectStar().from("table").query();
Here are a few other examples:
SQL Statement | QueryBuilder code |
---|---|
SELECT * FROM table1 INNER JOIN table2 ON table2.c0 = table1.c0 | query = builder.selectStar().from("table1").join("table2").on("table2.c0=table1.c0").query(); |
SELECT * FROM table1 AS t1 INNER JOIN table2 AS t2 ON t1.c0 = t2.c0 | query = builder.selectStar().from("table1 AS t1").join("table2 AS t2").on("t1.c0=t2.c0").query(); |
SELECT * FROM table1 AS t1 INNER JOIN table2 AS t2 ON t1.c0 = t2.c0 INNER JOIN table3 AS t3 ON t1.c1 = t3.c1 | query = builder.selectStar() .from("table1 AS t1") .innerJoin("table2 AS t2") .on("t1.c0=t2.c0") .innerJoin("table3 AS t3") .on("t1.c1=t3.c1") .query(); |
SELECT * FROM table1 UNION SELECT * FROM table2 | query = builder.selectStar().from("table1").union().selectStar().from("table2").query(); |
SELECT t1.c1,t1.c2,t2.c3 FROM table1 AS t1 INNER JOIN table2 AS t2 ON t1.c0 = t2.c0 UNION ALL SELECT t3.c1,t3.c2,t4.c3 FROM table3 AS t3 INNER JOIN table4 AS t4 ON t3.c0 = t4.c0 | query = builder.select("t1.c1","t1.c2","t2.c3",) .from("table1 AS t1") .innerJoin("table2 AS t2") .on("t1.c0=t2.c0") .union() .select("t3.c1","t3.c2","t4.c3",) .from("table3 AS t3") .innerJoin("table4 AS t4") .on("t3.c0=t4.c0") .query(); |
This builder is stateful and therefore should only be used by one thread at a time. However, once a query has been built, the builder can be {@link #clear() cleared} and used to create another query.
The order in which the methods are called are (for the most part) important. Simply call the methods in the same order that would be most natural in a normal SQL query. For example, the following code creates a Query object that is equivalent to " SELECT * FROM table
":
QueryCommand query = builder.selectStar().from("table").query();
Here are a few other examples:
SQL Statement | QueryBuilder code |
---|---|
SELECT * FROM table1 INNER JOIN table2 ON table2.c0 = table1.c0 | query = builder.selectStar().from("table1").join("table2").on("table2.c0=table1.c0").query(); |
SELECT * FROM table1 AS t1 INNER JOIN table2 AS t2 ON t1.c0 = t2.c0 | query = builder.selectStar().from("table1 AS t1").join("table2 AS t2").on("t1.c0=t2.c0").query(); |
SELECT * FROM table1 AS t1 INNER JOIN table2 AS t2 ON t1.c0 = t2.c0 INNER JOIN table3 AS t3 ON t1.c1 = t3.c1 | query = builder.selectStar().from("table1 AS t1").innerJoin("table2 AS t2").on("t1.c0=t2.c0").innerJoin("table3 AS t3") .on("t1.c1=t3.c1").query(); |
SELECT * FROM table1 UNION SELECT * FROM table2 | query = builder.selectStar().from("table1").union().selectStar().from("table2").query(); |
SELECT t1.c1,t1.c2,t2.c3 FROM table1 AS t1 INNER JOIN table2 AS t2 ON t1.c0 = t2.c0 UNION ALL SELECT t3.c1,t3.c2,t4.c3 FROM table3 AS t3 INNER JOIN table4 AS t4 ON t3.c0 = t4.c0 | query = builder.select("t1.c1","t1.c2","t2.c3",) .from("table1 AS t1") .innerJoin("table2 AS t2") .on("t1.c0=t2.c0") .union() .select("t3.c1","t3.c2","t4.c3",) .from("table3 AS t3") .innerJoin("table4 AS t4") .on("t3.c0=t4.c0") .query(); |
More information about the JSON query syntax can be found in {@link JsonQueryParser}.
The builder enables the creation of {@link AbstractQuery} which can then beconverted into a JSON representation using the method {@link AbstractQuery#toString()} or into a {@link Query} using the method{@link AbstractQuery#toQuery(boolean)}. @see JsonQueryParser
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|