Last active
March 22, 2023 10:57
-
-
Save sdavids13/c3e2d9ddff38cd4fcf98d3593f5726c4 to your computer and use it in GitHub Desktop.
Solr block join filtering example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cd <solr distribution> | |
$ bin/solr start | |
$ bin/solr create -c demo | |
$ curl http://localhost:8983/solr/demo/update?commitWithin=3000 -d '{delete:{query:"*:*"}}' | |
$ curl http://localhost:8983/solr/demo/update?commitWithin=3000 -d ' | |
[ | |
{id : book1, type_s:book, title_t : "The Way of Kings", author_s : "Brandon Sanderson", | |
cat_s:fantasy, pubyear_i:2010, publisher_s:Tor, bin_s: bar, | |
_childDocuments_ : [ | |
{ id: book1_c1, type_s:review, review_dt:"2015-01-03T14:30:00Z", | |
stars_i:5, author_s:yonik, | |
comment_t:"A great start to what looks like an epic series!", | |
bin_s: foo | |
}, | |
{ id: book1_c2, type_s:review, review_dt:"2014-03-15T12:00:00Z", | |
stars_i:3, author_s:dan, | |
comment_t:"This book was too long.", | |
bin_s: bar | |
} | |
] | |
}, | |
{id : book2, type_s:book, title_t : "Snow Crash", author_s : "Neal Stephenson", | |
cat_s:sci-fi, pubyear_i:1992, publisher_s:Bantam, bin_s: baz, | |
_childDocuments_ : [ | |
{ id: book2_c1, type_s:review, review_dt:"2015-01-03T14:30:00Z", | |
stars_i:5, author_s:yonik, | |
comment_t:"Ahead of its time... I wonder if it helped inspire The Matrix?", | |
bin_s: foo | |
}, | |
{ id: book2_c2, type_s:review, review_dt:"2015-04-10T9:00:00Z", | |
stars_i:2, author_s:dan, | |
comment_t:"A pizza boy for the Mafia franchise? Really?", | |
bin_s: foo | |
}, | |
{ id: book2_c3, type_s:review, review_dt:"2015-06-02T00:00:00Z", | |
stars_i:4, author_s:mary, | |
comment_t:"Neal is so creative and detailed! Loved the metaverse!", | |
bin_s: baz | |
} | |
] | |
}, | |
{id : book3, type_s:book, title_t : "Snow Crash 2", author_s : "Neal Stephenson", | |
cat_s:sci-fi, pubyear_i:1995, publisher_s:Bantam, bin_s: foo, | |
_childDocuments_ : [ | |
{ id: book3_c1, type_s:review, review_dt:"2015-01-03T14:30:00Z", | |
stars_i:1, author_s:yonik, | |
comment_t:"Yet another lame sequel", | |
bin_s: foo | |
} | |
] | |
} | |
]' | |
$ curl http://localhost:8983/solr/demo/query -d 'q=cat_s:(fantasy OR sci-fi)&fl=*,[child parentFilter=type_s:book]' | |
{ | |
"responseHeader":{ | |
"status":0, | |
"QTime":3, | |
"params":{ | |
"q":"cat_s:(fantasy OR sci-fi)", | |
"fl":"*,[child parentFilter=type_s:book]"}}, | |
"response":{"numFound":3,"start":0,"docs":[ | |
{ | |
"id":"book1", | |
"type_s":"book", | |
"title_t":["The Way of Kings"], | |
"author_s":"Brandon Sanderson", | |
"cat_s":"fantasy", | |
"pubyear_i":2010, | |
"publisher_s":"Tor", | |
"bin_s":"bar", | |
"_version_":1550040224972668928, | |
"_childDocuments_":[ | |
{ | |
"id":"book1_c1", | |
"type_s":"review", | |
"review_dt":"2015-01-03T14:30:00Z", | |
"stars_i":5, | |
"author_s":"yonik", | |
"comment_t":["A great start to what looks like an epic series!"], | |
"bin_s":"foo"}, | |
{ | |
"id":"book1_c2", | |
"type_s":"review", | |
"review_dt":"2014-03-15T12:00:00Z", | |
"stars_i":3, | |
"author_s":"dan", | |
"comment_t":["This book was too long."], | |
"bin_s":"bar"}]}, | |
{ | |
"id":"book2", | |
"type_s":"book", | |
"title_t":["Snow Crash"], | |
"author_s":"Neal Stephenson", | |
"cat_s":"sci-fi", | |
"pubyear_i":1992, | |
"publisher_s":"Bantam", | |
"bin_s":"baz", | |
"_version_":1550040224976863232, | |
"_childDocuments_":[ | |
{ | |
"id":"book2_c1", | |
"type_s":"review", | |
"review_dt":"2015-01-03T14:30:00Z", | |
"stars_i":5, | |
"author_s":"yonik", | |
"comment_t":["Ahead of its time... I wonder if it helped inspire The Matrix?"], | |
"bin_s":"foo"}, | |
{ | |
"id":"book2_c2", | |
"type_s":"review", | |
"review_dt":"2015-04-10T09:00:00Z", | |
"stars_i":2, | |
"author_s":"dan", | |
"comment_t":["A pizza boy for the Mafia franchise? Really?"], | |
"bin_s":"foo"}, | |
{ | |
"id":"book2_c3", | |
"type_s":"review", | |
"review_dt":"2015-06-02T00:00:00Z", | |
"stars_i":4, | |
"author_s":"mary", | |
"comment_t":["Neal is so creative and detailed! Loved the metaverse!"], | |
"bin_s":"baz"}]}, | |
{ | |
"id":"book3", | |
"type_s":"book", | |
"title_t":["Snow Crash 2"], | |
"author_s":"Neal Stephenson", | |
"cat_s":"sci-fi", | |
"pubyear_i":1995, | |
"publisher_s":"Bantam", | |
"bin_s":"foo", | |
"_version_":1550040224978960384, | |
"_childDocuments_":[ | |
{ | |
"id":"book3_c1", | |
"type_s":"review", | |
"review_dt":"2015-01-03T14:30:00Z", | |
"stars_i":1, | |
"author_s":"yonik", | |
"comment_t":["Yet another lame sequel"], | |
"bin_s":"foo"}]}] | |
}} | |
Note: the fq is only applied to the parent documents, if it applied to child documents book1 wouldn't show up because yonik reviews has bins_s of foo: | |
$ curl http://localhost:8983/solr/demo/query -d 'q=%2bcat_s:(sci-fi OR fantasy) %2b_query_:"{!parent which=type_s:book} %2bauthor_s:(mary yonik)"&fq=bin_s:(baz bar)&fl=*' | |
{ | |
"responseHeader":{ | |
"status":0, | |
"QTime":0, | |
"params":{ | |
"q":"+cat_s:(sci-fi OR fantasy) +_query_:\"{!parent which=type_s:book} +author_s:(mary yonik)\"", | |
"fl":"*", | |
"fq":"bin_s:(baz bar)"}}, | |
"response":{"numFound":2,"start":0,"docs":[ | |
{ | |
"id":"book1", | |
"type_s":"book", | |
"title_t":["The Way of Kings"], | |
"author_s":"Brandon Sanderson", | |
"cat_s":"fantasy", | |
"pubyear_i":2010, | |
"publisher_s":"Tor", | |
"bin_s":"bar", | |
"_version_":1550040224972668928}, | |
{ | |
"id":"book2", | |
"type_s":"book", | |
"title_t":["Snow Crash"], | |
"author_s":"Neal Stephenson", | |
"cat_s":"sci-fi", | |
"pubyear_i":1992, | |
"publisher_s":"Bantam", | |
"bin_s":"baz", | |
"_version_":1550040224976863232}] | |
}} | |
You would need to set an upfront query clause inside of the blockjoin query to provide the desired result: | |
$ curl http://localhost:8983/solr/demo/query -d 'q=%2bcat_s:(sci-fi OR fantasy) %2b_query_:"{!parent which=type_s:book} %2bbin_s:(baz bar) %2bauthor_s:(mary yonik)"&fq=bin_s:(baz bar)&fl=*' | |
{ | |
"responseHeader":{ | |
"status":0, | |
"QTime":3, | |
"params":{ | |
"q":"+cat_s:(sci-fi OR fantasy) +_query_:\"{!parent which=type_s:book} +bin_s:(baz bar) +author_s:(mary yonik)\"", | |
"fl":"*", | |
"fq":"bin_s:(baz bar)"}}, | |
"response":{"numFound":1,"start":0,"docs":[ | |
{ | |
"id":"book2", | |
"type_s":"book", | |
"title_t":["Snow Crash"], | |
"author_s":"Neal Stephenson", | |
"cat_s":"sci-fi", | |
"pubyear_i":1992, | |
"publisher_s":"Bantam", | |
"bin_s":"baz", | |
"_version_":1550040224976863232}] | |
}} | |
We can simplify the redundent filter by using parameter substitution: | |
$ curl http://localhost:8983/solr/demo/query -d 'q=%2bcat_s:(sci-fi OR fantasy) %2b_query_:"{!parent which=type_s:book} ${binFilter} %2bauthor_s:(mary yonik)"&fq=${binFilter}&fl=*&binFilter=%2bbin_s:(baz bar)' | |
We can cache the binFilter query results by using the `filter()` query method: | |
$ curl http://localhost:8983/solr/demo/query -d 'q=%2bcat_s:(sci-fi OR fantasy) %2b_query_:"{!parent which=type_s:book} %2bfilter(${binFilter}) %2bauthor_s:(mary yonik)"&fq=${binFilter}&fl=*&binFilter=%2bbin_s:(baz bar)' | |
We can get rid of the nested `_query_` syntax and specify the query in the parent block join query definition: | |
$ curl http://localhost:8983/solr/demo/query -d 'q=%2bcat_s:(sci-fi OR fantasy) %2b{!parent which=type_s:book v="%2bfilter(${binFilter}) %2bauthor_s:(mary yonik)"}&fq=${binFilter}&fl=*&binFilter=%2bbin_s:(baz bar)' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment