|
Querying
Querying
QueryingSimple queryWhatever you give it is passed straight to Solr's q parameter ISolrOperations<Product> solr = ...
var products1 = solr.Query(new SolrQuery("lucene")); // search for "lucene" in the default field
var products2 = solr.Query(new SolrQuery("name:solr")); // search for "solr" in the "name" fieldQuery by fieldThis allows you to define field name and value separately: ISolrOperations<Product> solr = ...
var products = solr.Query(new SolrQueryByField("name", "solr")); // search for "solr" in the "name" fieldIt also has the benefit that it handles special character escaping for you. Query by rangeCreates a range query: ISolrOperations<Product> solr = ...
var products = solr.Query(new SolrQueryByRange<decimal>("price", 100m, 250.50m)); // search for price between 100 and 250.50Query by list of valuesvar q = new SolrQueryInList("name", "solr", "samsung", "maxtor");is the same as "name:solr OR name:samsung OR name:maxtor" "Any value" queryIt's often convenient to see what documents have a field defined or not: var q = new SolrHasValueQuery("name");is equivalent to the Solr query "name:[* TO *]" Query operatorsYou can use the && and || operators to connect queries, with the expected results: var q = new SolrQuery("solr") && new SolrQuery("name:desc");generates the query "solr AND name:desc" The plus (+) operator is also overloaded. It concatenates the queries and leaves the actual operator to the default as specified in Solr's configuration var q = new SolrQuery("solr") + new SolrQuery("name:desc");creates the query "solr name:desc" To negate a query, you can call Not() on it or just use the '!' operator: var q = !new SolrQuery("solr");creates the query "-solr" Finally, the minus (-) operator: var q = new SolrQuery("solr") - new SolrQuery("name:desc"); // solr - name:descwhich is equivalent to (and more intuitive than): var q = new SolrQuery("solr") + !new SolrQuery("name:desc"); // solr - name:descAlternatively, if you have a list of queries you want to aggregate you can use SolrMultipleCriteriaQuery. For example: new SolrMultipleCriteriaQuery(new[] {new SolrQuery("1"), new SolrQuery("2")})is the same as: new SolrQuery("1") + new SolrQuery("2")You can also define what operators to use to join these queries, e.g: new SolrMultipleCriteriaQuery(new[] {new SolrQuery("1"), new SolrQuery("2")}, "AND")BoostingYou can boost particular queries by calling Boost(), for example: var q = new SolrQuery("name:desc").Boost(2); // (name:desc)^2See the Lucene docs for more information about boosting. DSLSee the fluent API documentation for an alternative way of expressing queries. Filter queriesFilter queries can be used to specify a query that can be used to restrict the super set of documents that can be returned, without influencing score. ISolrOperations<Product> solr = ...
var products = solr.Query(SolrQuery.All, new QueryOptions {
FilterQueries = new ISolrQuery[] {
new SolrQueryByField("manu", "apache"),
new SolrQueryByRange<decimal>("price", 100m, 200m),
}
});More information in the Solr wiki. FieldsBy default Solr returns all stored fields. You can retrieve only selected fields instead: ISolrOperations<Product> solr = ...
var products = solr.Query(SolrQuery.All, new QueryOptions {
Fields = new[] {"id", "manu"}
});SortingBy default Solr returns search results ordered by "score desc". You can sort the results by any field(s): ISolrOperations<Product> solr = ...
var products = solr.Query(SolrQuery.All, new QueryOptions {
OrderBy = new[] {new SortOrder("manu", Order.DESC), SortOrder.Parse("id asc")}
});You can random sort using RandomSortOrder: solr.Query(SolrQuery.All, new QueryOptions {
OrderBy = new[] {new RandomSortOrder("randomF")},
});where "randomF" is a random sort field. RandomSortOrder has various constructors to generate a random seed (as in the example above) or use a predefined seed. PaginationIn Solr you can't retrieve all your documents in single query. However, by default SolrNet will try to retrieve a large amount of documents, trying to mimic the behavior of a RDBMS without a TOP clause. It's not recommended to rely on this behavior. Instead, always define pagination parameters, for example: ISolrOperations<Product> solr = ...
solr.Query("somequery", new QueryOptions{
Start = 10,
Rows = 25
});This will fetch at most 25 documents, starting from the 10th document in the total result set. Additional parametersSolr has lots of features that aren't directly mapped in SolrNet, but you can enable and use most of them with the ExtraParams dictionary. Parameters defined in ExtraParams are directly passed to the Solr querystring. For example you can restrict the maximum time allowed for a query: ISolrOperations<Product> solr = ...
var products = solr.Query(SolrQuery.All, new QueryOptions {
ExtraParams = new Dictionary<string, string> {
{"timeAllowed", "100"}
}
});Or enable DisMax instead of the standard request handler: ISolrOperations<Product> solr = ...
var products = solr.Query(SolrQuery.All, new QueryOptions {
ExtraParams = new Dictionary<string, string> {
{"qt", "dismax"}
}
});LocalParamsLocalParams provide a way to add certain metadata to a piece of query. It's used among other things to change the default operator type on the fly for a particular query. In SolrNet, LocalParams are represented by the LocalParams class, which is basically a Dictionary<string, string>. LocalParams are attached to a query using the "+" operator. Here's an example: solr.Query(new LocalParams {{"type", "dismax"},{"qf", "myfield"}} + new SolrQuery("solr rocks"));This will generate: q={!type=dismax qf=myfield}solr rocks More information about LocalParams in the Solr wiki. |