Project

General

Profile

« Previous | Next » 

Revision 3328

Added by Jing Tao almost 17 years ago

Add code to handle none-same value query term.

View differences:

src/edu/ucsb/nceas/metacat/QueryGroup.java
46 46
    private int countPercentageSearchItem = 0;
47 47
    private Vector queryTermsWithSameValue = null;//this two dimension vectors.
48 48
                                                  //will hold query terms which has same search value.
49
    private Vector queryTermsInPathIndex = null; //this vector holds query terms without same value
50
                                                                                 // and search path is in path index.
49 51
    private Vector queryTerms = null;//this vector only holds query terms without same search value.
52
                                                             // and search path is NOT in path index.
50 53
    private Vector queryGroupsChildren = null;
51 54
    private static Logger logMetacat = Logger.getLogger(QueryGroup.class);
52 55
    private static String UNION = "UNION";
......
61 64
      this.operator = operator;
62 65
      children = new Vector();
63 66
      queryTermsWithSameValue = new Vector();
67
      queryTermsInPathIndex = new Vector(); 
64 68
      queryTerms = new Vector();
65 69
      queryGroupsChildren = new Vector();
66 70
    }
......
107 111

  
108 112
      boolean first = true;
109 113
      
110
      if (!queryTermsWithSameValue.isEmpty())
114
      if (!queryTermsWithSameValue.isEmpty() || queryTermsInPathIndex.isEmpty())
