Authored by hugufei

Merge branch 'master' into dev

# Conflicts:
#	deploy/pom.xml
#	deploy/src/main/META-INF/autoconf/auto-config.xml
#	deploy/src/main/META-INF/autoconf/bin/env.bat
#	deploy/src/main/META-INF/autoconf/bin/env.sh
#	deploy/src/main/META-INF/autoconf/conf/jboss/application.xml
#	deploy/src/main/META-INF/autoconf/conf/tomcat/server.xml
#	deploy/src/main/application.xml
#	deploy/src/main/assembly/assembly.xml
#	deploy/src/main/bin/env.bat
#	deploy/src/main/bin/env.sh
#	deploy/src/main/bin/jbossctl-dev.bat
#	deploy/src/main/bin/tomcat-dev.bat
#	deploy/src/main/conf/tomcat/server.xml
#	pom.xml
#	service/pom.xml
#	service/src/main/java/com/yoho/search/restapi/BrandController.java
#	service/src/main/java/com/yoho/search/restapi/SearchController.java
#	service/src/main/java/com/yoho/search/restapi/TblProductController.java
#	service/src/main/java/com/yoho/search/service/BrandSearchService.java
#	service/src/main/java/com/yoho/search/service/SearchService.java
#	service/src/main/java/com/yoho/search/service/SearchSortSizeService.java
#	service/src/main/java/com/yoho/search/service/TblProductService.java
#	web/pom.xml
#	web/src/main/resources/META-INF/spring/spring-web-context.xml
#	web/src/main/resources/config.properties
#	web/src/main/resources/configuration.xml
#	web/src/main/resources/databases.yml
#	web/src/main/resources/logback.xml
#	web/src/main/webapp/META-INF/autoconf/auto-config.xml
#	web/src/main/webapp/META-INF/autoconf/config.properties
#	web/src/main/webapp/META-INF/autoconf/databases.yml
#	web/src/main/webapp/META-INF/autoconf/logback.xml
#	web/src/main/webapp/WEB-INF/web.xml
#	web/src/main/webapp/index.jsp
@@ -2,15 +2,9 @@ @@ -2,15 +2,9 @@
2 2
3 <config> 3 <config>
4 <group> 4 <group>
5 - <!--yoho-search-autoconfig-->  
6 -  
7 - <property name="yoho.search.service.env.webapphome" defaultValue="/home/test/yoho-search-service/deploy" description="应用根目录"/>  
8 - <property name="yoho.search.service.env.serverport" defaultValue="7" description="应用端口"/> 5 + <property name="yoho.search.service.env.webapphome" defaultValue="/home/master/yoho-search-service/deploy" description="应用根目录"/>
  6 + <property name="yoho.search.service.env.serverport" defaultValue="0" description="应用端口"/>
