SOLR-14397: Vector Search in Solr
Description
WORK IN PROGRESS, DO NOT MERGE.
Adds in vector_cosine function query and vector_dotproduct function queries, which can operate on ValueSources containing dense vector content. Supports multiple vectors per field (separated by |. For now, best to use a String field (with docValues=true) to create the vectors on documents, but will ultimately be implementing a DenseVector field to handle this more efficiently. See: https://issues.apache.org/jira/browse/SOLR-14397 for background info.
Solution
Since multivalued docvalues don't maintain insertion order, multiple vectors are instead encoded into the same docvalue per document separated by a | character. Currently the vectors are represented as raw strings (no Base64 encoding of Binary encoding - will do that later). Initial implementation let's you send in one or more vectors in the field, and at query time, to choose to return the score as the first parameter (the query vector) with either the first, last, max, min, or average similarity with all of the vectors in the vectors field.
Tests
No unit tests yet. You can use/test the functionality as follows:
Build and Start
bin/solr stop || ant server && bin/solr start -c -a "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=6900"
Create Collection
curl -H "Content-Type: application/json" \
"http://localhost:8983/solr/admin/collections?action=CREATE&name=vectors&collection.configName=_default&numShards=1"
Index Documents
curl -X POST -H "Content-Type: application/json" \
http://localhost:8983/solr/vectors/update?commit=true \
--data-binary ' [
{"id": "1", "name_s":"donut", "vectors_s":["5.0,0.0,1.0,5.0,0.0,4.0,5.0,1.0|4.0,0.0,1.2,3.0,0.3,3.0,3.0,0.75|6.0,0.0,2.0,4.0,0.0,5.0,6.0,0.8"]},
{"id": "2", "name_s":"apple juice",
"vectors_s":["1.0,5.0,0.0,0.0,0.0,4.0,4.0,3.0|0.0,5.0,0.0,0.0,0.0,3.0,5.0,4.0"]},
{"id": "3", "name_s":"cappuccino",
"vectors_s":["0.0,5.0,3.0,0.0,4.0,1.0,2.0,3.0|"]},
{"id": "4", "name_s":"cheese pizza",
"vectors_s":["5.0,0.0,4.0,4.0,0.0,1.0,5.0,2.0"]},
{"id": "5", "name_s":"green tea",
"vectors_s":["0.0,5.0,0.0,0.0,2.0,1.0,1.0,5.0"]},
{"id": "6", "name_s":"latte", "vectors_s":["0.0,5.0,4.0,0.0,4.0,1.0,3.0,3.0"]},
{"id": "7", "name_s":"soda", "vectors_s":["0.0,5.0,0.0,0.0,3.0,5.0,5.0,0.0"]},
{"id": "8", "name_s":"cheese bread sticks",
"vectors_s":["5.0,0.0,4.0,5.0,0.0,1.0,4.0,2.0"]},
{"id": "9", "name_s":"water", "vectors_s":["0.0,5.0,0.0,0.0,0.0,0.0,0.0,5.0"]},
{"id": "10", "name_s":"cinnamon bread sticks", "vectors_s":["5.0,0.0,1.0,5.0,0.0,3.0,4.0,2.0"]}
] '
Send Query
curl -H "Content-Type: application/json" \
"http://localhost:8983/solr/vectors/select?q=*:*&fl=id,name:name_s,cosine:\$func,vectors:vectors_s&func=vector_cosine(\$donut_vector,vectors_s,average)&sort=\$func%20desc&rows=11&donut_vector=5.0,0.0,1.0,5.0,0.0,4.0,5.0,1.0"
Response:
{
"responseHeader":{
"zkConnected":true,
"status":0,
"QTime":1,
"params":{
"q":"*:*",
"func":"vector_cosine($donut_vector,vectors_s,average)",
"donut_vector":"5.0,0.0,1.0,5.0,0.0,4.0,5.0,1.0",
"fl":"id,name:name_s,cosine:$func,vectors:vectors_s",
"json":"",
"sort":"$func desc",
"rows":"11"}},
"response":{"numFound":10,"start":0,"docs":[
{
"id":"1",
"cosine":0.9884526,
"name":"donut",
"vectors":"5.0,0.0,1.0,5.0,0.0,4.0,5.0,1.0|4.0,0.0,1.2,3.0,0.3,3.0,3.0,0.75|6.0,0.0,2.0,4.0,0.0,5.0,6.0,0.8"},
{
"id":"10",
"cosine":0.98544514,
"name":"cinnamon bread sticks",
"vectors":"5.0,0.0,1.0,5.0,0.0,3.0,4.0,2.0"},
{
"id":"4",
"cosine":0.88938314,
"name":"cheese pizza",
"vectors":"5.0,0.0,4.0,4.0,0.0,1.0,5.0,2.0"},
{
"id":"8",
"cosine":0.88938314,
"name":"cheese bread sticks",
"vectors":"5.0,0.0,4.0,5.0,0.0,1.0,4.0,2.0"},
{
"id":"2",
"cosine":0.524165,
"name":"apple juice",
"vectors":"1.0,5.0,0.0,0.0,0.0,4.0,4.0,3.0|0.0,5.0,0.0,0.0,0.0,3.0,5.0,4.0"},
{
"id":"7",
"cosine":0.50913316,
"name":"soda",
"vectors":"0.0,5.0,0.0,0.0,3.0,5.0,5.0,0.0"},
{
"id":"6",
"cosine":0.30926093,
"name":"latte",
"vectors":"0.0,5.0,4.0,0.0,4.0,1.0,3.0,3.0"},
{
"id":"3",
"cosine":0.25923792,
"name":"cappuccino",
"vectors":"0.0,5.0,3.0,0.0,4.0,1.0,2.0,3.0|"},
{
"id":"5",
"cosine":0.1939959,
"name":"green tea",
"vectors":"0.0,5.0,0.0,0.0,2.0,1.0,1.0,5.0"},
{
"id":"9",
"cosine":0.073323555,
"name":"water",
"vectors":"0.0,5.0,0.0,0.0,0.0,0.0,0.0,5.0"}]
}}
Checklist
Please review the following and check all that apply:
- [X] I have reviewed the guidelines for How to Contribute and my code conforms to the standards described there to the best of my ability.
- [X] I have created a Jira issue and added the issue ID to my pull request title.
- [X] I have given Solr maintainers access to contribute to my PR branch. (optional but recommended)
- [X] I have developed this patch against the
masterbranch. - [ ] I have run
ant precommitand the appropriate test suite. - [ ] I have added tests for my changes.
- [ ] I have added documentation for the Ref Guide (for Solr changes only).