{"id":88,"date":"2010-10-18T14:16:39","date_gmt":"2010-10-18T12:16:39","guid":{"rendered":"http:\/\/sematext.solr.pl\/?p=88"},"modified":"2020-11-10T14:17:08","modified_gmt":"2020-11-10T13:17:08","slug":"solr-and-autocomplete-part-1","status":"publish","type":"post","link":"https:\/\/solr.pl\/en\/2010\/10\/18\/solr-and-autocomplete-part-1\/","title":{"rendered":"Solr and autocomplete (part 1)"},"content":{"rendered":"<p>Almost everyone has seen how the autocomplete feature looks like. No wonder, then, Solr provides mechanisms by which we can build such functionality. In today&#8217;s entry I will show you how you can add autocomplete mechanism using faceting.<\/p>\n\n\n<!--more-->\n\n\n<h3><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-483\" title=\"Google Autocomplete\" src=\"http:\/\/solr.pl\/wp-content\/uploads\/2010\/10\/google_autocomplete2.png\" alt=\"\" width=\"641\" height=\"159\"><\/h3>\n<h3>Indeks<\/h3>\n<p>Suppose you want to show some hints to the user in the on-line store, for example you want to show products name. Suppose that our index is composed of the following fields:\n<\/p>\n<pre class=\"brush:xml\">&lt;field name=\"id\" type=\"string\" indexed=\"true\" stored=\"true\" multiValued=\"false\" required=\"true\"\/&gt;\n&lt;field name=\"name\" type=\"text\" indexed=\"true\" stored=\"true\" multiValued=\"false\" \/&gt;\n&lt;field name=\"description\" type=\"text\" indexed=\"true\" stored=\"true\" multiValued=\"false\" \/&gt;<\/pre>\n<p>A <em>text <\/em>type is defined as follows:\n<\/p>\n<pre class=\"brush:xml\">&lt;fieldType name=\"text\" class=\"solr.TextField\" positionIncrementGap=\"100\"&gt;\n &lt;analyzer&gt;\n  &lt;tokenizer class=\"solr.WhitespaceTokenizerFactory\"\/&gt;\n&nbsp; &lt;filter class=\"solr.WordDelimiterFilterFactory\" generateWordParts=\"1\" generateNumberParts=\"1\" catenateWords=\"1\" catenateNumbers=\"1\" catenateAll=\"0\" splitOnCaseChange=\"1\"\/&gt;\n&nbsp; &lt;filter class=\"solr.LowerCaseFilterFactory\"\/&gt;\n &lt;\/analyzer&gt;\n&lt;\/fieldType&gt;<\/pre>\n<h3>Configuration<\/h3>\n<p>To start, consider what you want to achieve &#8211; do we want to suggest only individual words that make up a name, or maybe full names that begin with the letters specified by the user. Depending on our choices we have to prepare the appropriate field on which we will build hints.<\/p>\n<h3>Prompting individual words that make up the name<\/h3>\n<p>In the case of single words, we should use a field that is tokenized. In our case, the field named <em>name <\/em>will be sufficient. However, note that if you want to use for example stemming you should define another type, which do not use stemming because of how this analysis operates on the contents of the field.<\/p>\n<h3>Prompting full name<\/h3>\n<p>For full names of the products suggestions we need a different field configuration &#8211; the best for this will be a untokenized field. But we can not use <em>string <\/em>based field for that. For this purpose, we define the field as follows:\n<\/p>\n<pre class=\"brush:xml\">&lt;field name=\"name_auto\" type=\"text_auto\" indexed=\"true\" stored=\"true\" multiValued=\"false\" \/&gt;<\/pre>\n<p>This type is defined as follows:\n<\/p>\n<pre class=\"brush:xml\">&lt;fieldType name=\"text_auto\" class=\"solr.TextField\"&gt;\n &lt;analyzer&gt;\n&nbsp; &lt;tokenizer class=\"solr.KeywordTokenizerFactory\"\/&gt;\n&nbsp; &lt;filter class=\"solr.LowerCaseFilterFactory\"\/&gt;\n &lt;\/analyzer&gt;\n&lt;\/fieldType&gt;<\/pre>\n<p>To not modify the format of the data we also add the appropriate definition of the copy information:\n<\/p>\n<pre class=\"brush:xml\">&lt;copyField source=\"name\" dest=\"name_auto\" \/&gt;<\/pre>\n<h3>How do we use it ?<\/h3>\n<p>To use the data we prepared we use a fairly simple query:\n<\/p>\n<pre class=\"brush:bash\">q=*:*&amp;facet=true&amp;facet.field=FIELD&amp;facet.mincount=1&amp;facet.prefix=USER_QUERY<\/pre>\n<p>Where:<\/p>\n<ul>\n<li><em>FIELD<\/em> &#8211; field on the basis of which we intend to make suggestions. In our case the field named<em><\/em> <em>name_auto<\/em>.<\/li>\n<li><em>USER_QUERY <\/em>&#8211; letters entered by the user.<\/li>\n<\/ul>\n<p>It is worth noting <em>rows=0<\/em> parameter is added here to only show the faceting result without the query results. Of course, this is not a necessity.<\/p>\n<p>An example query would look like that:\n<\/p>\n<pre class=\"brush:bash\">fl=id,name&amp;rows=0&amp;q=*:*&amp;facet=true&amp;facet.field=name_auto&amp;facet.mincount=1&amp;facet.prefix=har<\/pre>\n<p>The result of this query might look like this::\n<\/p>\n<pre class=\"brush:xml\">&lt;response&gt;\n &lt;lst name=\"responseHeader\"&gt;\n  &lt;int name=\"status\"&gt;0&lt;\/int&gt;\n  &lt;int name=\"QTime\"&gt;0&lt;\/int&gt;\n &lt;\/lst&gt;\n &lt;result name=\"response\" numFound=\"4\" start=\"0\"\/&gt;\n &lt;lst name=\"facet_counts\"&gt;\n  &lt;lst name=\"facet_queries\"\/&gt;\n  &lt;lst name=\"facet_fields\"&gt;\n   &lt;lst name=\"name_auto\"&gt;\n    &lt;int name=\"hard disk\"&gt;1&lt;\/int&gt;\n    &lt;int name=\"hard disk samsung\"&gt;1&lt;\/int&gt;\n    &lt;int name=\"hard disk seagate\"&gt;1&lt;\/int&gt;\n    &lt;int name=\"hard disk toshiba\"&gt;1&lt;\/int&gt;\n   &lt;\/lst&gt;\n  &lt;\/lst&gt;\n  &lt;lst name=\"facet_dates\"\/&gt;&lt;\/lst&gt;\n&lt;\/response&gt;<\/pre>\n<h3>Additional features<\/h3>\n<p>It is worth to mention the additional opportunities which are inherent to this method.<\/p>\n<p>The first possibility is to show the user additional information such as number of results that you get when you select an appropriate hint. If you want to show such information it will certainly be an interesting option.<\/p>\n<p>The next thing is sorting with the use of <em>facet.sort<\/em> parameter. Depending on your needs, we can sort the results by the number of documents (the default behavior, parameter set to <em>true<\/em>) or alphabetically (value set to <em>false<\/em>).<\/p>\n<p>We may limit the suggestions to those which have more results than a specified number. To take advantage of this opportunity pass in a parameter <em>facet.mincount<\/em> with the appropriate number.<\/p>\n<p>And as for me the biggest advantage of this method is the possibility of getting only those suggestions that not only match the letters that the user typed but also some other parameters, like category for example. For example, we want to show hints for the user who is in the household section of our store. We suspect that at this moment the user will not be interested in DVD-type products, and therefore we add a parameter <em>fq=department:homeAppliances<\/em> (assuming that we have such a department). After such a modified query, you do not get hints generated from the entire index, we only get those narrowed to the selected department.<\/p>\n<h3>A few words at the end<\/h3>\n<p>As other method, this one too, have its advantages and disadvantages. The advantage of this solution is its ease of use, no additional components requirement, and that the result hints can be easily narrowed to be generated only from those documents that match the query entered by the user. As a big plus is that the method includes number of result that will be shown after selecting the hint (of course with the same search parameters). For the downside is definitely need to have additional types and fields, quite limited abilities and the load caused by the use of faceting mechanism.<\/p>\n<p>The next entry about the <em>autocomplete <\/em>will try to expand on and show a further methods of generating hints using Solr.<\/p>","protected":false},"excerpt":{"rendered":"<p>Almost everyone has seen how the autocomplete feature looks like. No wonder, then, Solr provides mechanisms by which we can build such functionality. In today&#8217;s entry I will show you how you can add autocomplete mechanism using faceting.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[27],"tags":[229,175,164],"class_list":["post-88","post","type-post","status-publish","format-standard","hentry","category-solr-en","tag-autocomplete","tag-faceting-2","tag-solr-2"],"_links":{"self":[{"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/posts\/88","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/comments?post=88"}],"version-history":[{"count":1,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/posts\/88\/revisions"}],"predecessor-version":[{"id":89,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/posts\/88\/revisions\/89"}],"wp:attachment":[{"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/media?parent=88"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/categories?post=88"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/tags?post=88"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}