View Javadoc
1   /*
2    * Copyright (c) 2002-2017, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.grustorageelastic.util;
35  
36  import java.io.IOException;
37  import java.util.Map;
38  import java.util.Map.Entry;
39  
40  import org.apache.commons.lang.StringUtils;
41  
42  import com.fasterxml.jackson.core.JsonGenerationException;
43  import com.fasterxml.jackson.databind.JsonMappingException;
44  import com.fasterxml.jackson.databind.JsonNode;
45  import com.fasterxml.jackson.databind.ObjectMapper;
46  
47  import fr.paris.lutece.plugins.libraryelastic.business.search.BoolQuery;
48  import fr.paris.lutece.plugins.libraryelastic.business.search.MatchLeaf;
49  import fr.paris.lutece.plugins.libraryelastic.business.search.SearchRequest;
50  import fr.paris.lutece.plugins.libraryelastic.business.suggest.CompletionSuggestRequest;
51  import fr.paris.lutece.portal.service.util.AppLogService;
52  import fr.paris.lutece.portal.service.util.AppPropertiesService;
53  
54  /**
55   * Util class for elastic request
56   */
57  public final class ElasticSearchParameterUtil
58  {
59      // MARKERS
60      public static final String MARKER_AUTOCOMPLETE = "autocomplete";
61      public static final String MARKER_ITEM = "item";
62      public static final String MARKER_ELEMENTS = "elements";
63      public static final String MARKER_PAYLOAD = "payload";
64  
65      // CONSTANTS
66      private static final String SIZE_ELK_SEARCH_PARAM_VALUE = "grustorage-elastics.sizeSearchParamValue";
67  
68      // PROPERTIES KEYS
69      private static final String URL_ELK_SERVER = "grustorage-elastics.urlElk";
70      private static final String PATH_ELK_INDEX = "grustorage-elastics.index";
71      private static final String PATH_ELK_TYPE_USER = "grustorage-elastics.typeUser";
72  
73      // PATHS
74      public static final String PROP_URL_ELK_SERVER = AppPropertiesService.getProperty( URL_ELK_SERVER );
75      public static final String PROP_PATH_ELK_INDEX = AppPropertiesService.getProperty( PATH_ELK_INDEX );
76      public static final String PROP_PATH_ELK_TYPE_USER = AppPropertiesService.getProperty( PATH_ELK_TYPE_USER );
77  
78      /**
79       * Instantiates a new GRU elastics constants.
80       */
81      private ElasticSearchParameterUtil( )
82      {
83      }
84  
85      /**
86       * Format auto complete search.
87       *
88       * @param champ
89       *            the champ
90       * @return the string
91       * @throws JsonGenerationException
92       *             the json generation exception
93       * @throws JsonMappingException
94       *             the json mapping exception
95       * @throws IOException
96       *             Signals that an I/O exception has occurred.
97       */
98      public static CompletionSuggestRequest buildAutoCompleteSearch( String champ )
99      {
100         CompletionSuggestRequest suggest = new CompletionSuggestRequest( );
101         suggest.setMatchType( "text" );
102         suggest.setMatchValue( champ );
103         suggest.setSize( 15 );
104 
105         return suggest;
106     }
107 
108     /**
109      * Build a search requet for elasticsearch.
110      *
111      * @param map
112      *            the map
113      * @return the string
114      * @throws JsonGenerationException
115      *             the json generation exception
116      * @throws JsonMappingException
117      *             the json mapping exception
118      * @throws IOException
119      *             Signals that an I/O exception has occurred.
120      */
121     public static SearchRequest buildSearchRequest( Map<String, String> map )
122     {
123         SearchRequest root = new SearchRequest( );
124         BoolQuery query = new BoolQuery( );
125         for ( Entry<String, String> searchParam : map.entrySet( ) )
126         {
127             query.addShould( new MatchLeaf( searchParam.getKey( ), searchParam.getValue( ) ) );
128         }
129 
130         root.setSearchQuery( query );
131 
132         if ( StringUtils.isNotBlank( AppPropertiesService.getProperty( SIZE_ELK_SEARCH_PARAM_VALUE ) ) )
133         {
134             root.setSize( Integer.parseInt( AppPropertiesService.getProperty( SIZE_ELK_SEARCH_PARAM_VALUE ) ) );
135         }
136 
137         return root;
138     }
139 
140     /**
141      * Sets the json to json tree.
142      *
143      * @param strJson
144      *            the str json
145      * @return the json node
146      */
147     public static JsonNode setJsonToJsonTree( String strJson )
148     {
149         ObjectMapper mapper = new ObjectMapper( );
150         JsonNode tmp = null;
151 
152         try
153         {
154             tmp = mapper.readTree( strJson );
155         }
156         catch( IOException ex )
157         {
158             AppLogService.error( ex + " :" + ex.getMessage( ), ex );
159         }
160 
161         return tmp;
162     }
163 
164 }