{"id":544,"date":"2013-03-25T13:54:50","date_gmt":"2013-03-25T12:54:50","guid":{"rendered":"http:\/\/sematext.solr.pl\/?p=544"},"modified":"2020-11-12T13:55:32","modified_gmt":"2020-11-12T12:55:32","slug":"autocomplete-on-multivalued-fields-using-faceting","status":"publish","type":"post","link":"https:\/\/solr.pl\/en\/2013\/03\/25\/autocomplete-on-multivalued-fields-using-faceting\/","title":{"rendered":"Autocomplete on multivalued fields using faceting"},"content":{"rendered":"<p>In the <a href=\"http:\/\/solr.pl\/en\/2013\/02\/25\/autocomplete-on-multivalued-fields-using-highlighting\/\">previous<\/a> blog post about auto complete on multi-valued field we discussed how highlighting can help us get the information we are interested in. We also promised that we will get back to the topic and we will show how to achieve a similar functionality with the use of Solr faceting capabilities. So, let&#8217;s do it.<\/p>\n\n\n<!--more-->\n\n\n<h2>Before we start<\/h2>\n<p>Because this post is more or less a continuation of what we&#8217;ve wrote earlier about autocomplete on multi-valued fields we recommend to read the &#8220;<a href=\"http:\/\/solr.pl\/en\/2013\/02\/25\/autocomplete-on-multivalued-fields-using-highlighting\/\"><em>Autocomplete on multivalued field using highlighting<\/em><\/a>&#8221; before reading the rest of this entry. We would also like to note, that the method shown in this entry is very similar to the one shown in the &#8220;<a href=\"http:\/\/solr.pl\/en\/2010\/10\/18\/solr-and-autocomplete-part-1\/\">Solr and autocomplete (part 1)<\/a>&#8221; post, but we wanted to refresh that topic and show the example using multi-valued fields.<\/p>\n<h2>Configuration<\/h2>\n<p>Similar to the previous post we will start with Solr configuration.<\/p>\n<h3>Index structure<\/h3>\n<p>The structure of our index is exactly the same as the one previously shown, but let&#8217;s recall it. One thing &#8211; please remember that we want to have auto complete working on multi-valued field. This field is called <em>features<\/em> and the whole index fields configuration looks like this:\n<\/p>\n<pre class=\"brush:xml\">&lt;fields&gt;\n &lt;field name=\"id\" type=\"string\" indexed=\"true\" stored=\"true\" required=\"true\" multiValued=\"false\" \/&gt;\n &lt;field name=\"features\" type=\"string\" indexed=\"true\" stored=\"true\" multiValued=\"true\"\/&gt;\n &lt;field name=\"features_autocomplete\" type=\"text_autocomplete\" indexed=\"true\" stored=\"true\" multiValued=\"true\"\/&gt;\n\n &lt;field name=\"_version_\" type=\"long\" indexed=\"true\" stored=\"true\"\/&gt;\n&lt;\/fields&gt;<\/pre>\n<p>For getting values for auto complete we will use the <em>features_autocomplete<\/em> field.<\/p>\n<h3>Copy field<\/h3>\n<p>Of course we don&#8217;t want to change our indexer and we want Solr to automatically copy the data from <em>features<\/em> field to the <em>features_autocomplete<\/em> one. Because of that we will add the <em>copyField<\/em> definition to the <em>schema.xml<\/em> file, so it looks like this:\n<\/p>\n<pre class=\"brush:xml\">&lt;copyField source=\"features\" dest=\"features_autocomplete\"\/&gt;<\/pre>\n<h3>Our text_autocomplete field type<\/h3>\n<p>And we&#8217;ve come to the first difference &#8211; the <em>text_autocomplete<\/em> field type. This time it looks like this:\n<\/p>\n<pre class=\"brush:xml\">&lt;fieldType name=\"text_autocomplete\" class=\"solr.TextField\" positionIncrementGap=\"100\"&gt;\n &lt;analyzer&gt;\n  &lt;tokenizer class=\"solr.KeywordTokenizerFactory\"\/&gt;\n  &lt;filter class=\"solr.LowerCaseFilterFactory\"\/&gt;\n &lt;\/analyzer&gt;\n&lt;\/fieldType&gt;<\/pre>\n<p>Because of the fact that we will use faceting we use the <em>solr.KeywordTokenizerFactory<\/em> with the <em>solr.LowerCaseFilterFactory<\/em> to have the data in our field as a single, lowercased token.<\/p>\n<h3>Example data<\/h3>\n<p>Our example data is identical to what we had before, but even though let&#8217;s recall them for things to be clear:\n<\/p>\n<pre class=\"brush:xml\">&lt;add&gt;\n &lt;doc&gt;\n  &lt;field name=\"id\"&gt;1&lt;\/field&gt;\n  &lt;field name=\"features\"&gt;Multiple windows&lt;\/field&gt;\n  &lt;field name=\"features\"&gt;Single door&lt;\/field&gt;\n &lt;\/doc&gt;\n &lt;doc&gt;\n  &lt;field name=\"id\"&gt;2&lt;\/field&gt;\n  &lt;field name=\"features\"&gt;Single window&lt;\/field&gt;\n  &lt;field name=\"features\"&gt;Single door&lt;\/field&gt;\n &lt;\/doc&gt;\n &lt;doc&gt;\n  &lt;field name=\"id\"&gt;3&lt;\/field&gt;\n  &lt;field name=\"features\"&gt;Multiple windows&lt;\/field&gt;\n  &lt;field name=\"features\"&gt;Multiple doors&lt;\/field&gt;\n &lt;\/doc&gt;\n&lt;\/add&gt;<\/pre>\n<h2>Query with faceting<\/h2>\n<p>Let&#8217;s look how our query will look like when we will use faceting instead of highlighting.<\/p>\n<h3>Full query<\/h3>\n<p>When using faceting our query should look more or less like the following one:\n<\/p>\n<pre class=\"brush:xml\">q=*:*&amp;rows=0&amp;facet=true&amp;facet.field=features_autocomplete&amp;facet.prefix=sing<\/pre>\n<p>A few words about the parameters:<\/p>\n<ul>\n<li><em>rows=0<\/em> &#8211; we tell Solr that we don&#8217;t want the documents that matched the query in the results,<\/li>\n<li><em>facet=true<\/em> &#8211; we inform Solr that we want to use faceting,<\/li>\n<li><em>facet.field=features_autocomplete<\/em> &#8211; we say which field will be used to calculate faceting,<\/li>\n<li><em>facet.prefix=sing<\/em> &#8211; with the use of this parameter we provide the value of a query for auto complete.<\/li>\n<\/ul>\n<h3>Query results<\/h3>\n<p>Query results returned by Solr for the above query are as follows:\n<\/p>\n<pre class=\"brush:xml\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n&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 name=\"params\"&gt;\n    &lt;str name=\"facet\"&gt;true&lt;\/str&gt;\n    &lt;str name=\"q\"&gt;*:*&lt;\/str&gt;\n    &lt;str name=\"facet.prefix\"&gt;sing&lt;\/str&gt;\n    &lt;str name=\"facet.field\"&gt;features_autocomplete&lt;\/str&gt;\n    &lt;str name=\"rows\"&gt;0&lt;\/str&gt;\n  &lt;\/lst&gt;\n&lt;\/lst&gt;\n&lt;result name=\"response\" numFound=\"3\" start=\"0\"&gt;\n&lt;\/result&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=\"features_autocomplete\"&gt;\n      &lt;int name=\"single door\"&gt;2&lt;\/int&gt;\n      &lt;int name=\"single window\"&gt;1&lt;\/int&gt;\n    &lt;\/lst&gt;\n  &lt;\/lst&gt;\n  &lt;lst name=\"facet_dates\"\/&gt;\n  &lt;lst name=\"facet_ranges\"\/&gt;\n&lt;\/lst&gt;\n&lt;\/response&gt;<\/pre>\n<p>As you can see in the field faceting section we got the phrases we were interested in along with the number of documents they appear in.<\/p>\n<h3>What to remember about<\/h3>\n<p>The crucial thing to remember is that the value provided to the <em>facet.prefix<\/em> parameter is not analyzed. Because of that if we would provide the <em>Sing<\/em> value instead of the <em>sing<\/em>we wouldn&#8217;t get the results. You should remember that.<\/p>\n<h2>A short summary<\/h2>\n<p>The above entry shown the second method used to develop auto complete functionality on multi-valued fields. Of couse we didn&#8217;t say all about the topic and we will get back to it someday, but for now that is all. We hope that someone will find it useful \ud83d\ude42<\/p>","protected":false},"excerpt":{"rendered":"<p>In the previous blog post about auto complete on multi-valued field we discussed how highlighting can help us get the information we are interested in. We also promised that we will get back to the topic and we will show<\/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,508],"class_list":["post-544","post","type-post","status-publish","format-standard","hentry","category-solr-en","tag-autocomplete","tag-faceting-2","tag-multivalued-2"],"_links":{"self":[{"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/posts\/544","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=544"}],"version-history":[{"count":1,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/posts\/544\/revisions"}],"predecessor-version":[{"id":545,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/posts\/544\/revisions\/545"}],"wp:attachment":[{"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/media?parent=544"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/categories?post=544"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solr.pl\/en\/wp-json\/wp\/v2\/tags?post=544"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}