111 115
      {
112
    	  String sameValueQueryString = printSQLStringWithSameSearchValue();
113
    	  queryString.append(sameValueQueryString);
116
    	  String pathIndexQueryString = printSQLStringInPathIndex();
117
    	  queryString.append(pathIndexQueryString);
114 118
    	  if (queryString != null)
115 119
    	  {
116 120
    		  first = false;
......
176 180
    
177 181
    
178 182
    /*
179
     * If every query term in a queryGroup share a search value, we should
180
     * use a new query to replace the original query term query in order to
181
     * improve performance
183
     * If every query term in a queryGroup share a search value and search path
184
     * is in xml_path_index, we should use a new query to replace the original query term query in order to
185
     * improve performance. Also if even the term doesn't share any value with other term
186
     * we still use "OR" to replace UNION action (we only handle union operator in the query group).
187
     * 
182 188
     */
183
    private String printSQLStringWithSameSearchValue()
189
    private String printSQLStringInPathIndex()
184 190
    {
185 191
    	String sql ="";
186 192
    	String value ="";
187 193
    	StringBuffer sqlBuff = new StringBuffer();
188 194
    	Vector pathVector = new Vector();
189 195
    	int index =0;
190
    	if (queryTermsWithSameValue != null)
196
    	if (queryTermsWithSameValue != null && queryTermsInPathIndex != null)
191 197
    	{
192
    		boolean firstVector = true;
193
    		for (int j=0; j<queryTermsWithSameValue.size(); j++)
198
    		
199
    		sqlBuff.append("SELECT DISTINCT docid FROM xml_path_index WHERE ");
200
    		if (!queryTermsWithSameValue.isEmpty())
194 201
    		{
195
    	   		Vector queryTermVector = (Vector)queryTermsWithSameValue.elementAt(j);
196
	    		QueryTerm term1 = (QueryTerm)queryTermVector.elementAt(0);
197
	        	value = term1.getValue();
198
	        	boolean first = true;
199
	        	if (firstVector)
200
	        	{
201
				  firstVector = false;
202
	        	}
203
	        	else
204
	        	{
205
	        		sqlBuff.append(" "+operator+" ");
206
	        	}
207
	        	sqlBuff.append("SELECT DISTINCT docid FROM xml_path_index WHERE UPPER(nodedata) LIKE '%");
208
	        	if (value != null)
209
	        	{
210
	        	    sqlBuff.append(value.toUpperCase());
211
	        	}
212
	        	else
213
	        	{
214
	        		sqlBuff.append(value);
215
	        	}
216
				sqlBuff.append("%' AND path IN (");
217
	    		//gets every path in query term object
218
	    		for (int i=0; i<queryTermVector.size(); i++)
202
    			boolean firstVector = true;
203
	    		for (int j=0; j<queryTermsWithSameValue.size(); j++)
219 204
	    		{
220
	    			QueryTerm term = (QueryTerm)queryTermVector.elementAt(i);
221
	    			value = term.getValue();
222
	    			String path = term.getPathExpression();
223
	    			if (path != null && !path.equals(""))
224
	    			{
225
	    				if (first)
226
	    				{
227
	    					first = false;
228
	    					sqlBuff.append("'");
229
	    					sqlBuff.append(path);
230
	    					sqlBuff.append("'");
231
	    					
232
	    				}
233
	    				else
234
	    				{
235
	    					sqlBuff.append(",'");
236
	    					sqlBuff.append(path);
237
	    					sqlBuff.append("'");
238
	    				}
239
	    				index++;
240
	     				if (value != null && (value.equals("%") || value.equals("%%%")))
241
	                    {
242
	    				  countPercentageSearchItem++;
243
	                    }
244
    			     }
245
    		    }
246
    		    sqlBuff.append(")");
247
    	
248
    	    }
205
	    	   		Vector queryTermVector = (Vector)queryTermsWithSameValue.elementAt(j);
206
		    		QueryTerm term1 = (QueryTerm)queryTermVector.elementAt(0);
207
		        	value = term1.getValue();
208
		        	boolean first = true;
209
		        	if (firstVector)
210
		        	{
211
					  firstVector = false;
212
		        	}
213
		        	else
214
		        	{
215
		        		sqlBuff.append(" "+"OR"+" ");
216
		        	}
217
		        	sqlBuff.append(" (UPPER(nodedata) LIKE '%");
218
		        	if (value != null)
219
		        	{
220
		        	    sqlBuff.append(value.toUpperCase());
221
		        	}
222
		        	else
223
		        	{
224
		        		sqlBuff.append(value);
225
		        	}
226
					sqlBuff.append("%' AND path IN (");
227
		    		//gets every path in query term object
228
		    		for (int i=0; i<queryTermVector.size(); i++)
229
		    		{
230
		    			QueryTerm term = (QueryTerm)queryTermVector.elementAt(i);
231
		    			value = term.getValue();
232
		    			String path = term.getPathExpression();
233
		    			if (path != null && !path.equals(""))
234
		    			{
235
		    				if (first)
236
		    				{
237
		    					first = false;
238
		    					sqlBuff.append("'");
239
		    					sqlBuff.append(path);
240
		    					sqlBuff.append("'");
241
		    					
242
		    				}
243
		    				else
244
		    				{
245
		    					sqlBuff.append(",'");
246
		    					sqlBuff.append(path);
247
		    					sqlBuff.append("'");
248
		    				}
249
		    				index++;
250
		     				if (value != null && (value.equals("%") || value.equals("%%%")))
251
		                    {
252
		    				  countPercentageSearchItem++;
253
		                    }
254
	    			     }
255
	    		    }
256
	    		    sqlBuff.append("))");
257
	    	
258
	    	    }
259
	    	}
260
    		if (!queryTermsInPathIndex.isEmpty())
261
    		{
262
    			for (int j=0; j<queryTermsInPathIndex.size(); j++)
263
    			{
264
    				QueryTerm term = (QueryTerm)queryTermsInPathIndex.elementAt(j);
265
    				if (term != null)
266
    				{
267
	    				term.setInUnionGroup(true);
268
		    			 if (index > 0)
269
		    			 {
270
		    				 sqlBuff.append(" "+"OR"+" ");
271
		    			 }
272
		    			 sqlBuff.append("(");
273
	    				 sqlBuff.append(term.printSQL(true));
274
	    				 sqlBuff.append(")");
275
	    				 index++;
276
	    			}
277
    			}
278
    		}
249 279
    	}
250 280
    	if (index >0)
251 281
    	{
......
295 325
    				MetaCatUtil.pathsForIndexing.contains(newTerm.getPathExpression()))
296 326
    	    {
297 327
    			//System.out.println("in only union branch in handle new query term");
298
	    		for (int i=0; i<queryTerms.size(); i++)
328
	    		for (int i=0; i<queryTermsInPathIndex.size(); i++)
299 329
	    		{
300
	    			QueryTerm term = (QueryTerm)queryTerms.elementAt(i);
330
	    			QueryTerm term = (QueryTerm)queryTermsInPathIndex.elementAt(i);
301 331
	    			if (term != null && term.hasSameSearchValue(newTerm))
302 332
	    			{
303 333
	    				//System.out.println("1Move a query term and add a new query term into search value in handle new query term");
......
306 336
	    				newSameValueVector.add(term);
307 337
	    				newSameValueVector.addElement(newTerm);
308 338
	    				queryTermsWithSameValue.add(newSameValueVector);
309
	    				queryTerms.remove(i);
339
	    				queryTermsInPathIndex.remove(i);
310 340
	    				return;
311 341
	    			}
312 342
	    		}
......
324 354
	    				return;
325 355
	    			}
326 356
	    		}
357
	    		//nothing found, but the search path is still in xml_path_index,
358
	    		// save it into queryTermsInPathIndex vector
359
	    		queryTermsInPathIndex.add(newTerm);
360
	    		return;
327 361
    	    }
328
    		// both queryTerms and queryTermsWithSameValue don't have the search value,
329
    		// add this newTerm to queryTerms
362
    		
363
    		// add this newTerm to queryTerms since we couldn't find it in xml_path_index
330 364
    		queryTerms.add(newTerm);
331 365
    	}
332 366
    	

Also available in: Unified diff