1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
56
57 public final class ElasticSearchParameterUtil
58 {
59
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
66 private static final String SIZE_ELK_SEARCH_PARAM_VALUE = "grustorage-elastics.sizeSearchParamValue";
67
68
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
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
80
81 private ElasticSearchParameterUtil( )
82 {
83 }
84
85
86
87
88
89
90
91
92
93
94
95
96
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
110
111
112
113
114
115
116
117
118
119
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
142
143
144
145
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 }