ElasticIndexingServiceConnector.java

  1. /*
  2. * Copyright (C) 2003-2015 eXo Platform SAS.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. * GNU Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public License
  15. * along with this program. If not, see http://www.gnu.org/licenses/ .
  16. */
  17. package org.exoplatform.commons.search.index.impl;

  18. import org.apache.commons.lang.StringUtils;
  19. import org.json.simple.JSONObject;

  20. import org.exoplatform.commons.search.index.IndexingServiceConnector;
  21. import org.exoplatform.commons.utils.PropertyManager;
  22. import org.exoplatform.container.xml.InitParams;
  23. import org.exoplatform.container.xml.PropertiesParam;

  24. /**
  25.  * Created by The eXo Platform SAS
  26.  * Author : Thibault Clement
  27.  * tclement@exoplatform.com
  28.  * 7/22/15
  29.  */
  30. public abstract class ElasticIndexingServiceConnector extends IndexingServiceConnector {

  31.   private static final Integer REPLICAS_NUMBER_DEFAULT = 0;
  32.   private static final String REPLICAS_NUMBER_PROPERTY_NAME = "exo.es.indexing.replica.number.default";
  33.   private static final Integer SHARDS_NUMBER_DEFAULT = 5;
  34.   private static final String SHARDS_PROPERTY_NAME = "exo.es.indexing.shard.number.default";

  35.   protected String indexAlias;
  36.   protected String currentIndex;
  37.   protected String previousIndex;
  38.   protected boolean reindexOnUpgrade;
  39.   protected Integer shards = SHARDS_NUMBER_DEFAULT;
  40.   protected Integer replicas = REPLICAS_NUMBER_DEFAULT;

  41.   public ElasticIndexingServiceConnector(InitParams initParams) {
  42.     PropertiesParam param = initParams.getPropertiesParam("constructor.params");
  43.     String reindexOnUpgradeString = param.getProperty("reindexOnUpgrade");
  44.     this.reindexOnUpgrade = StringUtils.isNotBlank(reindexOnUpgradeString) && reindexOnUpgradeString.trim().equalsIgnoreCase("true");

  45.     this.indexAlias = param.getProperty("index_alias");
  46.     this.currentIndex = param.getProperty("index_current");
  47.     this.previousIndex = param.getProperty("index_previous");
  48.     setType(param.getProperty("type"));
  49.     //Get number of replicas in connector declaration or exo properties
  50.     if (StringUtils.isNotBlank(param.getProperty("replica.number"))) {
  51.       this.replicas = Integer.valueOf(param.getProperty("replica.number"));
  52.     }
  53.     else if (StringUtils.isNotBlank(PropertyManager.getProperty(REPLICAS_NUMBER_PROPERTY_NAME))) {
  54.       this.replicas = Integer.valueOf(PropertyManager.getProperty(REPLICAS_NUMBER_PROPERTY_NAME));
  55.     }
  56.     //Get number of shards in connector declaration or exo properties
  57.     if (StringUtils.isNotBlank(param.getProperty("shard.number"))) {
  58.       this.shards = Integer.valueOf(param.getProperty("shard.number"));
  59.     }
  60.     else if (StringUtils.isNotBlank(PropertyManager.getProperty(SHARDS_PROPERTY_NAME))) {
  61.       this.shards = Integer.valueOf(PropertyManager.getProperty(SHARDS_PROPERTY_NAME));
  62.     }
  63.   }

  64.   /**
  65.    *
  66.    * Default mapping rules for ES type
  67.    * {
  68.      "type_name" : {
  69.        "properties" : {
  70.          "permissions" : {"type" : "keyword"},
  71.          "sites" : {"type" : "keyword"}
  72.        }
  73.      }
  74.    }
  75.    *
  76.    * This method must be overridden by your specific connector if you want to define special mapping
  77.    *
  78.    * @return JSON containing a mapping to create new type
  79.    *
  80.    */
  81.   public String getMapping() {

  82.       JSONObject notAnalyzedField = new JSONObject();
  83.       notAnalyzedField.put("type", "text");
  84.       notAnalyzedField.put("index", false);

  85.       JSONObject keywordMapping = new JSONObject();
  86.       keywordMapping.put("type", "keyword");

  87.       JSONObject properties = new JSONObject();
  88.       properties.put("permissions", keywordMapping);
  89.       properties.put("sites", keywordMapping);
  90.       properties.put("url", notAnalyzedField);

  91.       JSONObject mappingProperties = new JSONObject();
  92.       mappingProperties.put("properties",properties);

  93.       JSONObject mappingJSON = new JSONObject();
  94.       mappingJSON.put(getType(), mappingProperties);

  95.       return mappingJSON.toJSONString();
  96.   }

  97.   public String getIndex() {
  98.     return indexAlias;
  99.   }

  100.   public void setIndex(String index) {
  101.     this.indexAlias = index;
  102.   }

  103.   public String getCurrentIndex() {
  104.     return currentIndex;
  105.   }

  106.   public String getPreviousIndex() {
  107.     return previousIndex;
  108.   }

  109.   public void setPreviousIndex(String previousIndex) {
  110.     this.previousIndex = previousIndex;
  111.   }

  112.   public boolean isReindexOnUpgrade() {
  113.     return reindexOnUpgrade;
  114.   }

  115.   public Integer getShards() {
  116.     return shards;
  117.   }

  118.   public void setShards(Integer shards) {
  119.     this.shards = shards;
  120.   }

  121.   public Integer getReplicas() {
  122.     return replicas;
  123.   }

  124.   public void setReplicas(Integer replicas) {
  125.     this.replicas = replicas;
  126.   }

  127.   @Override
  128.   public String delete(String id) {
  129.     return id;
  130.   }

  131.   public boolean isNeedIngestPipeline() {
  132.     return false;
  133.   }

  134.   public String getPipelineName() {
  135.     return null;
  136.   }

  137.   public String getAttachmentProcessor() {
  138.     return null;
  139.   }
  140. }