Skip to Content

Can we create another solrindextype other than product type in same solrsearchconfig?

Hi Experts, we have requirement to index another solrindextype which is totally different from product type.We have to use solr to search the new item type and display the search result on the specific searchpage. Can we create new solrindextype and index that itemtype on the solr? Any help regarding this will be very helpful. Thanks in advance.

Add a comment
10|10000 characters needed characters exceeded

Related questions

1 Answer

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Sep 12, 2016 at 07:37 AM

    Hi Dhiraj,

    It is possible and it is quite simple as the product flow. Take example of category. For indexing category following steps are required:

    Step:1. So you need to create index and create index property and value provider as well if required.

     Solr.impex
      
      INSERT_UPDATE SolrIndexedType;identifier[unique=true];type(code);variant;sorts(&sortRefID);;;;;;;;;;;;;;;;
      ;$solrCategoryIndexedType;Category;false;;;;;;;;;;;;;;;;;
      
      INSERT_UPDATE SolrFacetSearchConfig;name[unique=true];description;indexNamePrefix;languages(isocode);currencies(isocode);solrServerConfig(name);solrSearchConfig(description);solrIndexConfig(name);solrIndexedTypes(identifier);enabledLanguageFallbackMechanism;$catalogVersions;;;;;;;;;
      ;$facetSearchConfigName;$facetSearchConfigDescription;$searchIndexNamePrefix;$indexLanguages;$indexCurrencies;$serverConfigName;$searchConfigName;$indexConfigName;$solrIndexedType,$solrCategoryIndexedType;true;$productCatalog:Online,$contentCatalog:Online;;;;;;;;;
      
      
      INSERT_UPDATE SolrIndexedProperty;solrIndexedType(identifier)[unique=true];name[unique=true];type(code);sortableType(code);currency[default=false];localized[default=false];multiValue[default=false];useForSpellchecking[default=false];useForAutocomplete[default=false];fieldValueProvider;valueProviderParameter;;;;;;;;;
      ;$solrCategoryIndexedType; code                   ;string ;            ;    ;    ;    ;;;;;;;;;;;;;
      ;$solrCategoryIndexedType; name                   ;text   ;sortabletext;    ;true;    ;;;;;;;;;;;;;
      
      
      INSERT_UPDATE SolrIndexerQuery;solrIndexedType(identifier)[unique=true];identifier[unique=true];type(code);injectCurrentDate[default=true];injectCurrentTime[default=true];injectLastIndexTime[default=true];query;user(uid);;;;;;;;;;;;
      ;$solrCategoryIndexedType;$searchIndexNamePrefix-fullCategoryQuery;full;;;true;select {c:PK} from {Category as c};anonymous;;;;;;;;;;;;
    
    

    Step : 2 Need some provider if required. So value provider is used for fetching values from model and pass it to solr for indexing. Suppose you have newattribute. You need followinhg

    Solr.impex:

     INSERT_UPDATE SolrIndexedProperty;solrIndexedType(identifier)[unique=true];name[unique=true];type(code);localized[default=false];multiValue[default=true];categoryField[default=true];useForSpellchecking[default=false];useForAutocomplete[default=false];fieldValueProvider;;;;;;;;;;;
      ;$categoryIndexType; contentType                ;string ;            ;false;    ;    ;    ;categorynewAttributeHeightValueProvider;;;;;;;;;;;
    
    

    CategorynewAttributeHeightValueProvider.java

     public class ProductnewAttributeHeightValueProvider extends
              AbstractPropertyFieldValueProvider implements FieldValueProvider,
              Serializable {
      
          private static final long serialVersionUID = 1L;
          private FieldNameProvider fieldNameProvider;
      
          @Override
          public Collection<FieldValue> getFieldValues(final IndexConfig indexConfig,
                  final IndexedProperty indexedProperty, final Object model)
                  throws FieldValueProviderException {
              
              if (categoryModel== null) {
                  return Collections.emptyList();
      
              }
      
              final String newAttribute = categoryModel.getNewAttribute();
      
              if (null != height) {
                  final Collection<FieldValue> fieldValues = new ArrayList<FieldValue>();
                  fieldValues.addAll(createFieldValue(newAttribute, indexedProperty));
                  return fieldValues;
      
              } else {
                  return Collections.emptyList();
              }
          }
      
          protected List<FieldValue> createFieldValue(final String newAttribute,
                  final IndexedProperty indexedProperty) {
              final List<FieldValue> fieldValues = new ArrayList<FieldValue>();
              final Collection<String> fieldNames = fieldNameProvider.getFieldNames(
                      indexedProperty, null);
              for (final String fieldName : fieldNames) {
                  fieldValues.add(new FieldValue(fieldName, newAttribute));
              }
              return fieldValues;
          }
      
          
      
          @Required
          public void setFieldNameProvider(final FieldNameProvider fieldNameProvider) {
              this.fieldNameProvider = fieldNameProvider;
          }
      }
    
    

    Step : 3 Need to add query on which attribute you want to search new index type. Add this code to make search query for code and name or you can customize as you want

      yourcore-spring.xml
      
      <alias name="yourCommerceSearchTextPopulator" alias="yourCommerceSearchTextPopulator" />
          <bean id="yourCommerceSearchTextPopulator"
              class="com.core.search.solrfacetsearch.populators.YourSearchTextPopulator">
              <property name="freeTextQueryBuilders">
                  <list>
                      <bean
                          class="de.hybris.platform.commerceservices.search.solrfacetsearch.querybuilder.impl.DefaultFreeTextQueryBuilder">
                          <property name="propertyName" value="code" />
                          <property name="boost" value="90" />
                      </bean>
                      <bean
                          class="de.hybris.platform.commerceservices.search.solrfacetsearch.querybuilder.impl.DefaultFreeTextQueryBuilder">
                          <property name="propertyName" value="name" />
                          <property name="boost" value="50" />
                      </bean>
                  </list>
              </property>
          </bean>
    
    

    Step: 4 Write controller , facade, service, converter and populator. So from controller you need to write new flow for searching and getting the result from solr. So just refer current flow of product search and implement new flow. So you need some populator and converter as well. Refer these OOTB files DefaultSolrProductSearchFacade,DefaultSolrProductSearchService etc.

    Hope this will help you so pls accept as answer and keep upvote. cheers!

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Dhiraj Kulkarni

      It is just example, category is also an item type. So whatever is your item just follow the same. And it is upto you how you are managing catalog version. Wherever you want change just change the facade , service and other flow. Above code snippet will complete index part end to end. So once you will start implementation you will get it better.So dont worry about that just carry on.

      If it helps you pls accept as answer and keep upvote as it helps other as well.

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.