├── .gitignore ├── .travis.yml ├── CHANGES.txt ├── LICENSE ├── README.md ├── apache └── httpd.conf ├── bin ├── file-loader.sh ├── generate-xml.sh ├── prep-classpath.sh └── prep-classpath.tmpl ├── doc ├── INSTALL.txt └── README.txt ├── etc └── PrefixFromFile.jmx ├── example ├── etc │ ├── jetty.xml │ └── webdefault.xml ├── exampledocs │ ├── just-phrases.txt │ ├── phrases-sponsored.txt │ └── phrases-urls.txt ├── lib │ ├── jetty-6.1.3.jar │ ├── jetty-util-6.1.3.jar │ ├── jsp-2.1 │ │ ├── ant-1.6.5.jar │ │ ├── core-3.1.1.jar │ │ ├── jsp-2.1.jar │ │ └── jsp-api-2.1.jar │ └── servlet-api-2.5-6.1.3.jar ├── solr │ ├── collection1 │ │ └── conf │ │ │ ├── admin-extra.html │ │ │ ├── data-config.xml │ │ │ ├── managed-schema │ │ │ ├── schema-function-query-ordering.xml │ │ │ ├── solr.xml │ │ │ └── solrconfig.xml │ └── urp │ │ └── solrconfig.xml └── start.jar ├── package.sh ├── pom.xml ├── solr └── collection1 │ └── conf │ ├── managed-schema │ ├── solrconfig.xml │ ├── stopwords.txt │ └── synonyms.txt ├── src ├── main │ └── java │ │ ├── com │ │ └── sematext │ │ │ └── autocomplete │ │ │ ├── loader │ │ │ ├── CustomIndexLoader.java │ │ │ ├── FileLoader.java │ │ │ └── IndexLoader.java │ │ │ ├── servlet │ │ │ └── AutoCompleteServlet.java │ │ │ ├── solr │ │ │ ├── AcGroupResult.java │ │ │ ├── AcGroupingFieldValue.java │ │ │ ├── AutoCompleteRequestHandler.java │ │ │ ├── AutoCompleteSearchComponent.java │ │ │ └── group │ │ │ │ ├── DictionaryGroupingSort.java │ │ │ │ ├── ExamplePushToTheTopGroupingSort.java │ │ │ │ ├── ExampleResalePriceGroupingHandler.java │ │ │ │ ├── GroupingHandler.java │ │ │ │ ├── GroupingSort.java │ │ │ │ └── LocationGroupingHandler.java │ │ │ ├── tst │ │ │ ├── AutoCompleteService.java │ │ │ ├── CharUtility.java │ │ │ ├── DoublyLinkedList.java │ │ │ └── TernarySearchTree.java │ │ │ └── urp │ │ │ ├── AutocompleteUpdateRequestProcessor.java │ │ │ └── AutocompleteUpdateRequestProcessorFactory.java │ │ └── org │ │ └── apache │ │ └── lucene │ │ └── analysis │ │ └── position │ │ ├── PositionFilter.java │ │ └── PositionFilterFactory.java └── test │ └── java │ └── com │ └── sematext │ └── autocomplete │ ├── LoadPerformaceTester.java │ ├── PrefixTestListCreator.java │ └── solr │ └── AutoCompleteSearchComponentTest.java └── web ├── WEB-INF └── web.xml ├── auto-complete-function-query-order.html ├── auto-complete-rh.html ├── auto-complete-servlet.html ├── auto-complete-sort.html ├── auto-complete.html ├── css ├── autocomplete.css └── fonts │ └── fonts-min.css └── js ├── animation-min.js ├── autocomplete-min.js ├── autocomplete.js ├── connection-min.js ├── datasource-min.js └── yahoo-dom-event.js /.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /.idea 3 | /st-AutoComplete.iml 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | jdk: 3 | - oraclejdk8 4 | -------------------------------------------------------------------------------- /CHANGES.txt: -------------------------------------------------------------------------------- 1 | In version 1.6.7.1.0 2 | - 2017-10-19: upgrade to Solr 7.1.0 3 | In version 1.6.6.5.1 4 | - 2017-04-18: upgrade to Solr 6.5.1 5 | In version 1.6.6.3.0 6 | - 2016-12-09: upgrade to Solr 6.3.0 7 | In version 1.6.6.0.1 8 | - 2016-06-13: upgrade to Solr 6.0.1 -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /apache/httpd.conf: -------------------------------------------------------------------------------- 1 | # 2 | # Proxy module is needed to hide the servlet container with Solr behind Apache 3 | # 4 | LoadModule proxy_module /usr/lib/apache2/modules/mod_proxy.so 5 | LoadModule proxy_http_module /usr/lib/apache2/modules/mod_proxy_http.so 6 | 7 | # 8 | # URL Rewrite module is needed to expose simpler, user-friendlier URLs to the front end 9 | # 10 | LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so 11 | 12 | # 13 | # Hide Solr running on the same server behind Apache's /server path 14 | # 15 | ProxyPass /server http://localhost:8983/solr 16 | ProxyPassReverse /server http://localhost:8983/solr 17 | 18 | # 19 | # Expose /ac/ URL as the AutoComplete URL and rewrite it behind the scenes 20 | # 21 | RewriteRule ^/ac/(.*)$ /server/ac/select?wt=json&omitHeader=true&q=${escape:$2} [PT,L] 22 | 23 | # 24 | # Expose /ac/ URL as the AutoComplete URL and rewrite it behind the scenes 25 | # sort results by popularity field in ascending order 26 | # 27 | #RewriteRule ^/ac/(.*)$ /server/ac/select?wt=json&omitHeader=true&q=${escape:$2}&sort=popularity%20asc [PT,L] 28 | 29 | # 30 | # Expose /ac/ URL as the AutoComplete URL and rewrite it behind the scenes 31 | # apply functions to the popularity field to affect the order in which results are returned 32 | # For more Solr functions see http://wiki.apache.org/solr/FunctionQuery 33 | # 34 | #RewriteRule ^/ac/(.*)$ /server/ac/select?wt=json&omitHeader=true&q=${escape:$2}%20_val_:ord(popularity) [PT,L] 35 | #RewriteRule ^/ac/(.*)$ /server/ac/select?wt=json&omitHeader=true&q=${escape:$2}%20_val_:ord(popularity)^0.5 [PT,L] 36 | #RewriteRule ^/ac/(.*)$ /server/ac/select?wt=json&omitHeader=true&q=${escape:$2}%20_val_:popularity^0.5 [PT,L] 37 | #RewriteRule ^/ac/(.*)$ /server/ac/select?wt=json&omitHeader=true&q=${escape:$2}%20_val_:ord(popularity) [PT,L] 38 | #RewriteRule ^/ac/(.*)$ /server/ac/select?wt=json&omitHeader=true&q=${escape:$2}%20_val_:recip(rord(popularity),1,1000,1000) [PT,L] 39 | 40 | ## 41 | ## Explanation, bottom up, but following the request execution flow 42 | ################################################################### 43 | # * The AC JavaScript is configured to hit the /ac/.... URL. 44 | # * The URL rewrite rule rewrites that URL to the /server/ac/select... URL. 45 | # * That rewritten /server base URL is a "proxy URL" that maps to the full (but base) Solr URL. 46 | # * That full (but base) Solr URL points to wherever Solr AutoComplete instance is running. 47 | # In this example Solr with AutoComplete runs on the same server as Apache. 48 | # 49 | # In Solr clusters with multiple Solr slaves running AutoComplete instances it often 50 | # makes sense to have Apache instances running on those same servers in order to avoid 51 | # requests being proxied to a remote Solr server. 52 | -------------------------------------------------------------------------------- /bin/file-loader.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # 4 | # Usage: sh file-loader.sh 5 | # 6 | # Example: 7 | # sh file-loader.sh /tmp/suggestions.txt http://localhost:8983/solr 8 | # 9 | 10 | IN_FILE=$1 11 | SOLR_URL=$2 12 | JAR_DIR=target/dependency 13 | 14 | echo 15 | echo Loading $IN_FILE into AutoComplete backend at $SOLR_URL 16 | echo 17 | sleep 5 18 | 19 | cat $IN_FILE | java -cp target/st-AutoComplete-1.6.6.3.0-SNAPSHOT.jar:$JAR_DIR/solr-core-1.3.0.jar:$JAR_DIR/solr-common-1.3.0.jar:$JAR_DIR/commons-httpclient-4.4.1.jar:$JAR_DIR/commons-codec-1.10.jar com.sematext.autocomplete.loader.FileLoader $SOLR_URL 20 | 21 | echo 22 | wc -l $IN_FILE 23 | echo done 24 | echo 25 | -------------------------------------------------------------------------------- /bin/generate-xml.sh: -------------------------------------------------------------------------------- 1 | echo "" 2 | while read line; do 3 | echo " " 4 | done; 5 | echo "" 6 | -------------------------------------------------------------------------------- /bin/prep-classpath.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | JAR_DIR=target/dependency 4 | for jar in `ls -1 $JAR_DIR`; do 5 | CLASSPATH=$CLASSPATH:$JAR_DIR/$jar 6 | done 7 | echo "$CLASSPATH:target/st-AutoComplete-1.6.6.3.0-SNAPSHOT.jar" 8 | -------------------------------------------------------------------------------- /bin/prep-classpath.tmpl: -------------------------------------------------------------------------------- 1 | #!/bin/env bash 2 | 3 | JAR_DIR=target/dependency 4 | for jar in `ls -1 $JAR_DIR`; do 5 | CLASSPATH=$CLASSPATH:$JAR_DIR/$jar 6 | done 7 | echo "echo \$CLASSPATH:target/st-AutoComplete-__VERSION__.jar" 8 | -------------------------------------------------------------------------------- /doc/INSTALL.txt: -------------------------------------------------------------------------------- 1 | - download + install java 2 | - download jetty 3 | - download solr 4 | - cp solr war to jetty/webapp 5 | - mkdir -p solr_home/conf 6 | - cp st-AutoComplet/conf/*xml solr_home/conf/ 7 | - edit solr.xml to your liking 8 | - mkdir -p cores/coreAC-es cores/coreAC-en cores/coreAC-hr 9 | - ln -s cores/coreAC-hr/conf conf 10 | -------------------------------------------------------------------------------- /doc/README.txt: -------------------------------------------------------------------------------- 1 | ==AutoComplete Solr Setup== 2 | 3 | 1. put AC schema.xml in Solr's conf dir 4 | 2. start servlet container with Solr 5 | 3. Load AC data into AC 6 | 4. TODO: Tomo, what else is needed? 7 | 8 | ==Loading AC Suggestions== 9 | 10 | AC can be defined on a field in the "main" index or in a separate AC 11 | index. 12 | 13 | If AC lives in the "main" index, data is loaded into a special AC 14 | field as documents are sent to the main index for indexing. 15 | 16 | If AC lives as a separate AC index, this index needs to be populated 17 | with data from some data source: with a file (or CSV) loader, index 18 | loader if the data is in field in another index, or DataImportHandler 19 | if data is in a database. 20 | 21 | To load AC with data from an existing index, the "main" index field 22 | used for AC must be stored (not just indexed). The IndexLoader tool 23 | is used load data into the AC index. 24 | 25 | To load phrases from a text file into AC, use FileLoader. 26 | 27 | AC can also be loaded from a custom UpdateRequestProcessor (URP). The 28 | custom URP intercepts documents sent to the main index and posts their 29 | fields used for AC to the AC index. 30 | 31 | ==Custom Suggestion Ordering== 32 | 33 | Possible approaches: 34 | 1. Index-time document boosting 35 | 2. Function query 36 | 2.1. ExternalFileField 37 | 3. Sorting 38 | 39 | ## Using Index-time Document Boosting 40 | 41 | Index-time Document Boosting is good when you have a numeric value 42 | associated with every document and you want it to affect the ranking 43 | somewhat, but not fully override it. To use Index-time Document 44 | Boosting, use a numeric value such popularity as index-time boost 45 | factor. The higher the boost the higher the rank. For example: 46 | 47 | 48 | A Beautiful Mind 49 | A Believer Sings the Truth 50 | A Broken Frame 51 | A Che 52 | A Clockwork Orange 53 | A Clue For Scooby Doo 54 | A Concert Behind Prison Walls 55 | A Day at the Races (album) 56 | A Farewell to Arms 57 | A Kind Of Magic 58 | 59 | 60 | If data is fed using another mechanism, such as SolrJ, the equivalent 61 | method for setting Document boost should be used. 62 | 63 | The final ordering will depend on the relevance score, which will be 64 | *somewhat* affected by the boost. There are other factors that are 65 | included in relevance score computation, and a custom Similarity 66 | should be used to control ranking more precisely than a simple boost 67 | factor can provide. For instance, a custom Similarity implementation 68 | may want to override the computeNorm and lengthNorm methods and 69 | eliminate the score dependency on the number of terms in the phrase 70 | field. The DefaultSimilarity class favours shorter terms and punishes 71 | long terms, which is why with DefaultSimilarity matching phrases with 72 | equal boost will always be returned in shorter-phrases-first, 73 | longer-phrases-second, with the longest phrases last order. If this 74 | is not acceptable or desirable, a custom Similarity is needed. A more 75 | drastic measure could simply have all Similarity methods return a 76 | constant, other than the method that involves the boost factor, thus 77 | eliminating any scoring and letting the boost have absolute control 78 | over the order. (TODO: This can also be achieved by sorting on a 79 | numeric field, right Tomo?) If a custom Similarty is used, it must be 80 | set in schema.xml and AC data needs to be indexed with it. 81 | 82 | 83 | ## FunctionQuery Boosting 84 | 85 | If AC is embeded in the main index then one can't use document 86 | boosting, as it would affect hit scoring as well. However, we can use 87 | a Function Query to change suggestion ordering as follows. We set the 88 | popularity of the phrase in a separate document field 89 | (e.g. "popularity") like this: 90 | 91 | 92 | A Beautiful Mind10 93 | A Believer Sings the Truth20 94 | A Broken Frame30 95 | A Che40 96 | A Clockwork Orange50 97 | A Clue For Scooby Doo60 98 | A Concert Behind Prison Walls70 99 | A Day at the Races (album)80 100 | A Farewell to Arms90 101 | A Kind Of Magic100 102 | 103 | 104 | We then change the client query and add _val_:"ord(popularity)" to the 105 | query string. An example of a YUI page using such a function query is 106 | in web/auto-complete-function-query-order.html file. 107 | 108 | If ordering is expected to change frequently, one should consider this 109 | alternative approach that keeps numerical values (like "popularity"), 110 | one for each document, in an external file that can be more frequently 111 | regenerated without requiring reindexing of the AC data: 112 | 113 | http://lucene.apache.org/solr/api/org/apache/solr/schema/ExternalFileField.html 114 | 115 | 116 | ## Sorting 117 | 118 | Sorting is a simple solution for absolute ordering. If we have 119 | popularity field set we can simply add "&sort=popularity desc". 120 | AutoComplete YUI using sort is configured in 121 | web/auto-complete-sort.html 122 | 123 | 124 | ==UI Customization== 125 | 126 | Look and feel of UI (colors and font styles) can be customized by 127 | editing css/autocomplete.css, also many functional parameters can be 128 | defined (customized) in autocomplete.js: 129 | 130 | ## Minimum Query Length 131 | 132 | By default, as soon as a user starts typing characters into the input 133 | element, the AutoComplete control starts batching characters for a 134 | query to the DataSource. 135 | 136 | You may increase how many characters the user must type before 137 | triggering this batching, which can help reduce load on a server, 138 | especially if the first few characters of the input string will not 139 | produce meaningful query results. 140 | 141 | // Require user to type at least 1 characters before triggering a query 142 | YAHOO.widget.AutoComplete.prototype.minQueryLength = 1; 143 | 144 | ## Maximum Results Displayed 145 | 146 | You can define maximum number of results to display in results 147 | container: 148 | 149 | // Display up to 10 results in the container 150 | YAHOO.widget.AutoComplete.prototype.maxResultsDisplayed = 10; 151 | 152 | 153 | ## Animation 154 | 155 | If you include the YUI Animation utility on your web page, you can 156 | enable animation on the transitions of the AutoComplete container 157 | element using the following code: 158 | 159 | YAHOO.widget.AutoComplete.prototype.animVert = true; 160 | 161 | YAHOO.widget.AutoComplete.prototype.animHoriz = false; 162 | 163 | YAHOO.widget.AutoComplete.prototype.animSpeed = 0.3; 164 | 165 | By default, if the Animation utility is present, the container will 166 | animate vertically, but not horizontally, over 0.3 seconds. 167 | 168 | ## Query Delay 169 | 170 | By default, AutoComplete batches user input and sends queries 0.1 171 | seconds from the last key input event. You may adjust this delay for 172 | optimum user experience and/or server load. Keep in mind that this 173 | value only reflects the delay before sending queries, and any delays 174 | in receiving query results that may be caused by server or 175 | computational latency will not be reflected in this value. This value 176 | must be a Number greater than 0. 177 | 178 | YAHOO.widget.AutoComplete.prototype.queryDelay = 0.2; 179 | 180 | ## Auto-highlight 181 | 182 | By default, when the container populates with query results, the first 183 | item in the list will be automatically highlighted for the user. Use 184 | the following code to disable this feature: 185 | 186 | YAHOO.widget.AutoComplete.prototype.autoHighlight = false; 187 | 188 | ## Use Shadow 189 | 190 | If you would like the container element to have a drop-shadow, be sure 191 | to define a class yui-ac-shadow and enable the feature with the 192 | following code 193 | 194 | YAHOO.widget.AutoComplete.prototype.useShadow = true; 195 | 196 | ## Auto-highlight 197 | 198 | By default, when the container populates with query results, the first 199 | item in the list will be automatically highlighted for the user. Use 200 | the following code to disable this feature: 201 | 202 | YAHOO.widget.AutoComplete.prototype.autoHighlight = false; 203 | 204 | For full list of common configurations go here http://developer.yahoo.com/yui/autocomplete/#configs 205 | 206 | ==Wikipedia Demo SetUp== 207 | 208 | First download all titles wikipedia dump for languages, for example: 209 | http://download.wikimedia.org/hrwiki/20080303/hrwiki-latest-all-titles-in-ns0.gz 210 | http://download.wikimedia.org/enwiki/20081008/enwiki-latest-all-titles-in-ns0.gz 211 | http://download.wikimedia.org/eswiki/20081126/eswiki-latest-all-titles-in-ns0.gz 212 | 213 | Create folders for language cores and copy config files from /conf in 214 | core/conf. Edit multicore feature in example/multicore/solr.xml to 215 | point to core dirs. Start Solr: 216 | 217 | java -Dsolr.solr.home=multicore -jar start.jar 218 | 219 | Load wikipedia all-titles dump for specific language to Solr using 220 | FileLoader (in language specific core), for example: 221 | 222 | $ for lang in hr es en; do echo $lang; cat wiki/${lang}wiki-latest-all-titles-in-ns0 | sed -e 's/_/ /g' | java -cp ac.jar com.sematext.autocomplete.loader.FileLoader http://localhost:8080/solr/coreAC-$lang; done 223 | -------------------------------------------------------------------------------- /etc/PrefixFromFile.jmx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | false 7 | true 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | false 17 | 1000 18 | 19 | 5 20 | 1 21 | 1227212655000 22 | 1227212655000 23 | false 24 | continue 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | false 34 | json 35 | = 36 | true 37 | wt 38 | 39 | 40 | false 41 | phrase 42 | = 43 | true 44 | fl 45 | 46 | 47 | false 48 | prefixUnTok:${PREFIX}%20prefixTok:${PREFIX} 49 | = 50 | true 51 | q 52 | 53 | 54 | 55 | localhost 56 | 8983 57 | 58 | 59 | /solr/select 60 | GET 61 | false 62 | true 63 | true 64 | false 65 | 66 | 67 | 68 | false 69 | 70 | 71 | 72 | 73 | 74 | PREFIX 75 | 76 | 77 | 78 | ${_StringFromFile(prefix.dat)} 79 | 80 | 81 | false 82 | 83 | 84 | 85 | 86 | false 87 | 88 | saveConfig 89 | 90 | 91 | true 92 | true 93 | true 94 | 95 | true 96 | true 97 | true 98 | true 99 | false 100 | true 101 | true 102 | false 103 | false 104 | true 105 | false 106 | false 107 | false 108 | false 109 | false 110 | 0 111 | true 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | -------------------------------------------------------------------------------- /example/etc/jetty.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | org.mortbay.jetty.Request.maxFormContentSize 18 | 1000000 19 | 20 | 21 | 22 | 23 | 24 | 25 | 27 | 28 | 10 29 | 50 30 | 10000 31 | 32 | 33 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 63 | 64 | 65 | 70 | 71 | 72 | 73 | 74 | 50000 75 | 1500 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | /contexts 134 | 1 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | /webapps 157 | false 158 | true 159 | false 160 | /etc/webdefault.xml 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | /yyyy_mm_dd.request.log 196 | 90 197 | true 198 | false 199 | GMT 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | true 208 | 209 | true 210 | 211 | 212 | 213 | -------------------------------------------------------------------------------- /example/exampledocs/just-phrases.txt: -------------------------------------------------------------------------------- 1 | # AutoComplete suggestions to put in the "phrase" AutoComplete index 2 | # field. 3 | # 4 | phrase:First Item Here 5 | phrase:Second Item Here 6 | phrase:newton 7 | phrase:new york 8 | -------------------------------------------------------------------------------- /example/exampledocs/phrases-sponsored.txt: -------------------------------------------------------------------------------- 1 | phrase:First Item Here is_sponsored:false 2 | phrase:Second Item Here is_sponsored:true 3 | phrase:Here Item is_sponsored:false 4 | -------------------------------------------------------------------------------- /example/exampledocs/phrases-urls.txt: -------------------------------------------------------------------------------- 1 | # AutoComplete suggestions to put in the "phrase" AutoComplete index 2 | # field, along with a URL associated to each phrase for storing in 3 | # the "url" field. 4 | # 5 | phrase:First Item Here url:http://example.com/First 6 | phrase:Second Item Here url:http://example.com/Second 7 | -------------------------------------------------------------------------------- /example/lib/jetty-6.1.3.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sematext/solr-autocomplete/d670e7a5c6e27fcb0a69ca694977fd596765c7de/example/lib/jetty-6.1.3.jar -------------------------------------------------------------------------------- /example/lib/jetty-util-6.1.3.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sematext/solr-autocomplete/d670e7a5c6e27fcb0a69ca694977fd596765c7de/example/lib/jetty-util-6.1.3.jar -------------------------------------------------------------------------------- /example/lib/jsp-2.1/ant-1.6.5.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sematext/solr-autocomplete/d670e7a5c6e27fcb0a69ca694977fd596765c7de/example/lib/jsp-2.1/ant-1.6.5.jar -------------------------------------------------------------------------------- /example/lib/jsp-2.1/core-3.1.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sematext/solr-autocomplete/d670e7a5c6e27fcb0a69ca694977fd596765c7de/example/lib/jsp-2.1/core-3.1.1.jar -------------------------------------------------------------------------------- /example/lib/jsp-2.1/jsp-2.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sematext/solr-autocomplete/d670e7a5c6e27fcb0a69ca694977fd596765c7de/example/lib/jsp-2.1/jsp-2.1.jar -------------------------------------------------------------------------------- /example/lib/jsp-2.1/jsp-api-2.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sematext/solr-autocomplete/d670e7a5c6e27fcb0a69ca694977fd596765c7de/example/lib/jsp-2.1/jsp-api-2.1.jar -------------------------------------------------------------------------------- /example/lib/servlet-api-2.5-6.1.3.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sematext/solr-autocomplete/d670e7a5c6e27fcb0a69ca694977fd596765c7de/example/lib/servlet-api-2.5-6.1.3.jar -------------------------------------------------------------------------------- /example/solr/collection1/conf/admin-extra.html: -------------------------------------------------------------------------------- 1 | 17 | 18 | 32 | -------------------------------------------------------------------------------- /example/solr/collection1/conf/data-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /example/solr/collection1/conf/managed-schema: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | phrase 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | -------------------------------------------------------------------------------- /example/solr/collection1/conf/solr.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 10 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /example/solr/urp/solrconfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 11 | 12 | 600000 13 | false 14 | 15 | 16 | 60000 17 | 18 | 19 | 20 | 21 | 22 | , 23 | 24 | 27 | 28 | 30 | autocomplete 31 | 32 | 34 | name,[features] 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | solr 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /example/start.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sematext/solr-autocomplete/d670e7a5c6e27fcb0a69ca694977fd596765c7de/example/start.jar -------------------------------------------------------------------------------- /package.sh: -------------------------------------------------------------------------------- 1 | #!/bin/env bash 2 | 3 | version=`grep '' pom.xml | head -1 | grep version | cut -d\> -f2 | cut -d\< -f1` 4 | 5 | rm -v st-AutoComplete-$version.zip 6 | 7 | mvn clean compile jar:jar dependency:copy-dependencies 8 | rm -v ./target/dependency/*test*jar 9 | rm -v ./target/dependency/*junit*jar 10 | rm -v ./target/dependency/*jetty*jar 11 | rm -v ./target/dependency/*morfologik*jar 12 | rm -v ./target/dependency/*servlet*jar 13 | rm -v ./target/dependency/*spatial*jar 14 | rm -v ./target/dependency/*ant*jar 15 | rm -v ./target/dependency/*zookeeper*jar 16 | 17 | cat bin/prep-classpath.tmpl | sed -e "s/__VERSION__/$version/g" > bin/prep-classpath.sh 18 | zip -r st-AutoComplete-$version.zip doc/*pdf LICENSE.pdf apache/httpd.conf example/solr/collection1/conf/schema.xml example/solr/collection1/conf/solrconfig.xml web/auto-complete.html web/css web/js target/st-AutoComplete-$version.jar target/dependency/*jar bin/prep-classpath.sh example/exampledocs -x "*.svn*" 19 | 20 | echo 21 | unzip -l st-AutoComplete-$version.zip | egrep -v '.html$|/$' 22 | 23 | echo 24 | egrep -i "error|fail|cannot|can't" pack.log 25 | echo 26 | ls -al *zip 27 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | 4 | com.sematext.autocomplete 5 | st-AutoComplete 6 | AutoComplete 7 | 1.6.7.1.1-SNAPSHOT 8 | jar 9 | Solr component for implementing suggest-as-you-type and auto-complete functionality 10 | http://sematext.com/open-source/index.html 11 | 12 | 13 | 14 | The Apache Software License, Version 2.0 15 | http://www.apache.org/licenses/LICENSE-2.0 16 | repo 17 | 18 | 19 | 20 | 21 | Sematext 22 | http://sematext.com 23 | 24 | 25 | 26 | 27 | nmtien 28 | Tien Nguyen Manh 29 | tien.nguyenmanh@sematext.com 30 | Sematext 31 | 32 | 33 | 34 | 35 | scm:git:git@github.com:sematext/solr-autocomplete.git 36 | scm:git:git@github.com:sematext/solr-autocomplete.git 37 | https://github.com/sematext/solr-autocomplete 38 | HEAD 39 | 40 | 41 | 42 | 43 | ossrh 44 | https://oss.sonatype.org/content/repositories/snapshots 45 | 46 | 47 | 48 | 49 | UTF-8 50 | UTF-8 51 | 1.8 52 | 1.8 53 | 4.12 54 | 7.1.0 55 | 1.3.7.1.0 56 | 1.12.7.1.0 57 | 58 | 59 | 60 | 61 | 62 | org.apache.maven.plugins 63 | maven-resources-plugin 64 | 3.0.2 65 | 66 | UTF-8 67 | 68 | 69 | 70 | maven-compiler-plugin 71 | 72 | ${maven.compile.source} 73 | ${maven.compile.target} 74 | 75 | 76 | 77 | org.apache.maven.plugins 78 | maven-source-plugin 79 | 3.0.1 80 | 81 | 82 | attach-sources 83 | 84 | jar-no-fork 85 | 86 | 87 | 88 | 89 | 90 | org.apache.maven.plugins 91 | maven-javadoc-plugin 92 | 2.10.4 93 | 94 | 95 | attach-sources 96 | 97 | jar 98 | 99 | 100 | -Xdoclint:none 101 | 102 | 103 | 104 | 105 | 106 | org.apache.maven.plugins 107 | maven-release-plugin 108 | 2.5.3 109 | 110 | 111 | org.sonatype.plugins 112 | nexus-staging-maven-plugin 113 | 1.6.7 114 | true 115 | 116 | ossrh 117 | https://oss.sonatype.org/ 118 | true 119 | 120 | 121 | 135 | 136 | 137 | 138 | 139 | 140 | release-sign-artifacts 141 | 142 | 143 | performRelease 144 | true 145 | 146 | 147 | 148 | 149 | 150 | org.apache.maven.plugins 151 | maven-gpg-plugin 152 | 1.6 153 | 154 | 155 | sign-artifacts 156 | verify 157 | 158 | sign 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | javax.servlet 172 | servlet-api 173 | 2.5 174 | 175 | 176 | 177 | junit 178 | junit 179 | ${junit.version} 180 | test 181 | 182 | 183 | 184 | com.sematext.solr 185 | st-ReSearcher-core 186 | ${researcher.version} 187 | 188 | 189 | 190 | com.sematext.querysegmenter 191 | st-QuerySegmenter-core 192 | ${querysegmenter.version} 193 | 194 | 195 | 196 | org.apache.solr 197 | solr-dataimporthandler 198 | ${solr.version} 199 | test 200 | 201 | 202 | 203 | org.json 204 | json 205 | 20170516 206 | 207 | 208 | 209 | 210 | 211 | java.net 212 | https://oss.sonatype.org/content/repositories/snapshots/ 213 | 214 | 215 | 216 | -------------------------------------------------------------------------------- /solr/collection1/conf/managed-schema: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | phrase 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | -------------------------------------------------------------------------------- /solr/collection1/conf/stopwords.txt: -------------------------------------------------------------------------------- 1 | # Licensed to the Apache Software Foundation (ASF) under one or more 2 | # contributor license agreements. See the NOTICE file distributed with 3 | # this work for additional information regarding copyright ownership. 4 | # The ASF licenses this file to You under the Apache License, Version 2.0 5 | # (the "License"); you may not use this file except in compliance with 6 | # the License. You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | -------------------------------------------------------------------------------- /solr/collection1/conf/synonyms.txt: -------------------------------------------------------------------------------- 1 | # The ASF licenses this file to You under the Apache License, Version 2.0 2 | # (the "License"); you may not use this file except in compliance with 3 | # the License. You may obtain a copy of the License at 4 | # 5 | # http://www.apache.org/licenses/LICENSE-2.0 6 | # 7 | # Unless required by applicable law or agreed to in writing, software 8 | # distributed under the License is distributed on an "AS IS" BASIS, 9 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | # See the License for the specific language governing permissions and 11 | # limitations under the License. 12 | 13 | #----------------------------------------------------------------------- 14 | #some test synonym mappings unlikely to appear in real input text 15 | aaafoo => aaabar 16 | bbbfoo => bbbfoo bbbbar 17 | cccfoo => cccbar cccbaz 18 | fooaaa,baraaa,bazaaa 19 | 20 | # Some synonym groups specific to this example 21 | GB,gib,gigabyte,gigabytes 22 | MB,mib,megabyte,megabytes 23 | Television, Televisions, TV, TVs 24 | #notice we use "gib" instead of "GiB" so any WordDelimiterFilter coming 25 | #after us won't split it into two words. 26 | 27 | # Synonym mappings can be used for spelling correction too 28 | pixima => pixma 29 | 30 | -------------------------------------------------------------------------------- /src/main/java/com/sematext/autocomplete/loader/CustomIndexLoader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2009 Sematext International 3 | * All Rights Reserved 4 | * 5 | * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF Sematext International 6 | * The copyright notice above does not evidence any actual or intended 7 | * publication of such source code. 8 | */ 9 | package com.sematext.autocomplete.loader; 10 | 11 | import java.io.IOException; 12 | import java.nio.file.Paths; 13 | import java.util.HashMap; 14 | import java.util.HashSet; 15 | import java.util.Map; 16 | import java.util.Set; 17 | 18 | import org.apache.lucene.index.CorruptIndexException; 19 | import org.apache.lucene.index.DirectoryReader; 20 | import org.apache.lucene.index.IndexableField; 21 | import org.apache.lucene.index.MultiFields; 22 | import org.apache.lucene.store.FSDirectory; 23 | import org.apache.lucene.util.Bits; 24 | import org.apache.solr.client.solrj.SolrClient; 25 | import org.apache.solr.client.solrj.SolrServerException; 26 | import org.apache.solr.client.solrj.impl.ConcurrentUpdateSolrClient; 27 | import org.apache.solr.client.solrj.response.QueryResponse; 28 | import org.apache.solr.client.solrj.util.ClientUtils; 29 | import org.apache.solr.common.SolrDocument; 30 | import org.apache.solr.common.SolrInputDocument; 31 | import org.apache.solr.common.SolrInputField; 32 | import org.apache.solr.common.params.MapSolrParams; 33 | import org.apache.solr.common.params.SolrParams; 34 | 35 | /** 36 | * Reads specified fields from each document in the given index and sends each field as a separate AC 37 | * document/suggestion to the AutoComplete backend. If field is marked as array field (surrounded by []) it will be 38 | * tokenized on separator and each token will be indexed as separate AC document/suggestion 39 | * 40 | * Usage: 41 | * java -Dfile.encoding=UTF8 -Dclient.encoding.override=UTF-8 -Xmx256m -Xms256m -server com.sematext.autocomplete.loader.CustomIndexLoader 42 | * /path/to/index AutoCompleteSolrUrl indexField1,acField1 indexField2,acField2 ... 43 | * 44 | * 45 | * @author sematext, http://www.sematext.com/ 46 | */ 47 | public class CustomIndexLoader { 48 | static final String PHRASE = "phrase"; 49 | static final String TYPE = "type"; 50 | 51 | static int countDistinctNewDocs = 0; 52 | static int countIgnoredOriginalDocs = 0; 53 | static int countCreatedAcDocs = 0; 54 | 55 | static boolean mergingWithOldDocsEnabled = true; 56 | 57 | static long totalSearchTime = 0; 58 | 59 | @SuppressWarnings("unused") 60 | public static void main(String[] args) throws CorruptIndexException, IOException, SolrServerException { 61 | 62 | if (args.length < 3) { 63 | System.err.println("Usage: java -Dfile.encoding=UTF8 -Dclient.encoding.override=UTF-8 -Xmx256m -Xms256m -server " + CustomIndexLoader.class.getName() 64 | + " " 65 | + " optional:-C:" 66 | + " optional:-I:" 67 | + " optional:-S:" 68 | + " optional:-DM (disables merging of docs)"); 69 | System.exit(0); 70 | } 71 | 72 | System.out.println("CustomIndexLoader starting"); 73 | DirectoryReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(args[0]))); 74 | int docs = reader.maxDoc(); 75 | SolrClient solr = new ConcurrentUpdateSolrClient.Builder(args[1]).withQueueSize(2000).withThreadCount(2).build(); 76 | Set batch = new HashSet(1000); 77 | 78 | String[] fieldNames = args[2].split(","); 79 | 80 | String[] copyAsIsFields = new String [0]; 81 | Map copyFields = new HashMap(); 82 | Map ignoreFields = new HashMap(); 83 | String separator = ""; 84 | 85 | long startTime = System.currentTimeMillis(); 86 | 87 | for (int i = 3; i < args.length; i++) { 88 | if (args[i].startsWith("-C:")) { 89 | copyAsIsFields = args[i].substring(3).split(","); 90 | } else if (args[i].startsWith("-S:")) { 91 | separator = args[i].substring(3); 92 | } else if (args[i].startsWith("-I:")) { 93 | String [] iDefs = args[i].substring(3).split(";"); 94 | 95 | for (String def : iDefs) { 96 | String [] tmp = def.split(":"); 97 | ignoreFields.put(tmp[0], tmp[1].split(",")); 98 | } 99 | } else if (args[i].startsWith("-DM")) { 100 | mergingWithOldDocsEnabled = false; 101 | } 102 | } 103 | 104 | System.out.println("CustomIndexLoader found " + docs + " documents in main index"); 105 | 106 | Bits liveDocs = MultiFields.getLiveDocs(reader); 107 | 108 | // go through all docs in the index 109 | for (int i = 0; i < docs; i++) { 110 | printCurrentStats(i, startTime, docs); 111 | 112 | // process doc only if not deleted 113 | if (liveDocs == null || liveDocs.get(i)) { 114 | copyFields.clear(); 115 | // first extract all fields which should be copied as-is 116 | for (String fieldName : copyAsIsFields) { 117 | // if there is : separator, then use only part related to main index fieldName 118 | String oldFieldName = fieldName.split(":")[0]; 119 | String [] values = reader.document(i).getValues(oldFieldName); 120 | copyFields.put(fieldName, values); 121 | } 122 | 123 | boolean excludeDoc = false; 124 | String excludeFieldName = null; 125 | String excludeFieldValue = null; 126 | for (String fieldName : ignoreFields.keySet()) { 127 | String [] values = reader.document(i).getValues(fieldName); 128 | 129 | for (String val : values) { 130 | for (String valIgnore : ignoreFields.get(fieldName)) { 131 | if (val.equals(valIgnore)) { 132 | excludeDoc = true; 133 | excludeFieldName = fieldName; 134 | excludeFieldValue = val; 135 | break; 136 | } 137 | } 138 | } 139 | } 140 | 141 | if (excludeDoc) { 142 | countIgnoredOriginalDocs++; 143 | // System.out.println("Excluding doc based on field '" + excludeFieldName + "' with value '" + excludeFieldValue + "'"); 144 | continue; 145 | } 146 | 147 | for (String fieldName : fieldNames) { 148 | 149 | boolean arrayField = fieldName.startsWith("[") && fieldName.endsWith("]"); 150 | 151 | boolean tokenizeField = fieldName.startsWith("{") && fieldName.endsWith("}"); 152 | 153 | IndexableField field = null; 154 | if (arrayField || tokenizeField) { 155 | field = reader.document(i).getField(fieldName.substring(1, fieldName.length() - 1)); 156 | } else { 157 | field = reader.document(i).getField(fieldName); 158 | } 159 | 160 | if (field != null) { 161 | 162 | String phrase = field.stringValue(); 163 | 164 | if (arrayField || tokenizeField) { 165 | 166 | String[] phrases = null; 167 | 168 | if (arrayField) { 169 | phrases = phrase.split(separator); 170 | } else { 171 | phrases = phrase.split(" "); 172 | } 173 | 174 | for (String value : phrases) { 175 | SolrInputDocument doc = fetchExistingOrCreateNewSolrDoc(solr, value.trim()); 176 | addField(doc, PHRASE, value.trim()); 177 | addField(doc, "type", fieldName.substring(1, fieldName.length() - 1)); 178 | addCopyAsIsFields(doc, copyFields); 179 | solr.add(doc); 180 | } 181 | 182 | } else { 183 | // System.out.println("Adding doc for phrase : " + phrase); 184 | SolrInputDocument doc = fetchExistingOrCreateNewSolrDoc(solr, phrase); 185 | addField(doc, PHRASE, phrase); 186 | addField(doc, TYPE, fieldName); 187 | addCopyAsIsFields(doc, copyFields); 188 | solr.add(doc); 189 | } 190 | } 191 | 192 | } 193 | } 194 | } 195 | if (!batch.isEmpty()) 196 | solr.add(batch); 197 | reader.close(); 198 | System.out.println("Optimizing..."); 199 | solr.optimize(); 200 | System.out.println("Done..."); 201 | } 202 | 203 | private static void printCurrentStats(int i, long startTime, int totalDocCount) { 204 | if (i < 1000) { 205 | if (i % 100 == 0) { 206 | if (i == 0) { 207 | System.out.println(getHeaderMessage(i, totalDocCount)); 208 | } 209 | else { 210 | System.out.println(getHeaderMessage(i, totalDocCount) + " - " + getProcessedStats() + " - " + getTimeStats(startTime, totalDocCount, i)); 211 | } 212 | } 213 | } else { 214 | if (i % 1000 == 0) { 215 | System.out.println(getHeaderMessage(i, totalDocCount) + " - " + getProcessedStats() + " - " + getTimeStats(startTime, totalDocCount, i)); 216 | } 217 | }/* else { 218 | if (i % 10000 == 0) { 219 | System.out.println(getHeaderMessage(i, totalDocCount) + " - " + getProcessedStats() + " - " + getTimeStats(startTime, totalDocCount, i)); 220 | } 221 | }*/ 222 | } 223 | 224 | private static String getProcessedStats() { 225 | return "ignored original docs = " + countIgnoredOriginalDocs + ", new AC docs prepared = " + countCreatedAcDocs + 226 | ", new first-time docs inserted into AC = " + countDistinctNewDocs; 227 | } 228 | 229 | private static String getTimeStats(long startTime, long totalDocCount, long currentDoc) { 230 | return "current duration = " + formatTime(System.currentTimeMillis() - startTime) + ", time spent in search = " + formatTime(totalSearchTime) + ", ETA = " + formatTime(estimateRemaining(System.currentTimeMillis() - startTime, totalDocCount, currentDoc)); 231 | } 232 | 233 | private static long lastCurrentDoc = 0; 234 | private static long lastCurrentDuration = -1; 235 | 236 | private static long estimateRemaining(long currentDuration, long totalDocCount, long currentDoc) { 237 | double lastPeriodDocsPerMiliSec = ((double) currentDoc - (double) lastCurrentDoc) / ((double) currentDuration - (double) lastCurrentDuration); 238 | double remainingDocs = totalDocCount - currentDoc; 239 | 240 | lastCurrentDoc = currentDoc; 241 | lastCurrentDuration = currentDuration; 242 | 243 | return (long) (remainingDocs / lastPeriodDocsPerMiliSec); 244 | } 245 | 246 | private static String formatTime(long l) { 247 | String res = ""; 248 | // convert to seconds 249 | l = l / 1000; 250 | 251 | if (l >= 60 * 60) { 252 | res += String.valueOf((l / (60 * 60))) + " hrs, "; 253 | l = l % (60 * 60); 254 | } 255 | if (l >= 60) { 256 | res += String.valueOf((l / (60))) + " min, "; 257 | l = l % (60); 258 | } 259 | res += l + " sec"; 260 | 261 | return res; 262 | } 263 | 264 | private static String getHeaderMessage(int i, long totalDocCount) { 265 | return "CustomIndexLoader processing original doc " + (i + 1) + "/" + totalDocCount; 266 | } 267 | 268 | private static void addField(SolrInputDocument doc, String name, String value) { 269 | // find if such field already exists 270 | if (doc.get(name) == null) { 271 | // System.out.println("Adding field " + name + " without previous values"); 272 | doc.addField(name, value); 273 | } else { 274 | // for some fields we can't allow multiple values, like ID field phrase, so we have to perform this check 275 | SolrInputField f = doc.get(name); 276 | for (Object val : f.getValues()) { 277 | // fix for boolean values 278 | if ((value.equalsIgnoreCase("t") && val.toString().equalsIgnoreCase("true")) || 279 | (value.equalsIgnoreCase("f") && val.toString().equalsIgnoreCase("false"))) { 280 | return; 281 | } 282 | if (value.equals(val.toString())) { 283 | // if we find such value in the doc, we will not add it again 284 | // System.out.println("Field " + name + " already contains value " + value); 285 | return; 286 | } 287 | } 288 | // System.out.println("Adding field " + name + " without new value " + value); 289 | f.addValue(value); 290 | } 291 | } 292 | 293 | private static void addField(SolrInputDocument doc, String name, String [] values) { 294 | // find if such field already exists 295 | if (doc.get(name) == null) { 296 | doc.addField(name, values); 297 | } else { 298 | // for some fields we can't allow multiple values, like ID field phrase, so we have to perform this check 299 | SolrInputField f = doc.get(name); 300 | for (String v : values) { 301 | boolean valueAlreadyIn = false; 302 | for (Object val : f.getValues()) { 303 | if (v.equals(val.toString())) { 304 | // if we find such value in the doc, we will not add it again 305 | valueAlreadyIn = true; 306 | break; 307 | } 308 | // fix for boolean values 309 | if ((v.equalsIgnoreCase("t") && val.toString().equalsIgnoreCase("true")) || 310 | (v.equalsIgnoreCase("f") && val.toString().equalsIgnoreCase("false"))) { 311 | valueAlreadyIn = true; 312 | break; 313 | } 314 | } 315 | 316 | if (!valueAlreadyIn) { 317 | f.addValue(v); 318 | } 319 | } 320 | } 321 | } 322 | 323 | private static SolrInputDocument fetchExistingOrCreateNewSolrDoc(SolrClient solr, String id) throws SolrServerException, IOException { 324 | countCreatedAcDocs++; 325 | 326 | if (!mergingWithOldDocsEnabled) { 327 | // if disabled, always use fresh document and override older docs with the same phrase 328 | return new SolrInputDocument(); 329 | } 330 | if (id.equals("")) { 331 | return new SolrInputDocument(); 332 | } 333 | 334 | Map p = new HashMap(); 335 | p.put("q", PHRASE + ":\"" + ClientUtils.escapeQueryChars(id) + "\""); 336 | 337 | long t1 = System.currentTimeMillis(); 338 | 339 | SolrParams params = new MapSolrParams(p); 340 | QueryResponse res = solr.query(params); 341 | 342 | totalSearchTime += (System.currentTimeMillis() - t1); 343 | 344 | if (res.getResults().size() == 0) { 345 | // System.out.println("Document for phrase " + id + " NOT FOUND"); 346 | countDistinctNewDocs++; 347 | return new SolrInputDocument(); 348 | } else if (res.getResults().size() == 1) { 349 | SolrDocument doc = res.getResults().get(0); 350 | SolrInputDocument tmp = new SolrInputDocument(); 351 | 352 | // System.out.println("Document for phrase " + id + " found"); 353 | 354 | for (String fieldName : doc.getFieldNames()) { 355 | tmp.addField(fieldName, doc.getFieldValue(fieldName)); 356 | } 357 | return tmp; 358 | } else { 359 | throw new IllegalStateException("Query with params : " + p + " returned more than 1 hit!"); 360 | } 361 | } 362 | 363 | private static void addCopyAsIsFields(SolrInputDocument doc, Map copyFields) { 364 | for (String fName : copyFields.keySet()) { 365 | String [] fNames = fName.split(":"); 366 | // if field has a different name in AC index than in main index, use that name, otherwise use main index name 367 | addField(doc, fNames[fNames.length - 1], copyFields.get(fName)); 368 | } 369 | } 370 | } 371 | -------------------------------------------------------------------------------- /src/main/java/com/sematext/autocomplete/loader/FileLoader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2009 Sematext International 3 | * All Rights Reserved 4 | * 5 | * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF Sematext International 6 | * The copyright notice above does not evidence any actual or intended 7 | * publication of such source code. 8 | */ 9 | package com.sematext.autocomplete.loader; 10 | 11 | import org.apache.solr.client.solrj.SolrClient; 12 | import org.apache.solr.client.solrj.SolrServerException; 13 | import org.apache.solr.client.solrj.impl.ConcurrentUpdateSolrClient; 14 | import org.apache.solr.common.SolrInputDocument; 15 | 16 | import java.io.BufferedReader; 17 | import java.io.IOException; 18 | import java.io.InputStreamReader; 19 | import java.net.MalformedURLException; 20 | 21 | /** 22 | * Reads AutoComplete items from standard input and sends them to the AutoComplete backend. 23 | * The input should be line-oriented. Values should be prefixed with field names that match the 24 | * AutoComplete backend configuration. Multiple fields should be tab-separated. For example: 25 | *
26 |  *   phrase:First Item Here       url:http://example.com/First
27 |  *   phrase:Second Item Here      url:http://example.com/Second
28 |  * 
29 | * Lines starting with the # character are skipped. 30 | * 31 | * @author sematext, http://www.sematext.com/ 32 | */ 33 | public class FileLoader { 34 | protected SolrClient solr; 35 | protected int docs = 0; 36 | 37 | public static void main(String[] args) throws IOException, SolrServerException { 38 | if (args.length < 1) { 39 | System.out.println("Usage: cat | java -Dfile.encoding=UTF8 -Dclient.encoding.override=UTF-8 -Xmx256m -Xms256m -server " + FileLoader.class.getName() + " "); 40 | System.exit(0); 41 | } 42 | FileLoader loader = new FileLoader(args[0]); 43 | loader.run(); 44 | } 45 | 46 | public FileLoader(String solrURL) throws MalformedURLException { 47 | solr = new ConcurrentUpdateSolrClient.Builder(solrURL).withQueueSize(10000).withThreadCount(2).build(); 48 | } 49 | 50 | protected void run() throws SolrServerException, IOException { 51 | BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 52 | String line = reader.readLine(); 53 | while (line != null) { 54 | if (!line.startsWith("#")) { 55 | docs++; 56 | solr.add(makeDoc(line)); 57 | if (docs % 1000 == 0) { 58 | System.out.println("Docs: " + docs); 59 | } 60 | } 61 | line = reader.readLine(); 62 | } 63 | reader.close(); 64 | System.out.println("Optimizing..."); 65 | solr.optimize(); 66 | } 67 | 68 | /** 69 | * Creates a {@link SolrInputDocument} out of the input. 70 | * @param line the input to use for constructing the {@link SolrInputDocument} 71 | * @return the populated {@link SolrInputDocument} 72 | */ 73 | protected SolrInputDocument makeDoc(String line) { 74 | SolrInputDocument doc = new SolrInputDocument(); 75 | String[] pairs = line.split("\t"); 76 | for (String pair : pairs) { 77 | String[] nv = pair.split(":", 2); 78 | doc.addField(nv[0], nv[1]); 79 | } 80 | return doc; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/com/sematext/autocomplete/loader/IndexLoader.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2009 Sematext International 3 | * All Rights Reserved 4 | * 5 | * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF Sematext International 6 | * The copyright notice above does not evidence any actual or intended 7 | * publication of such source code. 8 | */ 9 | package com.sematext.autocomplete.loader; 10 | 11 | import org.apache.lucene.index.CorruptIndexException; 12 | import org.apache.lucene.index.DirectoryReader; 13 | import org.apache.lucene.index.IndexableField; 14 | import org.apache.lucene.index.MultiFields; 15 | import org.apache.lucene.store.FSDirectory; 16 | import org.apache.lucene.util.Bits; 17 | import org.apache.solr.client.solrj.SolrClient; 18 | import org.apache.solr.client.solrj.SolrServerException; 19 | import org.apache.solr.client.solrj.impl.ConcurrentUpdateSolrClient; 20 | import org.apache.solr.common.SolrInputDocument; 21 | 22 | import java.io.IOException; 23 | import java.nio.file.Paths; 24 | import java.util.HashMap; 25 | import java.util.HashSet; 26 | import java.util.Iterator; 27 | import java.util.Map; 28 | import java.util.Set; 29 | 30 | /** 31 | * Reads AutoComplete items from specified fields in the given index and sends them to the AutoComplete backend. 32 | * Usage: 33 | * 34 | * java -Dfile.encoding=UTF8 -Dclient.encoding.override=UTF-8 -Xmx256m -Xms256m -server com.sematext.autocomplete.loader.IndexLoader 35 | * /path/to/index AutoCompleteSolrUrl indexField1,acField1 indexField2,acField2 ... 36 | * 37 | * @author sematext, http://www.sematext.com/ 38 | */ 39 | public class IndexLoader { 40 | 41 | public static void main(String[] args) throws CorruptIndexException, IOException, SolrServerException { 42 | 43 | if (args.length < 3) { 44 | System.err.println("Usage: java -Dfile.encoding=UTF8 -Dclient.encoding.override=UTF-8 -Xmx256m -Xms256m -server " + IndexLoader.class.getName() 45 | + " [indexField2,acField2 ... ]"); 46 | System.exit(0); 47 | } 48 | Map fieldMap = getFieldMapping(args, 2); 49 | DirectoryReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(args[0]))); 50 | int docs = reader.maxDoc(); 51 | SolrClient solr = new ConcurrentUpdateSolrClient.Builder(args[1]).withQueueSize(10000).withThreadCount(2).build(); 52 | Set batch = new HashSet(1000); 53 | 54 | Bits liveDocs = MultiFields.getLiveDocs(reader); 55 | 56 | // go through all docs in the index 57 | for (int i = 0; i < docs; i++) { 58 | // process doc only if not deleted 59 | if (liveDocs == null || liveDocs.get(i)) { 60 | // loop through all fields to be looked at 61 | SolrInputDocument doc = new SolrInputDocument(); 62 | Iterator iter = fieldMap.keySet().iterator(); 63 | 64 | boolean phraseFieldEmpty = false; 65 | 66 | while (iter.hasNext()) { 67 | String indexField = iter.next(); 68 | String acField = fieldMap.get(indexField); 69 | IndexableField field = reader.document(i).getField(indexField); 70 | String value = field != null ? reader.document(i).getField(indexField).stringValue() : null; 71 | 72 | if (field != null && value != null && !value.isEmpty()) { 73 | doc.addField(acField, value); 74 | } else { 75 | // not very relevant piece of info 76 | // System.err.println("Field is null or empty, skipping: " + indexField); 77 | 78 | if (acField.equalsIgnoreCase("phrase")) { 79 | System.err.println("Since AC phrase field would be null, this doc will not be created: " + reader.document(i)); 80 | phraseFieldEmpty = true; 81 | break; 82 | } 83 | } 84 | } 85 | 86 | if (!phraseFieldEmpty) { 87 | solr.add(doc); 88 | if (docs % 1000 == 0) { 89 | System.out.println("Docs: " + docs); 90 | } 91 | } 92 | } 93 | } 94 | if (!batch.isEmpty()) 95 | solr.add(batch); 96 | reader.close(); 97 | System.out.println("Optimizing..."); 98 | solr.optimize(); 99 | solr.close(); 100 | } 101 | 102 | private static Map getFieldMapping(String[] pairs, int offset) { 103 | HashMap map = new HashMap(); 104 | for (int i=offset; i 0) { 62 | resp.setContentType("text/plain"); 63 | resp.setHeader("Cache-Control", "no-cache"); 64 | result.put("Result", matches); 65 | resultSet.put("ResultSet", result); 66 | resp.getWriter().write(resultSet.toString(2)); 67 | } 68 | } catch (JSONException e) { 69 | resp.getWriter().write("\"" + e.getMessage() + "\""); 70 | } 71 | } else { 72 | resp.setStatus(HttpServletResponse.SC_NO_CONTENT); 73 | } 74 | } 75 | 76 | else if ("lookup".equals(action)) { 77 | searchString = req.getParameter("searchString").trim().toUpperCase(); 78 | 79 | if ((searchString != null) && service.get(searchString) != null) { 80 | req.setAttribute("object", service.get(searchString)); 81 | } 82 | 83 | context.getRequestDispatcher("/display.jsp").forward(req, resp); 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/main/java/com/sematext/autocomplete/solr/AcGroupResult.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) Sematext International 3 | * All Rights Reserved 4 | * 5 | * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF Sematext International 6 | * The copyright notice above does not evidence any 7 | * actual or intended publication of such source code. 8 | */ 9 | package com.sematext.autocomplete.solr; 10 | 11 | import org.apache.solr.common.SolrDocumentList; 12 | import org.apache.solr.search.DocList; 13 | 14 | public class AcGroupResult { 15 | private AcGroupingFieldValue acGroupingFieldValue; 16 | private DocList resultingDocs; 17 | private SolrDocumentList distributedResultingDocs; 18 | 19 | public DocList getResultingDocs() { 20 | return resultingDocs; 21 | } 22 | public void setResultingDocs(DocList resultingDocs) { 23 | this.resultingDocs = resultingDocs; 24 | } 25 | public SolrDocumentList getDistributedResultingDocs() { 26 | return distributedResultingDocs; 27 | } 28 | public void setDistributedResultingDocs(SolrDocumentList distributedResultingDocs) { 29 | this.distributedResultingDocs = distributedResultingDocs; 30 | } 31 | public AcGroupingFieldValue getAcGroupingFieldValue() { 32 | return acGroupingFieldValue; 33 | } 34 | public void setAcGroupingFieldValue(AcGroupingFieldValue acGroupingFieldValue) { 35 | this.acGroupingFieldValue = acGroupingFieldValue; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/sematext/autocomplete/solr/AcGroupingFieldValue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) Sematext International 3 | * All Rights Reserved 4 | * 5 | * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF Sematext International 6 | * The copyright notice above does not evidence any 7 | * actual or intended publication of such source code. 8 | */ 9 | package com.sematext.autocomplete.solr; 10 | 11 | /** 12 | * Represents data for one value of grouping field. 13 | * 14 | * @author sematext, http://www.sematext.com/ 15 | */ 16 | public class AcGroupingFieldValue { 17 | private String fieldValue; 18 | private int requestedCountOfSuggestions; 19 | 20 | public String getFieldValue() { 21 | return fieldValue; 22 | } 23 | public void setFieldValue(String fieldValue) { 24 | this.fieldValue = fieldValue; 25 | } 26 | public int getRequestedCountOfSuggestions() { 27 | return requestedCountOfSuggestions; 28 | } 29 | public void setRequestedCountOfSuggestions(int requestedCountOfSuggestions) { 30 | this.requestedCountOfSuggestions = requestedCountOfSuggestions; 31 | } 32 | } -------------------------------------------------------------------------------- /src/main/java/com/sematext/autocomplete/solr/AutoCompleteRequestHandler.java: -------------------------------------------------------------------------------- 1 | package com.sematext.autocomplete.solr; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import org.apache.solr.common.util.NamedList; 7 | import org.apache.solr.common.util.SimpleOrderedMap; 8 | import org.apache.solr.handler.RequestHandlerBase; 9 | import org.apache.solr.request.SolrQueryRequest; 10 | import org.apache.solr.response.SolrQueryResponse; 11 | 12 | import com.sematext.autocomplete.tst.AutoCompleteService; 13 | import com.sematext.autocomplete.tst.DoublyLinkedList.DLLIterator; 14 | 15 | public class AutoCompleteRequestHandler extends RequestHandlerBase { 16 | 17 | private static AutoCompleteService service; 18 | 19 | @Override 20 | @SuppressWarnings("rawtypes") 21 | public void init(NamedList arg0) { 22 | super.init(arg0); 23 | service = new AutoCompleteService("etc/hr-wiki-titles.txt"); 24 | 25 | } 26 | 27 | @Override 28 | public String getDescription() { 29 | // TODO Auto-generated method stub 30 | return null; 31 | } 32 | 33 | @Override 34 | public void handleRequestBody(SolrQueryRequest request, SolrQueryResponse response) throws Exception { 35 | 36 | String searchString = request.getParams().get("searchString"); 37 | 38 | searchString = searchString.trim(); 39 | 40 | try { 41 | DLLIterator it = service.matchPrefix(searchString).iterator(); 42 | 43 | List> matches = new ArrayList>(); 44 | 45 | while (it.hasNext()) { 46 | String word = (String) it.next(); 47 | 48 | NamedList node = new SimpleOrderedMap(); 49 | node.add("Title", word); 50 | 51 | matches.add(node); 52 | } 53 | 54 | if (matches.size() > 0) { 55 | 56 | response.add("ResultSet", matches); 57 | } 58 | } 59 | 60 | catch (RuntimeException e) { 61 | response.add("status", "failed"); 62 | } 63 | 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/com/sematext/autocomplete/solr/group/DictionaryGroupingSort.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) Sematext International 3 | * All Rights Reserved 4 | * 5 | * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF Sematext International 6 | * The copyright notice above does not evidence any 7 | * actual or intended publication of such source code. 8 | */ 9 | package com.sematext.autocomplete.solr.group; 10 | 11 | import org.apache.solr.common.params.CommonParams; 12 | import org.apache.solr.common.params.SolrParams; 13 | import org.apache.solr.common.util.NamedList; 14 | import org.apache.solr.handler.component.ResponseBuilder; 15 | 16 | import java.util.List; 17 | import java.util.ListIterator; 18 | import java.util.Map.Entry; 19 | 20 | import com.sematext.autocomplete.solr.AcGroupResult; 21 | import com.sematext.querysegmenter.GenericSegmentDictionaryMemImpl; 22 | import com.sematext.querysegmenter.QuerySegmenter; 23 | import com.sematext.querysegmenter.QuerySegmenterDefaultImpl; 24 | import com.sematext.querysegmenter.TypedSegment; 25 | 26 | public class DictionaryGroupingSort extends GroupingSort { 27 | 28 | private QuerySegmenter segmenter = new QuerySegmenterDefaultImpl(); 29 | 30 | public DictionaryGroupingSort(SolrParams additionalComponentParams) { 31 | super(additionalComponentParams); 32 | NamedList all = additionalComponentParams.toNamedList(); 33 | for (Entry entry : all) { 34 | if (entry.getKey().equals("class")) { 35 | continue; 36 | } 37 | // The dictionary name is the group name 38 | segmenter.addFileDictionary(entry.getKey(), entry.getValue().toString(), GenericSegmentDictionaryMemImpl.class); 39 | } 40 | } 41 | 42 | @Override 43 | public void sort(ResponseBuilder rb, List allGroupsResults) { 44 | 45 | // Look if some words of the query is in a dictionary 46 | String q = rb.req.getParams().get(CommonParams.Q); 47 | List segments = segmenter.segment(q); 48 | 49 | // No words from the query in any dictionary. Just keep the original ordering of groups. 50 | if (segments.isEmpty()) { 51 | return; 52 | } 53 | 54 | // TODO for now, we only elevate the first group that matches, but we might need to do the same for the others. 55 | TypedSegment typedSegment = segments.get(0); 56 | 57 | // The dictionary name is the group name 58 | String groupNameToElevate = typedSegment.getDictionaryName(); 59 | 60 | // Check if a group with the same name is there. If it's the case, remove it from list (temporarily). 61 | AcGroupResult groupToElavate = removeMatchingGroup(allGroupsResults, groupNameToElevate); 62 | 63 | // Put back group at the top of the list 64 | if (groupToElavate != null) { 65 | allGroupsResults.add(0, groupToElavate); 66 | } 67 | } 68 | 69 | /** 70 | * Retrieve the group from the list that matches the group name to elevate. 71 | * 72 | * @param groups 73 | * all groups 74 | * @param groupNameToElevate 75 | * group name to elevate 76 | * @return matching group 77 | */ 78 | private AcGroupResult removeMatchingGroup(List groups, String groupNameToElevate) { 79 | AcGroupResult groupToElavate = null; 80 | ListIterator listIterator = groups.listIterator(); 81 | while (listIterator.hasNext()) { 82 | AcGroupResult group = listIterator.next(); 83 | String groupName = group.getAcGroupingFieldValue().getFieldValue(); 84 | if (groupName.equals(groupNameToElevate)) { 85 | listIterator.remove(); 86 | groupToElavate = group; 87 | break; 88 | } 89 | } 90 | return groupToElavate; 91 | } 92 | 93 | } 94 | -------------------------------------------------------------------------------- /src/main/java/com/sematext/autocomplete/solr/group/ExamplePushToTheTopGroupingSort.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) Sematext International 3 | * All Rights Reserved 4 | * 5 | * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF Sematext International 6 | * The copyright notice above does not evidence any 7 | * actual or intended publication of such source code. 8 | */ 9 | package com.sematext.autocomplete.solr.group; 10 | 11 | import java.util.Collections; 12 | import java.util.Comparator; 13 | import java.util.List; 14 | 15 | import org.apache.solr.common.params.SolrParams; 16 | import org.apache.solr.handler.component.ResponseBuilder; 17 | 18 | import com.sematext.autocomplete.solr.AcGroupResult; 19 | 20 | public class ExamplePushToTheTopGroupingSort extends GroupingSort { 21 | private String valueToPushToTop; 22 | 23 | public ExamplePushToTheTopGroupingSort(SolrParams additionalComponentParams) { 24 | super(additionalComponentParams); 25 | 26 | valueToPushToTop = additionalComponentParams.get("X"); 27 | } 28 | 29 | @Override 30 | public void sort(ResponseBuilder rb, List allGroupsResults) { 31 | Collections.sort(allGroupsResults, new PushToTopComparator(valueToPushToTop)); 32 | } 33 | } 34 | 35 | class PushToTopComparator implements Comparator { 36 | private String x; 37 | 38 | public PushToTopComparator(String x) { 39 | this.x = x; 40 | } 41 | @Override 42 | public int compare(AcGroupResult o1, AcGroupResult o2) { 43 | if (o1.getAcGroupingFieldValue().getFieldValue().equals(o2.getAcGroupingFieldValue().getFieldValue())) { 44 | return 0; 45 | } 46 | 47 | if (o1.getAcGroupingFieldValue().getFieldValue().equalsIgnoreCase(x)) { 48 | return -1; 49 | } 50 | 51 | return 1; 52 | } 53 | } -------------------------------------------------------------------------------- /src/main/java/com/sematext/autocomplete/solr/group/ExampleResalePriceGroupingHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) Sematext International 3 | * All Rights Reserved 4 | * 5 | * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF Sematext International 6 | * The copyright notice above does not evidence any 7 | * actual or intended publication of such source code. 8 | */ 9 | package com.sematext.autocomplete.solr.group; 10 | 11 | import org.apache.solr.common.SolrDocumentList; 12 | import org.apache.solr.common.params.ModifiableSolrParams; 13 | import org.apache.solr.common.params.SolrParams; 14 | import org.apache.solr.handler.component.ResponseBuilder; 15 | import org.apache.solr.search.DocList; 16 | import org.apache.solr.search.DocListAndSet; 17 | 18 | public class ExampleResalePriceGroupingHandler extends GroupingHandler { 19 | private String resalePrice; 20 | 21 | public ExampleResalePriceGroupingHandler(String fieldName, String groupName, SolrParams additionalComponentParams) { 22 | super(fieldName, groupName, additionalComponentParams); 23 | resalePrice = getAdditionalComponentParams().get("resalePrice"); 24 | } 25 | 26 | @Override 27 | public DocListAndSet postProcessResult(ResponseBuilder rb, DocList originalResult) { 28 | // no post processing needed 29 | return null; 30 | } 31 | 32 | @Override 33 | public DocListAndSet postProcessDistributedResult(ResponseBuilder rb, SolrDocumentList originalResult) { 34 | // no post processing needed 35 | return null; 36 | } 37 | 38 | @Override 39 | public void prepareGroupQueryParams(ModifiableSolrParams params) { 40 | // filtering out anything with price greater than config param "price" 41 | params.add("fq", "resalePrice:[* TO " + resalePrice + "]"); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/sematext/autocomplete/solr/group/GroupingHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) Sematext International 3 | * All Rights Reserved 4 | * 5 | * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF Sematext International 6 | * The copyright notice above does not evidence any 7 | * actual or intended publication of such source code. 8 | */ 9 | package com.sematext.autocomplete.solr.group; 10 | 11 | import org.apache.solr.common.SolrDocumentList; 12 | import org.apache.solr.common.params.ModifiableSolrParams; 13 | import org.apache.solr.common.params.SolrParams; 14 | import org.apache.solr.handler.component.ResponseBuilder; 15 | import org.apache.solr.search.DocList; 16 | import org.apache.solr.search.DocListAndSet; 17 | 18 | /** 19 | * Adds ability to define custom parameters and custom response processing for each "AC group" query. 20 | * 21 | * @author sematext, http://www.sematext.com/ 22 | */ 23 | public abstract class GroupingHandler { 24 | private String fieldName; 25 | private String groupName; 26 | private SolrParams additionalComponentParams; 27 | 28 | public GroupingHandler(String fieldName, String groupName, SolrParams additionalComponentParams) { 29 | this.fieldName = fieldName; 30 | this.groupName = groupName; 31 | this.additionalComponentParams = additionalComponentParams; 32 | } 33 | 34 | /** 35 | * This method should populate parameters Maps and List which will be applied to solr query params before executing a query 36 | * for particular AC group. Keys in the maps should be solr parameter name, while values 37 | * in the List for each parameter name should contain all values which will be applied for this parameter. 38 | * 39 | * @param requestParams parameters of original request. They should not be modified in this method!! 40 | */ 41 | public abstract void prepareGroupQueryParams(ModifiableSolrParams requestParams); 42 | 43 | /** 44 | * Can be used to adjust resulting set after the query for particular group was executed. If no changes are done, 45 | * the method should return null. 46 | * 47 | * @param rb . 48 | * @param originalResult . 49 | * @return new DocSlice result or null if no changes 50 | */ 51 | public abstract DocListAndSet postProcessResult(ResponseBuilder rb, DocList originalResult); 52 | 53 | public abstract DocListAndSet postProcessDistributedResult(ResponseBuilder rb, SolrDocumentList originalResult); 54 | 55 | /** 56 | * Checks if particular GroupHandler should be used to additionally pre-process request or post-process result set 57 | * of some group. Group is defined by : 58 | * - fieldName by which group is created 59 | * - groupName - the value in the fieldName field 60 | * 61 | * For instance, consider this grouping definition: 62 | * <str name=”type”>book:5 dvd:3 cd:2</str> 63 | * 64 | * This method would be invoked 3 times (since there are 3 different group queries): 65 | * - fieldName = "type", groupName = "book" 66 | * - fieldName = "type", groupName = "dvd" 67 | * - fieldName = "type", groupName = "cd" 68 | * 69 | * If some GroupHandler is supposed to pre-process/post-process only the query of one particular group (say, type=book), 70 | * this method will return true only when grouping fieldName is "type" and value by which the group is formed is "book". 71 | * 72 | * @param fieldName . 73 | * @param groupName . 74 | * @return . 75 | */ 76 | public final boolean accepts(String fieldName, String groupName) { 77 | if (this.fieldName.equalsIgnoreCase(fieldName) && this.groupName.equals(groupName)) { 78 | return true; 79 | } 80 | 81 | return false; 82 | } 83 | 84 | public String getFieldName() { 85 | return fieldName; 86 | } 87 | 88 | public void setFieldName(String fieldName) { 89 | this.fieldName = fieldName; 90 | } 91 | 92 | public String getGroupName() { 93 | return groupName; 94 | } 95 | 96 | public void setGroupName(String groupName) { 97 | this.groupName = groupName; 98 | } 99 | 100 | public SolrParams getAdditionalComponentParams() { 101 | return additionalComponentParams; 102 | } 103 | 104 | public void setAdditionalComponentParams(SolrParams additionalComponentParams) { 105 | this.additionalComponentParams = additionalComponentParams; 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /src/main/java/com/sematext/autocomplete/solr/group/GroupingSort.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) Sematext International 3 | * All Rights Reserved 4 | * 5 | * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF Sematext International 6 | * The copyright notice above does not evidence any 7 | * actual or intended publication of such source code. 8 | */ 9 | package com.sematext.autocomplete.solr.group; 10 | 11 | import java.util.List; 12 | 13 | import org.apache.solr.common.params.SolrParams; 14 | import org.apache.solr.handler.component.ResponseBuilder; 15 | 16 | import com.sematext.autocomplete.solr.AcGroupResult; 17 | 18 | public abstract class GroupingSort { 19 | private SolrParams additionalComponentParams; 20 | 21 | public GroupingSort(SolrParams additionalComponentParams) { 22 | this.additionalComponentParams = additionalComponentParams; 23 | } 24 | 25 | /** 26 | * Used to define the new order in which AC result groups will be returned in the result set. 27 | * 28 | * @param rb . 29 | * @param allGroupsResults . 30 | */ 31 | public abstract void sort(ResponseBuilder rb, List allGroupsResults); 32 | 33 | public SolrParams getAdditionalComponentParams() { 34 | return additionalComponentParams; 35 | } 36 | 37 | public void setAdditionalComponentParams(SolrParams additionalComponentParams) { 38 | this.additionalComponentParams = additionalComponentParams; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/sematext/autocomplete/solr/group/LocationGroupingHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) Sematext International 3 | * All Rights Reserved 4 | * 5 | * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF Sematext International 6 | * The copyright notice above does not evidence any 7 | * actual or intended publication of such source code. 8 | */ 9 | package com.sematext.autocomplete.solr.group; 10 | 11 | 12 | import org.apache.solr.common.SolrDocumentList; 13 | import org.apache.solr.common.params.CommonParams; 14 | import org.apache.solr.common.params.ModifiableSolrParams; 15 | import org.apache.solr.common.params.SolrParams; 16 | import org.apache.solr.common.params.SpatialParams; 17 | import org.apache.solr.handler.component.ResponseBuilder; 18 | import org.apache.solr.search.DocList; 19 | import org.apache.solr.search.DocListAndSet; 20 | 21 | public class LocationGroupingHandler extends GroupingHandler { 22 | 23 | private final String fq; 24 | private final String distance; 25 | private final String sfield; 26 | 27 | public LocationGroupingHandler(String fieldName, String groupName, SolrParams additionalComponentParams) { 28 | super(fieldName, groupName, additionalComponentParams); 29 | fq = getAdditionalComponentParams().get(CommonParams.FQ); 30 | distance = getAdditionalComponentParams().get(SpatialParams.DISTANCE); 31 | sfield = getAdditionalComponentParams().get(SpatialParams.FIELD); 32 | } 33 | 34 | @Override 35 | public void prepareGroupQueryParams(ModifiableSolrParams params) { 36 | 37 | String pt = params.get(SpatialParams.POINT); 38 | if (pt == null) { 39 | return; 40 | } 41 | 42 | params.add(SpatialParams.POINT, pt); 43 | params.add(CommonParams.FQ, fq); 44 | params.add(SpatialParams.FIELD, sfield); 45 | 46 | // Use distance from config if not available in the request 47 | String d = params.get(SpatialParams.DISTANCE); 48 | if (d == null) { 49 | params.add(SpatialParams.DISTANCE, distance); 50 | } 51 | } 52 | 53 | @Override 54 | public DocListAndSet postProcessResult(ResponseBuilder rb, DocList originalResult) { 55 | // TODO Auto-generated method stub 56 | return null; 57 | } 58 | 59 | @Override 60 | public DocListAndSet postProcessDistributedResult(ResponseBuilder rb, SolrDocumentList originalResult) { 61 | // TODO Auto-generated method stub 62 | return null; 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/com/sematext/autocomplete/tst/AutoCompleteService.java: -------------------------------------------------------------------------------- 1 | package com.sematext.autocomplete.tst; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.FileNotFoundException; 5 | import java.io.FileReader; 6 | import java.io.IOException; 7 | 8 | public class AutoCompleteService { 9 | private TernarySearchTree tst = new TernarySearchTree(); 10 | 11 | public AutoCompleteService(String file) { 12 | 13 | FileReader input; 14 | BufferedReader bufRead = null; 15 | try { 16 | input = new FileReader(file); 17 | bufRead = new BufferedReader(input); 18 | 19 | String line; 20 | 21 | line = bufRead.readLine(); 22 | 23 | while (line != null) { 24 | 25 | tst.put(line.toLowerCase(), line.toLowerCase()); 26 | 27 | line = bufRead.readLine(); 28 | } 29 | 30 | } catch (FileNotFoundException e) { 31 | throw new RuntimeException(e); 32 | } catch (IOException e) { 33 | throw new RuntimeException(e); 34 | } finally { 35 | if (bufRead != null) { 36 | try { 37 | bufRead.close(); 38 | } catch (IOException e) { 39 | } 40 | } 41 | } 42 | } 43 | 44 | public DoublyLinkedList matchPrefix(String prefix) { 45 | return tst.matchPrefix(prefix); 46 | } 47 | 48 | public Object get(String key) { 49 | return tst.get(key); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/sematext/autocomplete/tst/CharUtility.java: -------------------------------------------------------------------------------- 1 | package com.sematext.autocomplete.tst; 2 | 3 | public class CharUtility { 4 | 5 | /** 6 | * Returns an int value that is negative if cCompare comes before cRef in the alphabet, zero if the two are equal, 7 | * and positive if cCompare comes after cRef in the alphabet. 8 | * 9 | * @param cCompare . 10 | * @param cRef . 11 | * 12 | * @return . 13 | */ 14 | public static int compareCharsAlphabetically(char cCompare, char cRef) { 15 | return (alphabetizeChar(cCompare) - alphabetizeChar(cRef)); 16 | } 17 | 18 | private static int alphabetizeChar(char c) { 19 | if (c < 65) 20 | return c; 21 | if (c < 89) 22 | return (2 * c) - 65; 23 | if (c < 97) 24 | return c + 24; 25 | if (c < 121) 26 | return (2 * c) - 128; 27 | return c; 28 | } 29 | 30 | public static int compareWordsAlphabetically(String wCompare, String wRef) { 31 | 32 | int i = 0; 33 | int compareCharValue = -1; 34 | 35 | do { 36 | compareCharValue = compareCharsAlphabetically(wCompare.charAt(i), wRef.charAt(i)); 37 | i++; 38 | 39 | } while (i < wCompare.length() && i < wRef.length() && compareCharValue == 0); 40 | 41 | // word is larger if it is longer (and same in prefix) 42 | if (compareCharValue == 0 && wCompare.length() < wRef.length()) { 43 | return (0 - alphabetizeChar(wRef.charAt(i++))); 44 | } 45 | 46 | // word is larger if it is longer (and same in prefix) 47 | if (compareCharValue == 0 && wCompare.length() > wRef.length()) { 48 | return alphabetizeChar(wCompare.charAt(i++)); 49 | } 50 | 51 | return compareCharValue; 52 | } 53 | 54 | public static int compareWordsByHash(String wCompare, String wRef) { 55 | return wCompare.hashCode() - wRef.hashCode(); 56 | 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/sematext/autocomplete/tst/DoublyLinkedList.java: -------------------------------------------------------------------------------- 1 | package com.sematext.autocomplete.tst; 2 | 3 | import java.util.NoSuchElementException; 4 | 5 | public class DoublyLinkedList { 6 | 7 | private DLLNode head, last; 8 | private int size = 0; 9 | 10 | public void addFirst(Object data) { 11 | DLLNode newNode = new DLLNode(); 12 | newNode.data = data; 13 | if (size == 0) { 14 | head = newNode; 15 | last = head; 16 | } else { 17 | newNode.nextNode = head; 18 | head.previousNode = newNode; 19 | head = newNode; 20 | } 21 | size++; 22 | } 23 | 24 | public void addLast(Object data) { 25 | DLLNode newNode = new DLLNode(); 26 | newNode.data = data; 27 | if (size == 0) { 28 | head = newNode; 29 | } else { 30 | last.nextNode = newNode; 31 | newNode.previousNode = last; 32 | } 33 | last = newNode; 34 | size++; 35 | } 36 | 37 | public void removeFirst() { 38 | if (size <= 1) { 39 | head = null; 40 | last = null; 41 | } else { 42 | DLLNode oldHead = head; 43 | head = oldHead.nextNode; 44 | oldHead.nextNode = null; 45 | head.previousNode = null; 46 | } 47 | size--; 48 | } 49 | 50 | public void removeLast() { 51 | if (size <= 1) { 52 | head = null; 53 | last = null; 54 | } else { 55 | last = last.previousNode; 56 | last.nextNode.previousNode = null; 57 | last.nextNode = null; 58 | } 59 | size--; 60 | } 61 | 62 | public int size() { 63 | return size; 64 | } 65 | 66 | public void clear() { 67 | DLLNode currentNode = last; 68 | DLLNode tempNode; 69 | while (currentNode != null) { 70 | tempNode = currentNode.previousNode; 71 | currentNode.nextNode = null; 72 | currentNode.previousNode = null; 73 | currentNode.data = null; 74 | currentNode = tempNode; 75 | } 76 | last = null; 77 | head = null; 78 | size = 0; 79 | } 80 | 81 | protected class DLLNode { 82 | protected DLLNode nextNode, previousNode; 83 | 84 | protected Object data; 85 | } 86 | 87 | public DLLIterator iterator() { 88 | return new DLLIterator(); 89 | } 90 | 91 | public class DLLIterator { 92 | 93 | private DLLNode currentPreviousNode = null; 94 | 95 | private DLLNode currentNextNode = head; 96 | 97 | public boolean hasNext() { 98 | if (currentNextNode == null) { 99 | return false; 100 | } else { 101 | return (currentNextNode != null); 102 | } 103 | } 104 | 105 | public boolean hasPrevious() { 106 | if (currentPreviousNode == null) { 107 | return false; 108 | } else { 109 | return (currentPreviousNode != null); 110 | } 111 | } 112 | 113 | public Object next() { 114 | if (currentNextNode == null) 115 | throw new NoSuchElementException( 116 | "Attempt to retrieve next value from " 117 | + "DoublyLinkedList after all values have already been retrieved. Verify hasNext method returns true " 118 | + "before calling next method."); 119 | Object data = currentNextNode.data; 120 | DLLNode tempNode = currentNextNode; 121 | currentNextNode = currentNextNode.nextNode; 122 | currentPreviousNode = tempNode; 123 | return data; 124 | } 125 | 126 | public Object previous() { 127 | if (currentPreviousNode == null) 128 | throw new NoSuchElementException("Attempt to retrieve previous value from " 129 | + "head node of DoublyLinkedList. Verify hasPrevious method returns true " 130 | + "before calling previous method."); 131 | Object data = currentPreviousNode.data; 132 | DLLNode tempNode = currentPreviousNode; 133 | currentPreviousNode = currentPreviousNode.previousNode; 134 | currentNextNode = tempNode; 135 | return data; 136 | } 137 | 138 | public void resetToBeginning() { 139 | currentNextNode = head; 140 | currentPreviousNode = null; 141 | } 142 | 143 | public void resetToEnd() { 144 | currentNextNode = null; 145 | currentPreviousNode = last; 146 | } 147 | } 148 | 149 | @Override 150 | public String toString() { 151 | // TODO Auto-generated method stub 152 | return super.toString(); 153 | } 154 | 155 | // ****************************************************************************************************************************** 156 | // ***************************************** from here on down is test code 157 | // ******************************************* 158 | // ****************************************************************************************************************************** 159 | 160 | public static class Test { 161 | public static void main(String[] args) { 162 | DoublyLinkedList testListOne = new DoublyLinkedList(); 163 | String testObjectOne = "test object one"; 164 | testListOne.addFirst(testObjectOne); 165 | System.out.println("Size after adding one object by calling addFirst: " + testListOne.size); 166 | testListOne.removeFirst(); 167 | System.out.println("Then called removeFirst and size is: " + testListOne.size); 168 | 169 | testListOne.addLast(testObjectOne); 170 | System.out.println("Size after adding one object by calling addLast: " + testListOne.size); 171 | testListOne.removeLast(); 172 | System.out.println("Then called removeLast and size is: " + testListOne.size); 173 | testListOne.clear(); 174 | testListOne.clear(); 175 | 176 | testListOne.addFirst(testObjectOne); 177 | DLLIterator iterator = testListOne.iterator(); 178 | System.out.println("hasNext method of iterator after adding one object by calling addFirst: " 179 | + iterator.hasNext()); 180 | System.out.println("hasPrevious method of iterator after adding one object by calling addFirst: " 181 | + iterator.hasPrevious()); 182 | String resultString = (String) iterator.next(); 183 | System.out.println("result string pulled out by calling next: " + resultString); 184 | System.out.println("hasNext method of iterator after calling next: " + iterator.hasNext()); 185 | System.out.println("hasPrevious method of iterator after calling next: " + iterator.hasPrevious()); 186 | resultString = (String) iterator.previous(); 187 | System.out.println("result string pulled out by calling previous: " + resultString); 188 | testListOne.clear(); 189 | 190 | System.out.println(""); 191 | 192 | String testObjectTwo = "test object two"; 193 | String testObjectThree = "test object three"; 194 | 195 | testListOne.addFirst(testObjectTwo); 196 | testListOne.addFirst(testObjectOne); 197 | iterator.resetToBeginning(); 198 | while (iterator.hasNext()) 199 | System.out.println((String) iterator.next()); 200 | testListOne.clear(); 201 | 202 | System.out.println(""); 203 | 204 | testListOne.addLast(testObjectOne); 205 | testListOne.addLast(testObjectTwo); 206 | iterator.resetToBeginning(); 207 | while (iterator.hasNext()) 208 | System.out.println((String) iterator.next()); 209 | testListOne.clear(); 210 | 211 | System.out.println(""); 212 | 213 | testListOne.addFirst(testObjectThree); 214 | testListOne.addFirst(testObjectTwo); 215 | testListOne.addFirst(testObjectOne); 216 | iterator.resetToBeginning(); 217 | while (iterator.hasNext()) 218 | System.out.println((String) iterator.next()); 219 | testListOne.clear(); 220 | 221 | System.out.println(""); 222 | 223 | testListOne.addLast(testObjectOne); 224 | testListOne.addLast(testObjectTwo); 225 | testListOne.addLast(testObjectThree); 226 | iterator.resetToBeginning(); 227 | while (iterator.hasNext()) 228 | System.out.println((String) iterator.next()); 229 | testListOne.clear(); 230 | 231 | System.out.println(""); 232 | 233 | testListOne.addFirst(testObjectTwo); 234 | testListOne.addFirst(testObjectOne); 235 | iterator.resetToEnd(); 236 | while (iterator.hasPrevious()) 237 | System.out.println((String) iterator.previous()); 238 | testListOne.clear(); 239 | 240 | System.out.println(""); 241 | 242 | testListOne.addFirst(testObjectThree); 243 | testListOne.addFirst(testObjectTwo); 244 | testListOne.addFirst(testObjectOne); 245 | System.out.println("size after adding three objects: " + testListOne.size()); 246 | iterator.resetToEnd(); 247 | while (iterator.hasPrevious()) 248 | System.out.println((String) iterator.previous()); 249 | testListOne.clear(); 250 | 251 | System.out.println(""); 252 | } 253 | } 254 | } 255 | -------------------------------------------------------------------------------- /src/main/java/com/sematext/autocomplete/urp/AutocompleteUpdateRequestProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2007-2009 Sematext International 3 | * All Rights Reserved 4 | * 5 | * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF Sematext International 6 | * The copyright notice above does not evidence any actual or intended 7 | * publication of such source code. 8 | */ 9 | package com.sematext.autocomplete.urp; 10 | 11 | import org.apache.solr.client.solrj.SolrClient; 12 | import org.apache.solr.client.solrj.SolrServerException; 13 | import org.apache.solr.client.solrj.response.QueryResponse; 14 | import org.apache.solr.client.solrj.util.ClientUtils; 15 | import org.apache.solr.common.SolrDocument; 16 | import org.apache.solr.common.SolrInputDocument; 17 | import org.apache.solr.common.SolrInputField; 18 | import org.apache.solr.common.params.MapSolrParams; 19 | import org.apache.solr.common.params.SolrParams; 20 | import org.apache.solr.update.AddUpdateCommand; 21 | import org.apache.solr.update.processor.UpdateRequestProcessor; 22 | import org.slf4j.Logger; 23 | import org.slf4j.LoggerFactory; 24 | 25 | import java.io.IOException; 26 | import java.util.Collection; 27 | import java.util.HashMap; 28 | import java.util.List; 29 | import java.util.Map; 30 | 31 | public class AutocompleteUpdateRequestProcessor extends UpdateRequestProcessor { 32 | private static final Logger LOG = LoggerFactory.getLogger(AutocompleteUpdateRequestProcessor.class); 33 | 34 | static final String PHRASE = "phrase"; 35 | static final String TYPE = "type"; 36 | 37 | private SolrClient solrAC; 38 | private List fields; 39 | private List copyAsIsFields; 40 | private String separator; 41 | 42 | public AutocompleteUpdateRequestProcessor(SolrClient solrAC, List fields, List copyAsIsFields, String separator, UpdateRequestProcessor next) { 43 | super(next); 44 | this.solrAC = solrAC; 45 | this.fields = fields; 46 | this.copyAsIsFields = copyAsIsFields; 47 | this.separator = separator; 48 | } 49 | 50 | @Override 51 | public void processAdd(AddUpdateCommand cmd) throws IOException { 52 | SolrInputField [] copyAsIsFieldsValues = null; 53 | 54 | if (copyAsIsFields.size() > 0) { 55 | copyAsIsFieldsValues = new SolrInputField[copyAsIsFields.size()]; 56 | } 57 | 58 | SolrInputDocument doc = cmd.getSolrInputDocument(); 59 | 60 | // first extract all fields which should be copied as-is 61 | int index = 0; 62 | for (String fieldName : copyAsIsFields) { 63 | SolrInputField field = doc.getField(fieldName); 64 | 65 | if (field != null) { 66 | copyAsIsFieldsValues[index++] = field; 67 | } 68 | } 69 | 70 | try { 71 | for (String fieldName : fields) { 72 | 73 | boolean arrayField = fieldName.startsWith("[") && fieldName.endsWith("]"); 74 | boolean tokenizeField = fieldName.startsWith("{") && fieldName.endsWith("}"); 75 | 76 | SolrInputField field = null; 77 | if (arrayField || tokenizeField) { 78 | field = doc.getField(fieldName.substring(1, fieldName.length() - 1)); 79 | } else { 80 | field = doc.getField(fieldName); 81 | } 82 | 83 | if (field != null && field.getValue() != null) { 84 | 85 | String phrase = field.getValue().toString(); 86 | 87 | if (arrayField || tokenizeField) { 88 | 89 | String[] phrases = null; 90 | 91 | if (arrayField) { 92 | phrases = phrase.split(separator); 93 | } else { 94 | phrases = phrase.split(" "); 95 | } 96 | 97 | for (String value : phrases) { 98 | SolrInputDocument document = fetchExistingOrCreateNewSolrDoc(value.trim()); 99 | addField(document, PHRASE, decoratePhrase(value.trim(), doc)); 100 | addField(document, "type", fieldName.substring(1, fieldName.length() - 1)); 101 | addCopyAsIsFields(document, copyAsIsFieldsValues); 102 | try { 103 | solrAC.add(document); 104 | } catch (SolrServerException e) { 105 | e.printStackTrace(); 106 | } 107 | } 108 | 109 | } else { 110 | SolrInputDocument document = fetchExistingOrCreateNewSolrDoc(phrase); 111 | addField(document, PHRASE, decoratePhrase(phrase, doc)); 112 | addField(document, TYPE, fieldName); 113 | addCopyAsIsFields(document, copyAsIsFieldsValues); 114 | try { 115 | solrAC.add(document); 116 | } catch (SolrServerException e) { 117 | e.printStackTrace(); 118 | } 119 | } 120 | } 121 | } 122 | 123 | // not done any more, since users should be able to configure it as they want 124 | // solrAC.commit(); 125 | } catch (SolrServerException e) { 126 | LOG.error("Error while updating the document", e); 127 | } catch (Throwable thr) { 128 | LOG.error("Error while updating the document", thr); 129 | } 130 | 131 | super.processAdd(cmd); 132 | } 133 | 134 | /** 135 | * Can be overriden by subclasses, for instance, if AC phrase should not be just copied from 136 | * some phrase field but decorated before adding it to AC doc. Examples for decoration: 137 | * - phrase should have a prefix made from value in field authorName 138 | * - phrase should not contain any special characters 139 | * - ... 140 | * 141 | * This method is invoked once for each value found in "phrase" field from source (main index) 142 | * document. 143 | * 144 | * @param phraseFieldValue . 145 | * @param mainIndexDoc . 146 | * @return . 147 | */ 148 | protected String decoratePhrase(String phraseFieldValue, SolrInputDocument mainIndexDoc) { 149 | return phraseFieldValue; 150 | } 151 | 152 | private void addField(SolrInputDocument doc, String name, String value) { 153 | // find if such field already exists 154 | if (doc.get(name) == null) { 155 | doc.addField(name, value); 156 | } else { 157 | // for some fields we can't allow multiple values, like ID field phrase, so we have to perform this check 158 | SolrInputField f = doc.get(name); 159 | 160 | boolean valueExists = false; 161 | 162 | for (Object existingValue : f.getValues()) { 163 | if (existingValue == null && value == null) { 164 | valueExists = true; 165 | break; 166 | } 167 | if (existingValue != null && value != null && existingValue.equals(value)) { 168 | valueExists = true; 169 | break; 170 | } 171 | } 172 | 173 | if (!valueExists) { 174 | f.addValue(value); 175 | } 176 | } 177 | } 178 | 179 | private void addField(SolrInputDocument doc, String name, Collection values) { 180 | // find if such field already exists 181 | if (doc.get(name) == null) { 182 | if (values != null) { 183 | for (Object value : values) { 184 | doc.addField(name, value); 185 | } 186 | } 187 | } else { 188 | // for some fields we can't allow multiple values, like ID field phrase, so we have to perform this check 189 | SolrInputField f = doc.get(name); 190 | 191 | for (Object value : values) { 192 | boolean valueExists = false; 193 | 194 | for (Object existingValue : f.getValues()) { 195 | if (existingValue == null && value == null) { 196 | valueExists = true; 197 | break; 198 | } 199 | if (existingValue != null && value != null && existingValue.equals(value)) { 200 | valueExists = true; 201 | break; 202 | } 203 | } 204 | 205 | if (!valueExists) { 206 | f.addValue(value); 207 | } 208 | } 209 | } 210 | } 211 | 212 | private SolrInputDocument fetchExistingOrCreateNewSolrDoc(String id) throws SolrServerException, IOException { 213 | Map p = new HashMap(); 214 | p.put("q", PHRASE + ":\"" + ClientUtils.escapeQueryChars(id) + "\""); 215 | 216 | SolrParams params = new MapSolrParams(p); 217 | 218 | QueryResponse res = solrAC.query(params); 219 | 220 | if (res.getResults().size() == 0) { 221 | return new SolrInputDocument(); 222 | } else if (res.getResults().size() == 1) { 223 | SolrDocument doc = res.getResults().get(0); 224 | SolrInputDocument tmp = new SolrInputDocument(); 225 | 226 | for (String fieldName : doc.getFieldNames()) { 227 | tmp.addField(fieldName, doc.getFieldValue(fieldName)); 228 | } 229 | return tmp; 230 | } else { 231 | throw new IllegalStateException("Query with params : " + p + " returned more than 1 hit!"); 232 | } 233 | } 234 | 235 | private void addCopyAsIsFields(SolrInputDocument doc, SolrInputField[] copyAsIsFieldsValues) { 236 | if (copyAsIsFieldsValues != null) { 237 | for (SolrInputField f : copyAsIsFieldsValues) { 238 | if (f != null) { 239 | Collection values = f.getValues(); 240 | 241 | if (values != null && values.size() > 0) { 242 | addField(doc, f.getName(), values); 243 | } 244 | } 245 | } 246 | } 247 | } 248 | } 249 | -------------------------------------------------------------------------------- /src/main/java/com/sematext/autocomplete/urp/AutocompleteUpdateRequestProcessorFactory.java: -------------------------------------------------------------------------------- 1 | package com.sematext.autocomplete.urp; 2 | 3 | import org.apache.solr.client.solrj.SolrClient; 4 | import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer; 5 | import org.apache.solr.client.solrj.impl.HttpSolrClient; 6 | import org.apache.solr.common.util.NamedList; 7 | import org.apache.solr.core.SolrCore; 8 | import org.apache.solr.request.SolrQueryRequest; 9 | import org.apache.solr.response.SolrQueryResponse; 10 | import org.apache.solr.update.processor.UpdateRequestProcessor; 11 | import org.apache.solr.update.processor.UpdateRequestProcessorFactory; 12 | import org.apache.solr.util.plugin.SolrCoreAware; 13 | 14 | import java.util.ArrayList; 15 | import java.util.List; 16 | import java.util.StringTokenizer; 17 | 18 | public class AutocompleteUpdateRequestProcessorFactory extends UpdateRequestProcessorFactory implements SolrCoreAware { 19 | 20 | private String solrAC; 21 | private SolrClient solrACServer; 22 | private String separator; 23 | private SolrCore core; 24 | 25 | private List fields = new ArrayList(); 26 | private List copyAsIsFields = new ArrayList(); 27 | 28 | @Override 29 | @SuppressWarnings("rawtypes") 30 | public void init(NamedList args) { 31 | super.init(args); 32 | 33 | solrAC = (String) args.get("solrAC"); 34 | 35 | if (solrAC.startsWith("http:")) { 36 | 37 | // Used when AC core is deployed on separate Solr 38 | this.solrACServer = new HttpSolrClient.Builder(solrAC).build(); 39 | } 40 | 41 | this.separator = (String) args.get("separator"); 42 | 43 | String fieldsStr = (String) args.get("fields"); 44 | String copyAsIsFieldsStr = (String) args.get("copyAsIsFields"); 45 | 46 | if (fieldsStr == null) { 47 | throw new RuntimeException( 48 | "Can't initialize AutocompleteUpdateRequestProcessor unless fields are specified"); 49 | } 50 | 51 | StringTokenizer tok = new StringTokenizer(fieldsStr, ","); 52 | while (tok.hasMoreTokens()) { 53 | fields.add(tok.nextToken().trim()); 54 | } 55 | 56 | if (copyAsIsFieldsStr != null) { 57 | String [] fs = copyAsIsFieldsStr.split(","); 58 | for (String f : fs) { 59 | copyAsIsFields.add(f.trim()); 60 | } 61 | } 62 | } 63 | 64 | @Override 65 | public UpdateRequestProcessor getInstance(SolrQueryRequest req, SolrQueryResponse rsp, 66 | UpdateRequestProcessor nextURP) { 67 | if (this.solrACServer == null) { 68 | // Used with embedded Solr AC core; when AC core is deployed on same Solr and 'main index' 69 | this.solrACServer = new EmbeddedSolrServer(core.getCoreContainer(), solrAC); 70 | } 71 | 72 | return new AutocompleteUpdateRequestProcessor(solrACServer, fields, copyAsIsFields, separator, nextURP); 73 | } 74 | 75 | @Override 76 | public void inform(SolrCore core) { 77 | this.core = core; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/main/java/org/apache/lucene/analysis/position/PositionFilter.java: -------------------------------------------------------------------------------- 1 | package org.apache.lucene.analysis.position; 2 | 3 | /* 4 | * Licensed to the Apache Software Foundation (ASF) under one or more 5 | * contributor license agreements. See the NOTICE file distributed with 6 | * this work for additional information regarding copyright ownership. 7 | * The ASF licenses this file to You under the Apache License, Version 2.0 8 | * (the "License"); you may not use this file except in compliance with 9 | * the License. You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | import java.io.IOException; 21 | 22 | import org.apache.lucene.analysis.TokenFilter; 23 | import org.apache.lucene.analysis.TokenStream; 24 | import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute; 25 | 26 | /** Set the positionIncrement of all tokens to the "positionIncrement", 27 | * except the first return token which retains its original positionIncrement value. 28 | * The default positionIncrement value is zero. 29 | * @deprecated (4.4) PositionFilter makes {@link TokenStream} graphs inconsistent 30 | * which can cause highlighting bugs. Its main use-case being to make 31 | * QueryParser 32 | * generate boolean queries instead of phrase queries, it is now advised to use 33 | * {@code QueryParser.setAutoGeneratePhraseQueries(boolean)} 34 | * (for simple cases) or to override {@code QueryParser.newFieldQuery}. 35 | */ 36 | @Deprecated 37 | public final class PositionFilter extends TokenFilter { 38 | 39 | /** Position increment to assign to all but the first token - default = 0 */ 40 | private final int positionIncrement; 41 | 42 | /** The first token must have non-zero positionIncrement **/ 43 | private boolean firstTokenPositioned = false; 44 | 45 | private PositionIncrementAttribute posIncrAtt = addAttribute(PositionIncrementAttribute.class); 46 | 47 | /** 48 | * Constructs a PositionFilter that assigns a position increment of zero to 49 | * all but the first token from the given input stream. 50 | * 51 | * @param input the input stream 52 | */ 53 | public PositionFilter(final TokenStream input) { 54 | this(input, 0); 55 | } 56 | 57 | /** 58 | * Constructs a PositionFilter that assigns the given position increment to 59 | * all but the first token from the given input stream. 60 | * 61 | * @param input the input stream 62 | * @param positionIncrement position increment to assign to all but the first 63 | * token from the input stream 64 | */ 65 | public PositionFilter(final TokenStream input, final int positionIncrement) { 66 | super(input); 67 | if (positionIncrement < 0) { 68 | throw new IllegalArgumentException("positionIncrement may not be negative"); 69 | } 70 | this.positionIncrement = positionIncrement; 71 | } 72 | 73 | @Override 74 | public final boolean incrementToken() throws IOException { 75 | if (input.incrementToken()) { 76 | if (firstTokenPositioned) { 77 | posIncrAtt.setPositionIncrement(positionIncrement); 78 | } else { 79 | firstTokenPositioned = true; 80 | } 81 | return true; 82 | } else { 83 | return false; 84 | } 85 | } 86 | 87 | @Override 88 | public void reset() throws IOException { 89 | super.reset(); 90 | firstTokenPositioned = false; 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/main/java/org/apache/lucene/analysis/position/PositionFilterFactory.java: -------------------------------------------------------------------------------- 1 | package org.apache.lucene.analysis.position; 2 | 3 | /* 4 | * Licensed to the Apache Software Foundation (ASF) under one or more 5 | * contributor license agreements. See the NOTICE file distributed with 6 | * this work for additional information regarding copyright ownership. 7 | * The ASF licenses this file to You under the Apache License, Version 2.0 8 | * (the "License"); you may not use this file except in compliance with 9 | * the License. You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | */ 19 | 20 | import org.apache.lucene.analysis.TokenStream; 21 | import org.apache.lucene.analysis.util.TokenFilterFactory; 22 | 23 | import java.util.Map; 24 | 25 | /** 26 | * Factory for {@link PositionFilter}. 27 | * Set the positionIncrement of all tokens to the "positionIncrement", except the first return token which retains its 28 | * original positionIncrement value. The default positionIncrement value is zero. 29 | *
30 |  * <fieldType name="text_position" class="solr.TextField" positionIncrementGap="100">
31 |  *   <analyzer>
32 |  *     <tokenizer class="solr.WhitespaceTokenizerFactory"/>
33 |  *     <filter class="solr.PositionFilterFactory" positionIncrement="0"/>
34 |  *   </analyzer>
35 |  * </fieldType>
36 | * 37 | * @see org.apache.lucene.analysis.position.PositionFilter 38 | * @since solr 1.4 39 | * @deprecated (4.4) 40 | */ 41 | @Deprecated 42 | public class PositionFilterFactory extends TokenFilterFactory { 43 | private final int positionIncrement; 44 | 45 | /** 46 | * @param args . 47 | */ 48 | public PositionFilterFactory(Map args) { 49 | super(args); 50 | positionIncrement = getInt(args, "positionIncrement", 0); 51 | if (!args.isEmpty()) { 52 | throw new IllegalArgumentException("Unknown parameters: " + args); 53 | } 54 | } 55 | 56 | @Override 57 | public PositionFilter create(TokenStream input) { 58 | return new PositionFilter(input, positionIncrement); 59 | } 60 | } 61 | 62 | -------------------------------------------------------------------------------- /src/test/java/com/sematext/autocomplete/LoadPerformaceTester.java: -------------------------------------------------------------------------------- 1 | package com.sematext.autocomplete; 2 | 3 | import org.apache.solr.client.solrj.SolrClient; 4 | import org.apache.solr.client.solrj.SolrServerException; 5 | import org.apache.solr.client.solrj.impl.HttpSolrClient; 6 | import org.apache.solr.common.SolrException; 7 | import org.apache.solr.common.SolrInputDocument; 8 | 9 | import java.io.BufferedReader; 10 | import java.io.FileNotFoundException; 11 | import java.io.FileReader; 12 | import java.io.IOException; 13 | import java.net.MalformedURLException; 14 | 15 | public class LoadPerformaceTester { 16 | 17 | /** 18 | * @param args 19 | * @throws MalformedURLException 20 | */ 21 | public static void main(String[] args) throws MalformedURLException { 22 | 23 | if (args.length < 2) { 24 | System.out.println("Usage: LoadPerformanceTester "); 25 | System.exit(0); 26 | } 27 | 28 | FileReader input; 29 | SolrClient solr = new HttpSolrClient.Builder(args[1]).build(); 30 | 31 | try { 32 | input = new FileReader(args[0]); 33 | BufferedReader bufRead = new BufferedReader(input); 34 | 35 | String line = bufRead.readLine(); 36 | while (line != null) { 37 | 38 | SolrInputDocument simpleDocument = new SolrInputDocument(); 39 | simpleDocument.addField("phrase", line.toLowerCase()); 40 | 41 | try { 42 | solr.add(simpleDocument); 43 | } catch (SolrException se) { 44 | se.printStackTrace(); 45 | } catch (SolrServerException e) { 46 | e.printStackTrace(); 47 | } 48 | 49 | line = bufRead.readLine(); 50 | } 51 | 52 | solr.commit(); 53 | solr.optimize(); 54 | 55 | bufRead.close(); 56 | input.close(); 57 | solr.close(); 58 | } catch (FileNotFoundException e) { 59 | throw new RuntimeException(e); 60 | } catch (IOException e) { 61 | throw new RuntimeException(e); 62 | } catch (SolrServerException e) { 63 | e.printStackTrace(); 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/test/java/com/sematext/autocomplete/PrefixTestListCreator.java: -------------------------------------------------------------------------------- 1 | package com.sematext.autocomplete; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.BufferedWriter; 5 | import java.io.FileNotFoundException; 6 | import java.io.FileReader; 7 | import java.io.FileWriter; 8 | import java.io.IOException; 9 | import java.net.MalformedURLException; 10 | 11 | public class PrefixTestListCreator { 12 | 13 | /** 14 | * @param args 15 | * @throws MalformedURLException 16 | */ 17 | public static void main(String[] args) throws MalformedURLException { 18 | 19 | if (args.length < 4) { 20 | System.out.println("Usage: PrefixTestListCreator "); 21 | System.exit(0); 22 | } 23 | 24 | FileReader input; 25 | FileWriter output; 26 | 27 | try { 28 | input = new FileReader(args[0]); 29 | output = new FileWriter(args[1]); 30 | 31 | int min = Integer.parseInt(args[2]); 32 | int max = Integer.parseInt(args[3]); 33 | 34 | BufferedReader bufRead = new BufferedReader(input); 35 | BufferedWriter bufferedWriter = new BufferedWriter(output); 36 | 37 | String line = bufRead.readLine(); 38 | while (line != null) { 39 | 40 | String[] words = line.split(" "); 41 | 42 | for (int i = 0; i < words.length; i++) { 43 | for (int j = 0; j < words[i].length(); j++) { 44 | String prefix = words[i].substring(0, j); 45 | if (min < prefix.length() && prefix.length() < max) { 46 | bufferedWriter.write(prefix + '\n'); 47 | } 48 | } 49 | } 50 | line = bufRead.readLine(); 51 | } 52 | 53 | bufRead.close(); 54 | bufferedWriter.close(); 55 | input.close(); 56 | output.close(); 57 | 58 | } catch (FileNotFoundException e) { 59 | throw new RuntimeException(e); 60 | } catch (IOException e) { 61 | throw new RuntimeException(e); 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/test/java/com/sematext/autocomplete/solr/AutoCompleteSearchComponentTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) Sematext International 3 | * All Rights Reserved 4 | * 5 | * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF Sematext International 6 | * The copyright notice above does not evidence any 7 | * actual or intended publication of such source code. 8 | */ 9 | package com.sematext.autocomplete.solr; 10 | 11 | import org.apache.solr.SolrTestCaseJ4; 12 | import org.apache.solr.common.params.CommonParams; 13 | import org.junit.AfterClass; 14 | import org.junit.BeforeClass; 15 | import org.junit.Test; 16 | 17 | import java.io.IOException; 18 | 19 | public class AutoCompleteSearchComponentTest extends SolrTestCaseJ4 { 20 | 21 | @BeforeClass 22 | public static void beforeTests() throws Exception { 23 | // to run from IDE: 24 | // initCore("example/solr/collection1/conf/solrconfig.xml", "example/solr/collection1/conf/schema-function-query-ordering.xml", "example/solr"); 25 | 26 | // to build with maven 27 | initCore("solrconfig.xml", "schema.xml", "solr"); 28 | 29 | assertU(adoc("phrase", "elvis presley", "is_sponsored", "true", "type", "dvd")); 30 | assertU(adoc("phrase", "bob marley", "is_sponsored", "true", "type", "cd")); 31 | assertU(adoc("phrase", "bob dylan", "is_sponsored", "false", "type", "book", "resalePrice", "15.99")); 32 | assertU(adoc("phrase", "the doors", "is_sponsored", "false", "type", "dvd")); 33 | assertU(adoc("phrase", "bob marley & the wailers", "is_sponsored", "true", "type", "dvd")); 34 | assertU(adoc("phrase", "bono", "is_sponsored", "true", "type", "book")); 35 | assertU(adoc("phrase", "bob marley & the wailers 2", "is_sponsored", "false", "type", "dvd", "resalePrice", "5.00")); 36 | assertU(adoc("phrase", "bob marley & the wailers 3", "is_sponsored", "true", "type", "book", "resalePrice", "5.00")); 37 | assertU(adoc("phrase", "bono and bob marley 1", "is_sponsored", "false", "type", "cd", "resalePrice", "3.18")); 38 | assertU(adoc("phrase", "bono and bob marley 2", "is_sponsored", "true", "type", "book")); 39 | assertU(adoc("phrase", "bono and bob marley 3", "is_sponsored", "false", "type", "dvd", "resalePrice", "7.22")); 40 | assertU(adoc("phrase", "new york city", "is_sponsored", "true", "type", "book")); 41 | assertU(adoc("phrase", "newton newton", "is_sponsored", "false", "type", "dvd")); 42 | assertU(adoc("phrase", "the washington times article", "is_sponsored", "true", "type", "book")); 43 | assertU(adoc("phrase", "times in washington", "is_sponsored", "true", "type", "book")); 44 | 45 | assertU("commit", commit()); 46 | } 47 | 48 | @AfterClass 49 | public static void afterClass() throws IOException { 50 | //h.getCore().getSearcher().get().close(); 51 | } 52 | 53 | @Test 54 | public void testAcQuery() { 55 | assertQ(req(CommonParams.QT, "dismax_ac", 56 | CommonParams.DF, "prefixTok", 57 | CommonParams.Q, "bo", 58 | AutoCompleteSearchComponent.AC_GROUPING_FIELD_PARAM_NAME, "is_sponsored", 59 | AutoCompleteSearchComponent.COMPONENT_NAME, "true") 60 | ,"//result[@name='response'][@numFound='4']" 61 | ,"//result[@name='response']/doc[1]/bool[@name='is_sponsored'][.='true']" 62 | ,"//result[@name='response']/doc[2]/bool[@name='is_sponsored'][.='false']" 63 | ,"//result[@name='response']/doc[3]/bool[@name='is_sponsored'][.='false']" 64 | ,"//result[@name='response']/doc[4]/bool[@name='is_sponsored'][.='false']" 65 | ); 66 | } 67 | 68 | @Test 69 | public void testAcQueryReversedOrder() { 70 | assertQ(req(CommonParams.QT, "dismax_ac", 71 | CommonParams.DF, "prefixTok", 72 | CommonParams.Q, "bo", 73 | AutoCompleteSearchComponent.AC_GROUPING_FIELD_PARAM_NAME, "is_sponsored", 74 | AutoCompleteSearchComponent.AC_GROUPING_FIELD_DEFINITION_PARAM_NAME, "false:3 true:1", 75 | AutoCompleteSearchComponent.COMPONENT_NAME, "true") 76 | ,"//result[@name='response'][@numFound='4']" 77 | ,"//result[@name='response']/doc[1]/bool[@name='is_sponsored'][.='false']" 78 | ,"//result[@name='response']/doc[2]/bool[@name='is_sponsored'][.='false']" 79 | ,"//result[@name='response']/doc[3]/bool[@name='is_sponsored'][.='false']" 80 | ,"//result[@name='response']/doc[4]/bool[@name='is_sponsored'][.='true']" 81 | ); 82 | } 83 | 84 | @Test 85 | public void testAcQueryWithNonExistantValue() { 86 | assertQ(req(CommonParams.QT, "dismax_ac", 87 | CommonParams.DF, "prefixTok", 88 | CommonParams.Q, "bo", 89 | AutoCompleteSearchComponent.AC_GROUPING_FIELD_PARAM_NAME, "type", 90 | AutoCompleteSearchComponent.AC_GROUPING_FIELD_DEFINITION_PARAM_NAME, "dvd:3 book:1 pc:4", 91 | AutoCompleteSearchComponent.COMPONENT_NAME, "true") 92 | ,"//result[@name='response'][@numFound='4']" 93 | ,"//result[@name='response']/doc[1]/str[@name='type'][.='dvd']" 94 | ,"//result[@name='response']/doc[2]/str[@name='type'][.='dvd']" 95 | ,"//result[@name='response']/doc[3]/str[@name='type'][.='dvd']" 96 | ,"//result[@name='response']/doc[4]/str[@name='type'][.='book']" 97 | ); 98 | } 99 | 100 | @Test 101 | public void testAcQueryWithZeroRequestedCount() { 102 | assertQ(req(CommonParams.QT, "dismax_ac", 103 | CommonParams.DF, "prefixTok", 104 | CommonParams.Q, "bo", 105 | AutoCompleteSearchComponent.AC_GROUPING_FIELD_PARAM_NAME, "is_sponsored", 106 | AutoCompleteSearchComponent.AC_GROUPING_FIELD_DEFINITION_PARAM_NAME, "false:0 true:1", 107 | AutoCompleteSearchComponent.COMPONENT_NAME, "true") 108 | ,"//result[@name='response'][@numFound='1']" 109 | ,"//result[@name='response']/doc[1]/bool[@name='is_sponsored'][.='true']" 110 | ); 111 | } 112 | 113 | @Test 114 | public void testAcQuerySomeValueExistsMultipleTimes() { 115 | assertQ(req(CommonParams.QT, "dismax_ac", 116 | CommonParams.DF, "prefixTok", 117 | CommonParams.Q, "bo", 118 | AutoCompleteSearchComponent.AC_GROUPING_FIELD_PARAM_NAME, "is_sponsored", 119 | AutoCompleteSearchComponent.AC_GROUPING_FIELD_DEFINITION_PARAM_NAME, "false:3 true:1 false:2", 120 | AutoCompleteSearchComponent.COMPONENT_NAME, "true") 121 | ,"//result[@name='response'][@numFound='6']" 122 | ,"//result[@name='response']/doc[1]/bool[@name='is_sponsored'][.='false']" 123 | ,"//result[@name='response']/doc[2]/bool[@name='is_sponsored'][.='false']" 124 | ,"//result[@name='response']/doc[3]/bool[@name='is_sponsored'][.='false']" 125 | ,"//result[@name='response']/doc[4]/bool[@name='is_sponsored'][.='true']" 126 | ,"//result[@name='response']/doc[5]/bool[@name='is_sponsored'][.='false']" 127 | ,"//result[@name='response']/doc[6]/bool[@name='is_sponsored'][.='false']" 128 | ); 129 | } 130 | 131 | @Test 132 | public void testAcBoostFullWords() { 133 | assertQ(req(CommonParams.QT, "dismax_ac", 134 | CommonParams.DF, "prefixTok", 135 | CommonParams.Q, "new", 136 | AutoCompleteSearchComponent.COMPONENT_NAME, "true") 137 | ,"//result[@name='response'][@numFound='2']" 138 | ,"//result[@name='response']/doc[1]/str[@name='phrase'][.='newton newton']" 139 | ,"//result[@name='response']/doc[2]/str[@name='phrase'][.='new york city']" 140 | ); 141 | 142 | assertQ(req(CommonParams.QT, "dismax_ac", 143 | CommonParams.Q, "new", 144 | CommonParams.DEBUG_QUERY, "true", 145 | AutoCompleteSearchComponent.AC_MATCH_FULL_WORDS_PARAM_NAME, "true", 146 | AutoCompleteSearchComponent.COMPONENT_NAME, "true") 147 | ,"//result[@name='response'][@numFound='2']" 148 | ,"//result[@name='response']/doc[1]/str[@name='phrase'][.='new york city']" 149 | ,"//result[@name='response']/doc[2]/str[@name='phrase'][.='newton newton']" 150 | ); 151 | } 152 | 153 | @Test 154 | public void testAcBoostCorrectWordOrdering() { 155 | assertQ(req(CommonParams.QT, "dismax_ac", 156 | CommonParams.DF, "prefixTok", 157 | CommonParams.Q, "washington tim", 158 | AutoCompleteSearchComponent.COMPONENT_NAME, "true") 159 | ,"//result[@name='response'][@numFound='2']" 160 | ,"//result[@name='response']/doc[2]/str[@name='phrase'][.='the washington times article']" 161 | ,"//result[@name='response']/doc[1]/str[@name='phrase'][.='times in washington']" 162 | ); 163 | assertQ(req(CommonParams.QT, "dismax_ac", 164 | CommonParams.Q, "washington tim", 165 | AutoCompleteSearchComponent.AC_MATCH_CORRECT_WORD_ORDERING_PARAM_NAME, "true", 166 | AutoCompleteSearchComponent.COMPONENT_NAME, "true") 167 | ,"//result[@name='response'][@numFound='2']" 168 | ,"//result[@name='response']/doc[1]/str[@name='phrase'][.='the washington times article']" 169 | ,"//result[@name='response']/doc[2]/str[@name='phrase'][.='times in washington']" 170 | ); 171 | } 172 | 173 | @Test 174 | public void testAcSpellchecking() { 175 | assertQ(req(CommonParams.QT, "dismax_ac", 176 | CommonParams.DF, "phrase", 177 | CommonParams.Q, "washington tim", 178 | CommonParams.DEBUG, "true", 179 | AutoCompleteSearchComponent.COMPONENT_NAME, "true") 180 | ,"//result[@name='response'][@numFound='2']" 181 | ,"//result[@name='response']/doc[2]/str[@name='phrase'][.='the washington times article']" 182 | ,"//result[@name='response']/doc[1]/str[@name='phrase'][.='times in washington']" 183 | ); 184 | } 185 | 186 | @Test 187 | public void testAcSpellchecking2() { 188 | assertQ(req(CommonParams.QT, "dismax_ac", 189 | CommonParams.DF, "prefixTok", 190 | CommonParams.Q, "washEngton tim", 191 | AutoCompleteSearchComponent.AC_SPELLCHECKING_PARAM_NAME, "true", 192 | AutoCompleteSearchComponent.COMPONENT_NAME, "true") 193 | ,"//result[@name='response'][@numFound='2']" 194 | ,"//result[@name='response']/doc[1]/str[@name='phrase'][.='the washington times article']" 195 | ,"//result[@name='response']/doc[2]/str[@name='phrase'][.='times in washington']" 196 | ); 197 | } 198 | 199 | @Test 200 | public void testAcBoostCorrectWordOrdering_noMlatches() { 201 | assertQ(req(CommonParams.QT, "dismax_ac", 202 | CommonParams.DF, "prefixTok", 203 | CommonParams.Q, "washeng", 204 | AutoCompleteSearchComponent.AC_MATCH_CORRECT_WORD_ORDERING_PARAM_NAME, "true", 205 | AutoCompleteSearchComponent.COMPONENT_NAME, "true") 206 | ,"//result[@name='response'][@numFound='0']" 207 | ); 208 | } 209 | 210 | @Test 211 | public void testAcGroupingWithHandler() { 212 | assertQ(req(CommonParams.QT, "dismax_ac", 213 | CommonParams.DF, "prefixTok", 214 | CommonParams.Q, "bo", 215 | AutoCompleteSearchComponent.AC_GROUPING_FIELD_PARAM_NAME, "is_sponsored", 216 | AutoCompleteSearchComponent.AC_GROUPING_FIELD_DEFINITION_PARAM_NAME, "false:3 true:1 false:2", 217 | AutoCompleteSearchComponent.COMPONENT_NAME, "true") 218 | ,"//result[@name='response'][@numFound='6']" 219 | ,"//result[@name='response']/doc[1]/bool[@name='is_sponsored'][.='false']" 220 | ,"//result[@name='response']/doc[2]/bool[@name='is_sponsored'][.='false']" 221 | ,"//result[@name='response']/doc[3]/bool[@name='is_sponsored'][.='false']" 222 | ,"//result[@name='response']/doc[4]/bool[@name='is_sponsored'][.='true']" 223 | ,"//result[@name='response']/doc[5]/bool[@name='is_sponsored'][.='false']" 224 | ,"//result[@name='response']/doc[6]/bool[@name='is_sponsored'][.='false']" 225 | 226 | ,"//result[@name='response']/doc[1]/str[@name='phrase'][.='bono and bob marley 1']" 227 | ,"//result[@name='response']/doc[2]/str[@name='phrase'][.='bono and bob marley 3']" 228 | ,"//result[@name='response']/doc[3]/str[@name='phrase'][.='bob dylan']" 229 | ); 230 | 231 | assertQ(req(CommonParams.QT, "dismax_ac_groupingHandlers", 232 | CommonParams.DF, "prefixTok", 233 | CommonParams.Q, "bo", 234 | AutoCompleteSearchComponent.AC_GROUPING_FIELD_PARAM_NAME, "is_sponsored", 235 | AutoCompleteSearchComponent.AC_GROUPING_FIELD_DEFINITION_PARAM_NAME, "false:3 true:1 false:2", 236 | AutoCompleteSearchComponent.COMPONENT_NAME, "true") 237 | ,"//result[@name='response'][@numFound='6']" 238 | ,"//result[@name='response']/doc[1]/bool[@name='is_sponsored'][.='false']" 239 | ,"//result[@name='response']/doc[2]/bool[@name='is_sponsored'][.='false']" 240 | ,"//result[@name='response']/doc[3]/bool[@name='is_sponsored'][.='false']" 241 | ,"//result[@name='response']/doc[4]/bool[@name='is_sponsored'][.='true']" 242 | ,"//result[@name='response']/doc[5]/bool[@name='is_sponsored'][.='false']" 243 | ,"//result[@name='response']/doc[6]/bool[@name='is_sponsored'][.='false']" 244 | 245 | ,"//result[@name='response']/doc[1]/str[@name='phrase'][.='bono and bob marley 1']" 246 | ,"//result[@name='response']/doc[2]/str[@name='phrase'][.='bono and bob marley 3']" 247 | ,"//result[@name='response']/doc[3]/str[@name='phrase'][.='bob marley & the wailers 2']" 248 | ); 249 | } 250 | 251 | @Test 252 | public void testAcGroupingWithSort() { 253 | assertQ(req(CommonParams.QT, "dismax_ac", 254 | CommonParams.DF, "prefixTok", 255 | CommonParams.Q, "bo", 256 | AutoCompleteSearchComponent.AC_GROUPING_FIELD_PARAM_NAME, "is_sponsored", 257 | AutoCompleteSearchComponent.AC_GROUPING_FIELD_DEFINITION_PARAM_NAME, "false:3 true:1 false:2", 258 | AutoCompleteSearchComponent.COMPONENT_NAME, "true") 259 | ,"//result[@name='response'][@numFound='6']" 260 | ,"//result[@name='response']/doc[1]/bool[@name='is_sponsored'][.='false']" 261 | ,"//result[@name='response']/doc[2]/bool[@name='is_sponsored'][.='false']" 262 | ,"//result[@name='response']/doc[3]/bool[@name='is_sponsored'][.='false']" 263 | ,"//result[@name='response']/doc[4]/bool[@name='is_sponsored'][.='true']" 264 | ,"//result[@name='response']/doc[5]/bool[@name='is_sponsored'][.='false']" 265 | ,"//result[@name='response']/doc[6]/bool[@name='is_sponsored'][.='false']" 266 | 267 | ,"//result[@name='response']/doc[1]/str[@name='phrase'][.='bono and bob marley 1']" 268 | ,"//result[@name='response']/doc[2]/str[@name='phrase'][.='bono and bob marley 3']" 269 | ,"//result[@name='response']/doc[3]/str[@name='phrase'][.='bob dylan']" 270 | ); 271 | 272 | assertQ(req(CommonParams.QT, "dismax_ac_groupingHandlers", 273 | CommonParams.DF, "prefixTok", 274 | CommonParams.Q, "bo", 275 | AutoCompleteSearchComponent.AC_GROUPING_FIELD_PARAM_NAME, "is_sponsored", 276 | AutoCompleteSearchComponent.AC_GROUPING_FIELD_DEFINITION_PARAM_NAME, "false:3 true:1 false:2", 277 | AutoCompleteSearchComponent.AC_GROUPING_SORT_PARAM_NAME, "is_sponsored:X_at_the_top", 278 | AutoCompleteSearchComponent.COMPONENT_NAME, "true") 279 | ,"//result[@name='response'][@numFound='6']" 280 | ,"//result[@name='response']/doc[1]/bool[@name='is_sponsored'][.='true']" 281 | ,"//result[@name='response']/doc[2]/bool[@name='is_sponsored'][.='false']" 282 | ,"//result[@name='response']/doc[3]/bool[@name='is_sponsored'][.='false']" 283 | ,"//result[@name='response']/doc[4]/bool[@name='is_sponsored'][.='false']" 284 | ,"//result[@name='response']/doc[5]/bool[@name='is_sponsored'][.='false']" 285 | ,"//result[@name='response']/doc[6]/bool[@name='is_sponsored'][.='false']" 286 | ); 287 | } 288 | } -------------------------------------------------------------------------------- /web/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | AutoCompleteServlet 5 | com.sematext.autocomplete.servlet.AutoCompleteServlet 6 | 7 | 8 | AutoCompleteServlet 9 | /autocomplete 10 | 11 | 12 | 30 13 | 14 | 15 | auto-complete.html 16 | 17 | 18 | -------------------------------------------------------------------------------- /web/auto-complete-function-query-order.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Customizing Remote Requests 6 | 7 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 |

Customizing Remote Requests

45 | 46 |
47 |

Solr Request Handler Auto Complete

48 | 49 |
50 | 51 | 52 | 53 |

Search:

54 |
55 | 56 |
57 |
58 | 59 | 86 | 87 | 88 | 89 | 90 | 91 | -------------------------------------------------------------------------------- /web/auto-complete-rh.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Customizing Remote Requests 6 | 7 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 |

Customizing Remote Requests

45 | 46 |
47 |

Solr Request Handler Auto Complete

48 | 49 |
50 | 51 | 52 | 53 |

Search:

54 |
55 | 56 |
57 |
58 | 59 | 86 | 87 | 88 | 89 | 90 | 91 | -------------------------------------------------------------------------------- /web/auto-complete-servlet.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Customizing Remote Requests 6 | 7 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 |

Customizing Remote Requests

45 | 46 |
47 |

Auto Complete using servlet backend.

48 | 49 |
50 | 51 |

Search:

52 |
53 | 54 |
55 |
56 | 57 | 84 | 85 | 86 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /web/auto-complete-sort.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Customizing Remote Requests 6 | 7 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 |

Customizing Remote Requests

45 | 46 |
47 |

Solr Request Handler Auto Complete

48 | 49 |
50 | 51 | 52 | 53 |

Search:

54 |
55 | 56 |
57 |
58 | 59 | 86 | 87 | 88 | 89 | 90 | 91 | -------------------------------------------------------------------------------- /web/auto-complete.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Auto-Complete Demo 6 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 27 | 28 | 29 | 30 |

Auto-Complete Demo

31 | 32 | 33 | 34 | 40 | 41 | 42 | 49 | 50 |
35 | 36 |
37 | 38 |
39 |
Select language: 43 | 48 |
51 | 52 | 53 | 156 | 157 | 158 | 159 | -------------------------------------------------------------------------------- /web/css/autocomplete.css: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2008, Yahoo! Inc. All rights reserved. 3 | Code licensed under the BSD License: 4 | http://developer.yahoo.net/yui/license.txt 5 | version: 2.6.0 6 | */ 7 | .yui-skin-sam .yui-ac{position:relative;font-family:verdana;font-size:100%;}.yui-skin-sam .yui-ac-input{position:absolute;width:100%;}.yui-skin-sam .yui-ac-container{position:absolute;top:1.6em;width:100%;}.yui-skin-sam .yui-ac-content{position:absolute;width:100%;border:1px solid #808080;background:#fff;overflow:hidden;z-index:9050;}.yui-skin-sam .yui-ac-shadow{position:absolute;margin:.3em;width:100%;background:#000;-moz-opacity:0.10;opacity:.10;filter:alpha(opacity=10);z-index:9049;}.yui-skin-sam .yui-ac iframe{opacity:0;filter:alpha(opacity=0);padding-right:.3em;padding-bottom:.3em;}.yui-skin-sam .yui-ac-content ul{margin:0;padding:0;width:100%;}.yui-skin-sam .yui-ac-content li{margin:0;padding:2px 5px;cursor:default;white-space:nowrap;list-style:none;zoom:1;}.yui-skin-sam .yui-ac-content li.yui-ac-prehighlight{background:#B3D4FF;}.yui-skin-sam .yui-ac-content li.yui-ac-highlight{background:#426FD9;color:#FFF;} 8 | -------------------------------------------------------------------------------- /web/css/fonts/fonts-min.css: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2008, Yahoo! Inc. All rights reserved. 3 | Code licensed under the BSD License: 4 | http://developer.yahoo.net/yui/license.txt 5 | version: 2.6.0 6 | */ 7 | body{font:13px/1.231 arial,helvetica,clean,sans-serif;*font-size:small;*font:x-small;}select,input,button,textarea{font:99% arial,helvetica,clean,sans-serif;}table{font-size:inherit;font:100%;}pre,code,kbd,samp,tt{font-family:monospace;*font-size:108%;line-height:100%;} -------------------------------------------------------------------------------- /web/js/animation-min.js: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2008, Yahoo! Inc. All rights reserved. 3 | Code licensed under the BSD License: 4 | http://developer.yahoo.net/yui/license.txt 5 | version: 2.6.0 6 | */ 7 | (function(){var B=YAHOO.util;var A=function(D,C,E,F){if(!D){}this.init(D,C,E,F);};A.NAME="Anim";A.prototype={toString:function(){var C=this.getEl()||{};var D=C.id||C.tagName;return(this.constructor.NAME+": "+D);},patterns:{noNegatives:/width|height|opacity|padding/i,offsetAttribute:/^((width|height)|(top|left))$/,defaultUnit:/width|height|top$|bottom$|left$|right$/i,offsetUnit:/\d+(em|%|en|ex|pt|in|cm|mm|pc)$/i},doMethod:function(C,E,D){return this.method(this.currentFrame,E,D-E,this.totalFrames);},setAttribute:function(C,E,D){if(this.patterns.noNegatives.test(C)){E=(E>0)?E:0;}B.Dom.setStyle(this.getEl(),C,E+D);},getAttribute:function(C){var E=this.getEl();var G=B.Dom.getStyle(E,C);if(G!=="auto"&&!this.patterns.offsetUnit.test(G)){return parseFloat(G);}var D=this.patterns.offsetAttribute.exec(C)||[];var H=!!(D[3]);var F=!!(D[2]);if(F||(B.Dom.getStyle(E,"position")=="absolute"&&H)){G=E["offset"+D[0].charAt(0).toUpperCase()+D[0].substr(1)];}else{G=0;}return G;},getDefaultUnit:function(C){if(this.patterns.defaultUnit.test(C)){return"px";}return"";},setRuntimeAttribute:function(D){var I;var E;var F=this.attributes;this.runtimeAttributes[D]={};var H=function(J){return(typeof J!=="undefined");};if(!H(F[D]["to"])&&!H(F[D]["by"])){return false;}I=(H(F[D]["from"]))?F[D]["from"]:this.getAttribute(D);if(H(F[D]["to"])){E=F[D]["to"];}else{if(H(F[D]["by"])){if(I.constructor==Array){E=[];for(var G=0,C=I.length;G0&&isFinite(K)){if(G.currentFrame+K>=J){K=J-(I+1);}G.currentFrame+=K;}};};YAHOO.util.Bezier=new function(){this.getPosition=function(E,D){var F=E.length;var C=[];for(var B=0;B0&&!(L[0] instanceof Array)){L=[L];}else{var K=[];for(M=0,O=L.length;M0){this.runtimeAttributes[P]=this.runtimeAttributes[P].concat(L);}this.runtimeAttributes[P][this.runtimeAttributes[P].length]=I;}else{F.setRuntimeAttribute.call(this,P);}};var B=function(G,I){var H=E.Dom.getXY(this.getEl());G=[G[0]-H[0]+I[0],G[1]-H[1]+I[1]];return G;};var D=function(G){return(typeof G!=="undefined");};E.Motion=A;})();(function(){var D=function(F,E,G,H){if(F){D.superclass.constructor.call(this,F,E,G,H);}};D.NAME="Scroll";var B=YAHOO.util;YAHOO.extend(D,B.ColorAnim);var C=D.superclass;var A=D.prototype;A.doMethod=function(E,H,F){var G=null;if(E=="scroll"){G=[this.method(this.currentFrame,H[0],F[0]-H[0],this.totalFrames),this.method(this.currentFrame,H[1],F[1]-H[1],this.totalFrames)];}else{G=C.doMethod.call(this,E,H,F);}return G;};A.getAttribute=function(E){var G=null;var F=this.getEl();if(E=="scroll"){G=[F.scrollLeft,F.scrollTop];}else{G=C.getAttribute.call(this,E);}return G;};A.setAttribute=function(E,H,G){var F=this.getEl();if(E=="scroll"){F.scrollLeft=H[0];F.scrollTop=H[1];}else{C.setAttribute.call(this,E,H,G);}};B.Scroll=D;})();YAHOO.register("animation",YAHOO.util.Anim,{version:"2.6.0",build:"1321"}); -------------------------------------------------------------------------------- /web/js/connection-min.js: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2008, Yahoo! Inc. All rights reserved. 3 | Code licensed under the BSD License: 4 | http://developer.yahoo.net/yui/license.txt 5 | version: 2.6.0 6 | */ 7 | YAHOO.util.Connect={_msxml_progid:["Microsoft.XMLHTTP","MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP"],_http_headers:{},_has_http_headers:false,_use_default_post_header:true,_default_post_header:"application/x-www-form-urlencoded; charset=UTF-8",_default_form_header:"application/x-www-form-urlencoded",_use_default_xhr_header:true,_default_xhr_header:"XMLHttpRequest",_has_default_headers:true,_default_headers:{},_isFormSubmit:false,_isFileUpload:false,_formNode:null,_sFormData:null,_poll:{},_timeOut:{},_polling_interval:50,_transaction_id:0,_submitElementValue:null,_hasSubmitListener:(function(){if(YAHOO.util.Event){YAHOO.util.Event.addListener(document,"click",function(B){var A=YAHOO.util.Event.getTarget(B);if(A.nodeName.toLowerCase()=="input"&&(A.type&&A.type.toLowerCase()=="submit")){YAHOO.util.Connect._submitElementValue=encodeURIComponent(A.name)+"="+encodeURIComponent(A.value);}});return true;}return false;})(),startEvent:new YAHOO.util.CustomEvent("start"),completeEvent:new YAHOO.util.CustomEvent("complete"),successEvent:new YAHOO.util.CustomEvent("success"),failureEvent:new YAHOO.util.CustomEvent("failure"),uploadEvent:new YAHOO.util.CustomEvent("upload"),abortEvent:new YAHOO.util.CustomEvent("abort"),_customEvents:{onStart:["startEvent","start"],onComplete:["completeEvent","complete"],onSuccess:["successEvent","success"],onFailure:["failureEvent","failure"],onUpload:["uploadEvent","upload"],onAbort:["abortEvent","abort"]},setProgId:function(A){this._msxml_progid.unshift(A);},setDefaultPostHeader:function(A){if(typeof A=="string"){this._default_post_header=A;}else{if(typeof A=="boolean"){this._use_default_post_header=A;}}},setDefaultXhrHeader:function(A){if(typeof A=="string"){this._default_xhr_header=A;}else{this._use_default_xhr_header=A;}},setPollingInterval:function(A){if(typeof A=="number"&&isFinite(A)){this._polling_interval=A;}},createXhrObject:function(F){var E,A;try{A=new XMLHttpRequest();E={conn:A,tId:F};}catch(D){for(var B=0;B=200&&D<300||D===1223){C=this.createResponseObject(F,B);if(G&&G.success){if(!G.scope){G.success(C);}else{G.success.apply(G.scope,[C]);}}this.successEvent.fire(C);if(F.successEvent){F.successEvent.fire(C);}}else{switch(D){case 12002:case 12029:case 12030:case 12031:case 12152:case 13030:C=this.createExceptionObject(F.tId,B,(A?A:false));if(G&&G.failure){if(!G.scope){G.failure(C);}else{G.failure.apply(G.scope,[C]);}}break;default:C=this.createResponseObject(F,B);if(G&&G.failure){if(!G.scope){G.failure(C);}else{G.failure.apply(G.scope,[C]);}}}this.failureEvent.fire(C);if(F.failureEvent){F.failureEvent.fire(C);}}this.releaseObject(F);C=null;},createResponseObject:function(A,G){var D={};var I={};try{var C=A.conn.getAllResponseHeaders();var F=C.split("\n");for(var E=0;E-1){A=B.options[B.selectedIndex];F[O++]=K+encodeURIComponent((A.attributes.value&&A.attributes.value.specified)?A.value:A.text);}break;case"select-multiple":if(B.selectedIndex>-1){for(D=B.selectedIndex,N=B.options.length;D');if(typeof A=="boolean"){C.src="javascript:false";}}else{C=document.createElement("iframe");C.id=B;C.name=B;}C.style.position="absolute";C.style.top="-1000px";C.style.left="-1000px";document.body.appendChild(C);},appendPostData:function(A){var D=[],B=A.split("&"),C,E;for(C=0;C0){for(H=0;H