Skip to content

Commit 4372215

Browse files
authored
Add support for json params object (#28)
* Add support for json params object * Add test and README example for json params object
1 parent 51a28c0 commit 4372215

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ query := solr.NewQuery(solr.NewDisMaxQueryParser().
5555
solr.NewQueryFacet("high_popularity").
5656
Query("popularity:[8 TO 10]"),
5757
).
58+
Params(solr.M{
59+
"spellcheck.q": "solr rocks",
60+
}).
5861
Sort("score").
5962
Offset(1).
6063
Limit(10).

query.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ type Query struct {
1616
// responseWriter string // wt
1717

1818
// supported params in json request api
19+
// https://solr.apache.org/guide/8_7/json-request-api.html#supported-properties-and-syntax
1920
sort string
2021
offset int // start
2122
limit int // rows
2223
filters []string // fq
2324
fields []string // fl
25+
params M // additional params to add verbatim to request query params
2426

2527
// query is the main query
2628
query string
@@ -68,6 +70,10 @@ func (q *Query) BuildQuery() M {
6870
qm["fields"] = q.fields
6971
}
7072

73+
if len(q.params) > 0 {
74+
qm["params"] = q.params
75+
}
76+
7177
if len(q.facets) > 0 {
7278
facets := M{}
7379
for _, facet := range q.facets {
@@ -110,6 +116,12 @@ func (q *Query) Fields(fields ...string) *Query {
110116
return q
111117
}
112118

119+
// Params sets the params param
120+
func (q *Query) Params(params M) *Query {
121+
q.params = params
122+
return q
123+
}
124+
113125
// Facets sets the facet query
114126
func (q *Query) Facets(facets ...Faceter) *Query {
115127
q.facets = facets

query_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ func TestQuery(t *testing.T) {
4343
Limit(10).
4444
Filters("inStock:true").
4545
Fields("name", "price").
46+
Params(solr.M{
47+
"spellcheck.q": "sports",
48+
}).
4649
BuildQuery()
4750

4851
expect := solr.M{
@@ -54,6 +57,9 @@ func TestQuery(t *testing.T) {
5457
"filter": []string{"inStock:true"},
5558
"limit": 10,
5659
"offset": 1,
60+
"params": solr.M{
61+
"spellcheck.q": "sports",
62+
},
5763
"queries": solr.M{
5864
"query_filters": []solr.M{
5965
{"#size_tag": solr.M{"field": solr.M{"f": "size", "query": "XL"}}},
@@ -66,3 +72,24 @@ func TestQuery(t *testing.T) {
6672

6773
a.Equal(expect, got)
6874
}
75+
76+
// Ensure params can handle arbitrary arguments (like those used in parameter dereferencing)
77+
// https://solr.apache.org/guide/solr/9_7/query-guide/local-params.html#parameter-dereferencing
78+
func TestQueryParamsArbitraryArgs(t *testing.T) {
79+
a := assert.New(t)
80+
got := solr.NewQuery(solr.NewDisMaxQueryParser().
81+
Query("$customQueryArg").BuildParser()).
82+
Params(solr.M{
83+
"customQueryArg": "solr rocks",
84+
}).
85+
BuildQuery()
86+
87+
expect := solr.M{
88+
"params": solr.M{
89+
"customQueryArg": "solr rocks",
90+
},
91+
"query": "{!dismax v=$customQueryArg}",
92+
}
93+
94+
a.Equal(expect, got)
95+
}

0 commit comments

Comments
 (0)