treygrainger opened a new pull request #1419: SOLR-14397: Vector Search in Solr URL: https://github.com/apache/lucene-solr/pull/1419 <!-- _(If you are a project committer then you may remove some/all of the following template.)_ Before creating a pull request, please file an issue in the ASF Jira system for Lucene or Solr: * https://issues.apache.org/jira/projects/LUCENE * https://issues.apache.org/jira/projects/SOLR You will need to create an account in Jira in order to create an issue. The title of the PR should reference the Jira issue number in the form: * LUCENE-####: <short description of problem or changes> * SOLR-####: <short description of problem or changes> LUCENE and SOLR must be fully capitalized. A short description helps people scanning pull requests for items they can work on. Properly referencing the issue in the title ensures that Jira is correctly updated with code review comments and commits. --> # 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. # 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](https://wiki.apache.org/solr/HowToContribute) 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](https://help.github.com/en/articles/allowing-changes-to-a-pull-request-branch-created-from-a-fork) to contribute to my PR branch. (optional but recommended) - [X] I have developed this patch against the `master` branch. - [ ] I have run `ant precommit` and the appropriate test suite. - [ ] I have added tests for my changes. - [ ] I have added documentation for the [Ref Guide](https://github.com/apache/lucene-solr/tree/master/solr/solr-ref-guide) (for Solr changes only).
---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org