9 <property name="yoho.search.service.env.namespace" defaultValue="search-service" description="应用命名空间"/> 7 <property name="yoho.search.service.env.namespace" defaultValue="search-service" description="应用命名空间"/>
10 - <property name="yoho.env.javahome" defaultValue="/usr/local/java" description="" />  
11 - <property name="yoho.env.productmode" defaultValue="TEST" description="" />  
12 - <property name="yoho.env.serverhome" defaultValue="/usr/local/tomcat" description="" />  
13 - <property name="yoho.env.servertype" defaultValue="tomcat" description="" />  
14 </group> 8 </group>
15 <script> 9 <script>
16 <generate template="META-INF/autoconf/bin/env.sh" destfile="bin/env.sh"/> 10 <generate template="META-INF/autoconf/bin/env.sh" destfile="bin/env.sh"/>
@@ -57,7 +57,7 @@ if [ $PRODUCTION_MODE = "PRODUCT" ]; then @@ -57,7 +57,7 @@ if [ $PRODUCTION_MODE = "PRODUCT" ]; then
57 if [ -n "$str" ]; then 57 if [ -n "$str" ]; then
58 let memTotal=`cat /proc/meminfo |grep MemTotal|awk '{printf "%d", $2/1024 }'` 58 let memTotal=`cat /proc/meminfo |grep MemTotal|awk '{printf "%d", $2/1024 }'`
59 if [ $memTotal -gt 10000 ];then 59 if [ $memTotal -gt 10000 ];then
60 - JAVA_MEM_OPTS=" -server -Xmx4000M -Xms4000M -Xmn600M -XX:PermSize=200M -XX:MaxPermSize=200M -Xss256K -XX:+DisableExplicitGC -XX:SurvivorRatio=1 -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:CMSFullGCsBeforeCompaction=0 -XX:+CMSClassUnloadingEnabled -XX:LargePageSizeInBytes=128M -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=80 -XX:SoftRefLRUPolicyMSPerMB=0 -XX:+PrintGCDetails -XX:+PrintGCApplicationStoppedTime -Xloggc:$WEB_APP_HOME/logs/gc.log -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$WEB_APP_HOME/logs/heapdump.hprof " 60 + JAVA_MEM_OPTS=" -server -Xmx2500M -Xms2500M -Xmn400M -XX:PermSize=200M -XX:MaxPermSize=200M -Xss256K -XX:+DisableExplicitGC -XX:SurvivorRatio=1 -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:CMSFullGCsBeforeCompaction=0 -XX:+CMSClassUnloadingEnabled -XX:LargePageSizeInBytes=128M -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=80 -XX:SoftRefLRUPolicyMSPerMB=0 -XX:+PrintGCDetails -XX:+PrintGCApplicationStoppedTime -Xloggc:$WEB_APP_HOME/logs/gc.log -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$WEB_APP_HOME/logs/heapdump.hprof "
61 else 61 else
62 JAVA_MEM_OPTS=" -server -Xmx1g -Xms1g -Xmn256m -XX:PermSize=128m -Xss256k -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:LargePageSizeInBytes=64m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 " 62 JAVA_MEM_OPTS=" -server -Xmx1g -Xms1g -Xmn256m -XX:PermSize=128m -Xss256k -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:LargePageSizeInBytes=64m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 "
63 fi 63 fi
1 set JAVA_MEM_OPT= -Xms1024m -Xmx1024m -XX:MaxPermSize=128m -Djava.net.preferIPv4Stack=true 1 set JAVA_MEM_OPT= -Xms1024m -Xmx1024m -XX:MaxPermSize=128m -Djava.net.preferIPv4Stack=true
2 set JAVA_HOME=/usr/local/java 2 set JAVA_HOME=/usr/local/java
3 -set WEB_APP_HOME=/home/dev/yoho-search-service/deploy  
4 - 3 +set WEB_APP_HOME=/home/test/yoho-search-service/deploy
5 4
6 set CATALINA_HOME=/usr/local/tomcat 5 set CATALINA_HOME=/usr/local/tomcat
7 -set CATALINA_BASE=/home/dev/yoho-search-service/deploy/tomcat_server 6 +set CATALINA_BASE=/home/test/yoho-search/deploy/tomcat_server
8 7
9 8
10 rem set JAVA_DEBUG_OPT= -server -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=8082,server=y,suspend=n 9 rem set JAVA_DEBUG_OPT= -server -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=8082,server=y,suspend=n
@@ -6,16 +6,15 @@ BASE_BIN_DIR=`dirname $0` @@ -6,16 +6,15 @@ BASE_BIN_DIR=`dirname $0`
6 . $BASE_BIN_DIR/functions.sh 6 . $BASE_BIN_DIR/functions.sh
7 7
8 export JAVA_HOME=/usr/local/java 8 export JAVA_HOME=/usr/local/java
9 -export WEB_APP_HOME=/home/dev/yoho-search-service/deploy 9 +export WEB_APP_HOME=/home/master/yoho-search-service/deploy
10 export SERVER_PORT=0 10 export SERVER_PORT=0
11 -export PRODUCTION_MODE="TEST" 11 +export PRODUCTION_MODE="PRODUCT"
12 export SERVER_HOME=/usr/local/tomcat 12 export SERVER_HOME=/usr/local/tomcat
13 export SERVER_TYPE=tomcat 13 export SERVER_TYPE=tomcat
14 export SERVER_NAMESPACE=yohosearch 14 export SERVER_NAMESPACE=yohosearch
15 export GW_URL=$Gateway_URL 15 export GW_URL=$Gateway_URL
16 16
17 -  
18 - export SERVER_BASE_DIR=" -Dcatalina.base=/home/dev/yoho-search-service/deploy/.tomcat " 17 +export SERVER_BASE_DIR=" -Dcatalina.base=/home/master/yoho-search-service/deploy/.tomcat "
19 18
20 export NAMING_PORT=`expr 9000 + $SERVER_PORT` 19 export NAMING_PORT=`expr 9000 + $SERVER_PORT`
21 export HTTP_SERVER_PORT=`expr 8080 + $SERVER_PORT` 20 export HTTP_SERVER_PORT=`expr 8080 + $SERVER_PORT`
@@ -19,7 +19,7 @@ @@ -19,7 +19,7 @@
19 define subcomponents such as "Valves" at this level. 19 define subcomponents such as "Valves" at this level.
20 Documentation at /docs/config/server.html 20 Documentation at /docs/config/server.html
21 --> 21 -->
22 -<Server port="8100" shutdown="SHUTDOWN"> 22 +<Server port="8107" shutdown="SHUTDOWN">
23 <Listener className="org.apache.catalina.startup.VersionLoggerListener" /> 23 <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
24 <!-- Security listener. Documentation at /docs/config/listeners.html 24 <!-- Security listener. Documentation at /docs/config/listeners.html
25 <Listener className="org.apache.catalina.security.SecurityListener" /> 25 <Listener className="org.apache.catalina.security.SecurityListener" />
@@ -88,7 +88,7 @@ @@ -88,7 +88,7 @@
88 --> 88 -->
89 89
90 <!-- Define an AJP 1.3 Connector on port 8009 --> 90 <!-- Define an AJP 1.3 Connector on port 8009 -->
91 - <Connector port="8000" protocol="AJP/1.3" redirectPort="8443" /> 91 + <Connector port="8007" protocol="AJP/1.3" redirectPort="8443" />
92 92
93 93
94 <!-- An Engine represents the entry point (within Catalina) that processes 94 <!-- An Engine represents the entry point (within Catalina) that processes
  1 +package com.yoho.search.service;
  2 +
  3 +import java.util.ArrayList;
  4 +import java.util.HashMap;
  5 +import java.util.Iterator;
  6 +import java.util.LinkedHashMap;
  7 +import java.util.List;
  8 +import java.util.Map;
  9 +
  10 +import org.elasticsearch.index.query.BoolFilterBuilder;
  11 +import org.elasticsearch.index.query.FilterBuilders;
  12 +import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
  13 +import org.elasticsearch.search.aggregations.AggregationBuilders;
  14 +import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
  15 +import org.elasticsearch.search.aggregations.bucket.terms.Terms;
  16 +import org.springframework.beans.factory.annotation.Autowired;
  17 +import org.springframework.stereotype.Service;
  18 +
  19 +import com.yoho.search.dal.model.SearchParam;
  20 +import com.yoho.search.dal.model.SearchResult;
  21 +import com.yoho.search.index.Index;
  22 +import com.yoho.search.index.IndexClient;
  23 +import com.yoho.search.index.SearchCallback;
  24 +import com.yoho.search.index.service.IndexService;
  25 +import com.yoho.search.utils.PriceRangeUtils;
  26 +
  27 +/**
  28 + * Created by YOHO on 15-11-2.
  29 + */
  30 +@Service
  31 +public class SearchBaseService {
  32 +
  33 + @Autowired
  34 + private IndexService indexService;
  35 +
  36 + /**
  37 + * 执行检索
  38 + *
  39 + * @param indexName
  40 + * @param searchParam
  41 + * @return SearchResult 返回检索结果
  42 + */
  43 + public SearchResult doSearch(final String indexName, final SearchParam searchParam) {
  44 + SearchResult searchResult = null;
  45 + Index firstIndex = indexService.getIndex(indexName);
  46 + if (firstIndex != null) {
  47 + searchResult = indexService.execute(new SearchCallback<SearchResult>() {
  48 + @Override
  49 + public SearchResult execute(IndexClient indexClient) {
  50 + return indexClient.search(indexName, searchParam);
  51 + }
  52 + }, firstIndex);
  53 + }
  54 + return searchResult;
  55 + }
  56 +
  57 + // 构造通用的检索条件
  58 + public BoolFilterBuilder buildFilter(BoolFilterBuilder boolFilterBuilder, Map<String, String> paramMap) {
  59 + String key;
  60 + String field;
  61 + for (Map.Entry<String, String> entry : paramMap.entrySet()) {
  62 + if (entry.getKey().startsWith("filter_")) {
  63 + key = entry.getKey();
  64 + field = key.substring(7, key.length());
  65 + String value = entry.getValue();
  66 + if (value.contains(",")) {
  67 + String[] values = value.split(",");
  68 + boolFilterBuilder.must(FilterBuilders.termsFilter(field, values));
  69 + } else {
  70 + boolFilterBuilder.must(FilterBuilders.termFilter(field, value));
  71 + }
  72 + } else if (entry.getKey().startsWith("not_filter_")) {
  73 + key = entry.getKey();
  74 + field = key.substring(11, key.length());
  75 + String value = entry.getValue();
  76 + if (value.contains(",")) {
  77 + String[] values = value.split(",");
  78 + boolFilterBuilder.mustNot(FilterBuilders.termsFilter(field, values));
  79 + } else {
  80 + boolFilterBuilder.mustNot(FilterBuilders.termFilter(field, value));
  81 + }
  82 + } else if (entry.getKey().startsWith("range_gte_")) {
  83 + key = entry.getKey();
  84 + field = key.substring(10, key.length());
  85 + boolFilterBuilder.must(FilterBuilders.rangeFilter(field).gte(entry.getValue()));
  86 + } else if (entry.getKey().startsWith("range_lte_")) {
  87 + key = entry.getKey();
  88 + field = key.substring(10, key.length());
  89 + boolFilterBuilder.must(FilterBuilders.rangeFilter(field).lte(entry.getValue()));
  90 + } else if (entry.getKey().startsWith("range_gt_")) {
  91 + key = entry.getKey();
  92 + field = key.substring(9, key.length());
  93 + boolFilterBuilder.must(FilterBuilders.rangeFilter(field).gt(entry.getValue()));
  94 + } else if (entry.getKey().startsWith("range_lt_")) {
  95 + key = entry.getKey();
  96 + field = key.substring(9, key.length());
  97 + boolFilterBuilder.must(FilterBuilders.rangeFilter(field).lt(entry.getValue()));
  98 + }
  99 + }
  100 + return boolFilterBuilder;
  101 + }
  102 +
  103 + public List<AbstractAggregationBuilder> buildAggregations(Map<String, String> paramMap) {
  104 + List<AbstractAggregationBuilder> list = new ArrayList<AbstractAggregationBuilder>();
  105 + String key;
  106 + String field;
  107 + for (Map.Entry<String, String> entry : paramMap.entrySet()) {
  108 + if (entry.getKey().startsWith("agg_")) {
  109 + key = entry.getKey();
  110 + field = key.substring(4, key.length());
  111 + if ("goods_price".equals(field)) {
  112 + list.add(AggregationBuilders.terms(field + "Agg").field(field).size(10000).order(Terms.Order.term(true)));
  113 + } else {
  114 + list.add(AggregationBuilders.terms(field + "Agg").field(field).size(1000));
  115 + }
  116 + }
  117 + }
  118 + return list;
  119 + }
  120 +
  121 + public List<Map<String, Object>> buildFilterResponseMap(MultiBucketsAggregation aggregation, String fieldName) {
  122 + List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
  123 + Map<String, Object> map;
  124 + Iterator<? extends MultiBucketsAggregation.Bucket> itSizeAgg = aggregation.getBuckets().iterator();
  125 + while (itSizeAgg.hasNext()) {
  126 + MultiBucketsAggregation.Bucket lt = itSizeAgg.next();
  127 + map = new HashMap<String, Object>();
  128 + map.put(fieldName, lt.getKey());
  129 + list.add(map);
  130 + }
  131 + return list;
  132 + }
  133 +
  134 + public Map<String, Object> getPriceResponseMap(MultiBucketsAggregation aggregation) {
  135 + Map<String, Object> priceMap = new LinkedHashMap<String, Object>();
  136 + if (aggregation.getBuckets().isEmpty())
  137 + return priceMap;
  138 + Iterator<? extends MultiBucketsAggregation.Bucket> itPriceAgg = aggregation.getBuckets().iterator();
  139 + int size = aggregation.getBuckets().size();
  140 + float[] prePrice = new float[size];
  141 + int i = 0;
  142 + while (itPriceAgg.hasNext()) {
  143 + prePrice[i] = Float.parseFloat(itPriceAgg.next().getKey());
  144 + i++;
  145 + }
  146 + float maxPrice = prePrice[i - 1];
  147 + List<Integer> intervals = PriceRangeUtils.getPriceInterval(prePrice, maxPrice);
  148 + size = intervals.size();
  149 + int j = 0;
  150 + for (j = 0; j < size - 1; j++) {
  151 + if (j > 0) {
  152 + priceMap.put((intervals.get(j) + 1) + "," + intervals.get(j + 1), "¥" + (intervals.get(j) + 1) + "-" + intervals.get(j + 1));
  153 + } else {
  154 + priceMap.put(intervals.get(j) + "," + intervals.get(j + 1), "¥" + intervals.get(j) + "-" + intervals.get(j + 1));
  155 + }
  156 + }
  157 + priceMap.put((intervals.get(j) + 1) + ",99999", "¥" + intervals.get(j) + "以上");
  158 + return priceMap;
  159 + }
  160 +
  161 + public Map<String, Object> getGenderResponseMap(MultiBucketsAggregation aggregation) {
  162 + Map<String, Object> genderMap = new LinkedHashMap<String, Object>();
  163 + Iterator<? extends MultiBucketsAggregation.Bucket> itGenderAgg = aggregation.getBuckets().iterator();
  164 + while (itGenderAgg.hasNext()) {
  165 + MultiBucketsAggregation.Bucket ltGender = itGenderAgg.next();
  166 + String genderId = ltGender.getKey();
  167 + if ("1".equals(genderId)) {
  168 + genderMap.put("1,3", "BOYS");
  169 + } else if ("2".equals(genderId)) {
  170 + genderMap.put("2,3", "GIRLS");
  171 + } else {
  172 + genderMap.put("1,3", "BOYS");
  173 + genderMap.put("2,3", "GIRLS");
  174 + }
  175 + }
  176 + return genderMap;
  177 + }
  178 +}