yousufbadar

updated

15 years ago

Y

yousufbadar

1 Rookie

7 Posts

0

2372

September 14th, 2011 14:00

Search for Documents using attributes

Hi,

  We have documents stored in Centera cluster and would want to do a search based on some of the attributes.

i was able to run the sample code for search based on attributes but have found a serious contraint. the search criteria only works with a time contraint. a FROM and TO timestamp. and the interval too is very short. so i cannot say a starting date which is months/years ago. if the time interval is big the code fails with a socket error.

Can someone please guide me if i am in the right direction or am i missing something?

code is as follows:

STORE :

i add the following attributes: i tried with the Tag BUT that didnt work. donno how to query by Tag attributes.




theClip.setDescriptionAttribute("filename", filename);



theClip.setDescriptionAttribute("firstName", "Yousuf");



theClip.setDescriptionAttribute("lastName", "Badar");



theClip.setDescriptionAttribute("accession", "ACC123456789");



theClip.setDescriptionAttribute("patientId", "PID123456789");

RETRIEVE

Set selectedFields= new HashSet ();

selectedFields.add("creation.date");

selectedFields.add("modification.date");

selectedFields.add("lastName");

selectedFields.add("firstName");

Map fieldRegExps=new HashMap ();

fieldRegExps.put("firstName", "Yousuf");

fieldRegExps.put("lastName", "Badar");

          

                    thePool = conPool.getConnection();

 

              try{

                        TimeZone TZ = TimeZone.getTimeZone("EST");

                        Calendar clusterTime = Calendar.getInstance(TZ);

                              Calendar epoch = Calendar.getInstance(TZ);

                              epoch.setTime(new Date(0));

                              String start = ConvertDateTimeToString(epoch); 

                                        start = "2010.9.9.00.00.01";    

                               from = ConvertDisplayToCalendar(start); 

                              String  end = //ConvertDateTimeToString(clusterTime);

                                "2011.9.10.00.00.01";

                              to = ConvertDisplayToCalendar(end);         

              

                /*

                 * compile patterns

                 */

                        System.out.println("compiling patterns ...");

                Map patterns = new HashMap ();

                if(fieldRegExps != null) {

                  for(Map.Entry e : fieldRegExps.entrySet()) {

                    try{

                      Pattern p = Pattern.compile(e.getValue());

                      patterns.put(e.getKey(), p);

                    } catch(Exception ex) {

                      throw new Exception(

                        String.format("error while parsing regexp '%s' into pattern: %s", e.getValue(), ex.getMessage()), ex);

                    }

                  }

                }

                

                /*

                 * create Centerasearch expression

                 */

                FPQueryExpression exp = new FPQueryExpression();

                if(from != null) {

                          System.out.println(String.format("[query] date from is %s", ConvertDateTimeToString(from)));

                  exp.setStartTime(from.getTimeInMillis());

                } if(to != null) {

                  System.out.println(String.format("[query] date to is %s", ConvertDateTimeToString(to)));

                  exp.setEndTime(to.getTimeInMillis());

                }

              

                int type = 0;

                if(includeExistingClips) {

                  System.out.println("[query] including existing clips");

                  type |= FPLibraryConstants.FP_QUERY_TYPE_EXISTING;

                }

              

                if(includeDeletedClips) {

                  System.out.println("[query] including deleted clips");

                  type |= FPLibraryConstants.FP_QUERY_TYPE_DELETED;

                }

              

                exp.setType(type);

              

                StringBuilder buf = new StringBuilder();

                for(String field: selectedFields) {

                  buf.append(field);

                  buf.append(" ");

                

                 exp.selectField(field);

                }

              

                System.out.println(String.format("[query] selected fields are %s", buf.toString()));

              

                /*

                 * create query and do search

                 */

                System.out.println("creating pool query ...");

                FPPoolQuery query = new FPPoolQuery(thePool, exp);

              

                long timeout = -1;

                if(queryTimeout != null)

                  timeout = queryTimeout.longValue();

              

                FPQueryResult result = null;

              

              boolean finished = false;

              boolean incomplete = false;

              boolean process = false;

            

              String error = null;

            

              Set clipIds = new LinkedHashSet ();

            

              int retries = 0;

              int idx = 0;

                while(!(finished || incomplete)) {

                

                  // fetch next result

                

                  LOGGER.trace(String.format("[%03d] fetching result", idx));

                  result = query.FetchResult(timeout);

                  process = false;

                

                  LOGGER.trace(String.format("[%03d] result is %d", idx, result.getResultCode()));

                  if(result.getResultCode() != FPLibraryConstants.FP_QUERY_RESULT_CODE_PROGRESS)

                    retries = 0;

                        

                  // check status

                  switch(result.getResultCode()) {

                

                    case FPLibraryConstants.FP_QUERY_RESULT_CODE_OK:

                      process = true;

                      break;

                    

                    case FPLibraryConstants.FP_QUERY_RESULT_CODE_INCOMPLETE:

                      incomplete = true;

                      break;

                    

                    case FPLibraryConstants.FP_QUERY_RESULT_CODE_COMPLETE:

                    case FPLibraryConstants.FP_QUERY_RESULT_CODE_END:

                      process = true;

                      finished = true;

                      break;

                    case FPLibraryConstants.FP_QUERY_RESULT_CODE_ABORT:

                      error = "The query has been aborted because of a problem at the server side. The application may want to resubmit the query";

                      incomplete = true;

                      break;

                    

                    case FPLibraryConstants.FP_QUERY_RESULT_CODE_ERROR:

                      error = "The FetchResult call is aborted because a (platform-specific) error occurred.";

                      incomplete = true;

                      break;

                    

                    case FPLibraryConstants.FP_QUERY_RESULT_CODE_PROGRESS:

                    

                      if(maxRetries == null) {

                        error = "query timed out and retries are disabled, aborting search";

                        incomplete = true;

                        break;

                      }

                    

                      if(retries >= maxRetries.intValue()) {

                        error = String.format("query timed out maximum number of retries reached (%d), aborting search", maxRetries.intValue());

                        incomplete = true;

                      }

                                

                      retries++;

//                      System.out.println(String.format("search timed out after %d, going into retry %d / %d", retries, maxRetries.intValue()));

                      break;          

                  }

                

                  idx++;

                

                  if(!process)

                    continue;

                  // check if all patterns apply

                  boolean matched = true;

                  for(Map.Entry e : patterns.entrySet()) {

                  

                    String fieldName = e.getKey();

                  

                    String value = result.getField(fieldName);

                    if((value == null) || (value.trim().length() == 0))

                      continue;

                  

                    Matcher m = e.getValue().matcher(value);

                    if(!m.matches()) {

                      matched = false;

                      break;

                    }

                  }

                

                  if(!matched)

                    continue;

                

                  String clipId = result.getClipID();

                  System.out.println(clipId + "  "+ result.getField("firstName") +"  "

                                      +  result.getField("lastName") + " " + result.getField("creation.date")+ " "

                                      + result.getField("modification.date"));

                  clipIds.add(clipId);

                }

              } catch(Exception e) {

                throw new Exception(String.format("error while processing Centera search: %s", e.getMessage()), e);

              }