-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: search across ALL data in 1 to many
PostPosted: Tue Apr 16, 2013 4:03 pm 
Newbie

Joined: Fri Apr 05, 2013 3:28 pm
Posts: 9
From what I have read so far, I should be able to do one search across several objects when one of them is the at one end of a one to many relationship and the others are at the other end. For example, I have one class called KnowledgeArticle and, for simplicity's sake, I have two classes called BestPractice and CaseSummary. For KnowledgeArticle there can be 1 or many BestPractices and 1 to many CaseSummaries. BestPractice and CaseSummary both have a foreign key called KnowledgeArticleId. Each of the three have a primary key called ID.

I tried adding fields from my child class to the queryBuilder.keyword.onFieldds method, but when I try a search, I get an error because the fields are not in my primary class.

I want to execute one query and return all objects for all of the children. How else will I be able to sort my entire resultset? Or other operations that, at least logically, I imagine would need to take place against ALL of my articles. As it is, I am doing a search against BestPractices, then a search against CaseSummary...I don't believe I want to head down this trail because I have MANY child objects/tables.'

Can someone advise on how to handle this? I am a struggling newbie, but willing to try anything. Including my code for perusal.

Code:

package com.nimsoft.ca.search;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;


import org.hibernate.search.annotations.Analyze;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.IndexedEmbedded;
import org.hibernate.search.annotations.Store;

@Entity
@Indexed
@Table(name = "KNOWLEDGEARTICLE")

public class KnowledgeArticle implements java.io.Serializable {
   
   private static final long serialVersionUID = 1L;
   private String id;
    private String articleNumber;
   private Set<CaseSummary> caseSummaries = new HashSet<CaseSummary>(0);
   private Set<BestPractice> bestPractices = new HashSet<BestPractice>(0);
   
   public KnowledgeArticle() {
    }
   

    public KnowledgeArticle(String articlenumber) {
        this.articleNumber = articlenumber;
   
    }
   
   public KnowledgeArticle(String articlenumber, Set<BestPractice> bestpractices, Set<CaseSummary>  casesummaries) {
      this.articleNumber = articlenumber;
      this.bestPractices = bestpractices;
      this.caseSummaries = casesummaries;

   }
   
   @Id
   @GeneratedValue(strategy = IDENTITY)
   @Column(name = "ID" , unique = true, nullable = false)
    public String getId() {
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }
    @Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
   @Column(name = "ARTICLENUMBER", unique = true, nullable = false, length = 30)
    public String getArticleNumber() {
        return this.articleNumber;
    }

    public void setArticleNumber(String articlenumber) {
        this.articleNumber = articlenumber;
    }
   
    
    @OneToMany(fetch = FetchType.EAGER, mappedBy = "knowledgeArticle")
    @IndexedEmbedded

   public Set<BestPractice> getBestPractices() {
      return this.bestPractices;
   }

   public void setBestPractices(Set<BestPractice> bestpractices) {
      this.bestPractices = bestpractices;
   }

      @OneToMany(fetch = FetchType.EAGER, mappedBy = "knowledgeArticle")
    @IndexedEmbedded

      public Set<CaseSummary> getCaseSummaries() {
         return this.caseSummaries;
      }
   
      public void setCaseSummaries(Set<CaseSummary> casesummaries) {
         this.caseSummaries = casesummaries;
      }

}



Code:

package com.nimsoft.ca.search;

import java.sql.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.search.annotations.Analyze;
import org.hibernate.search.annotations.ContainedIn;
import org.hibernate.search.annotations.DateBridge;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Resolution;
import org.hibernate.search.annotations.Store;

@Entity
@Indexed
@Table(name = "BEST_PRACTICES__KAV")
public class BestPractice implements java.io.Serializable{

    /**
    *
    */
   private static final long serialVersionUID = 1L;
   
   private String Id;
    private KnowledgeArticle knowledgeArticle;
    private String articleNumber;
    private String articleType;
    private String title;
   
    private String summary;
    private Date createdDate;
    private Date firstPublishedDate;
    private Date lastModifiedDate;
    private Date lastPublishedDate;
    private String originatingCase;
    private String publishStatus;
    private String validationStatus;
    private String bestPractice;
    private String isVisibleInCSP;
    private String isVisibleInApp;
   
    public BestPractice() {
    }

    public BestPractice(String articlenumber, String articletype, String title, String summary, Date createddate,
          Date firstpublisheddate, Date lastmodifieddate, Date lastpublisheddate, String originatingcase,
          String publishstatus,  String validationstatus, String bestpractice, String isvisibleincsp, String isvisibleinapp) {
       
       this.articleNumber = articlenumber;
       this.articleType = articletype;
        this.title = title;
        this.summary = summary;
        this.createdDate = createddate;
        this.firstPublishedDate = firstpublisheddate;
        this.lastModifiedDate = lastmodifieddate;
        this.lastPublishedDate = lastpublisheddate;
        this.originatingCase = originatingcase;
        this.publishStatus = publishstatus;
        this.validationStatus = validationstatus;
        this.bestPractice = bestpractice;
        this.isVisibleInApp = isvisibleinapp;
        this.isVisibleInCSP = isvisibleincsp;
       
              
       
    }

   @Id
   @GeneratedValue(strategy = IDENTITY)
   @Column(name = "ID", unique = true, nullable = false)
    public String getId(){
        return this.Id;
    }

    public void setId(String id){
        this.Id = id;
    }
   
    @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumn(name = "KnowledgeArticleId", nullable = false)
    @ContainedIn
   public KnowledgeArticle getKnowledgeArticle() {
      return this.knowledgeArticle;
   }

   public void setKnowledgeArticle(KnowledgeArticle knowledgearticle) {
      this.knowledgeArticle = knowledgearticle;
   }
   @Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
   @Column(name = "ARTICLETYPE", nullable = false, length=40)
    public String getArticleType() {
        return this.articleType;
    }

    public void setArticleType(String articletype) {
        this.articleType = articletype;
    }
    @Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
   @Column(name = "TITLE", nullable = false, length=255)
    public String getTitle() {
        return this.title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
   
    @Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
     @Column(name = "SUMMARY", nullable = false, length=255)
      public String getSummary() {
          return this.summary;
      }
   
      public void setSummary(String summary) {
          this.summary = summary;
      }
   
    @Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
   @Column(name = "ARTICLENUMBER", nullable = false, length=30)
    public String getArticleNumber() {
        return this.articleNumber;
    }

    public void setArticleNumber(String articlenumber) {
        this.articleNumber = articlenumber;
    }

   @Column(name = "CREATEDDATE", nullable = true)
    public Date getCreatedDate() {
        return this.createdDate;
    }

    public void setCreatedDate(Date createddate) {
        this.createdDate = createddate;
    }
   

   @Column(name = "FIRSTPUBLISHEDDATE", nullable = true)
    public Date getFirstPublishedDate() {
        return this.firstPublishedDate;
    }

    public void setFirstPublishedDate(Date firstpublisheddate) {
        this.firstPublishedDate = firstpublisheddate;
    }
   

      @Column(name = "LASTMODIFIEDDATE", nullable = true)
       public Date getlastModifiedDate() {
           return this.lastModifiedDate;
       }
   
       public void setLastModifiedDate(Date lastmodifieddate) {
           this.lastModifiedDate = lastmodifieddate;
       }
       

   @Column(name = "LASTPUBLISHEDDATE", nullable = true)
     public Date getlastPublishedDate() {
         return this.lastPublishedDate;
     }
 
     public void setLastPublishedDate(Date lastpublisheddate) {
         this.lastPublishedDate = lastpublisheddate;
     }

    @Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
   @Column(name = "ORIGINATING_CASE__C", nullable = false, length=80)
     public String getOriginatingCase() {
         return this.originatingCase;
     }
 
     public void setOriginatingCase(String originatingcase) {
         this.originatingCase = originatingcase;
     }

    @Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
   @Column(name = "PUBLISHSTATUS", nullable = false, length=30)
     public String getPublishStatus() {
         return this.publishStatus;
     }
 
     public void setPublishStatus(String publishstatus) {
         this.publishStatus = publishStatus;
     }
     
    @Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
   @Column(name = "VALIDATIONSTATUS", nullable = false, length=40)
     public String getValidationStatus() {
         return this.validationStatus;
     }
 
     public void setValidationStatus(String validationstatus) {
         this.validationStatus = validationstatus;
     }

     @Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
   @Column(name = "ISVISIBLEINCSP", nullable = false, length=30)
     public String getIsVisibleInCSP() {
         return this.isVisibleInCSP;
     }
 
     public void setIsVisibleInCSP(String isvisibleincsp) {
         this.isVisibleInCSP = isvisibleincsp;
     }
     
     @Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
   @Column(name = "ISVISIBLEINAPP", nullable = false, length=30)
     public String getIsVisibleInApp() {
         return this.isVisibleInApp;
     }
 
     public void setIsVisibleInApp(String isvisibleinapp) {
         this.isVisibleInApp = isvisibleinapp;
     }
     
    @Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
   @Column(name = "BEST_PRACTICES__C", nullable = false, length=30)
     public String getBestPractice() {
         return this.bestPractice;
     }
 
     public void setBestPractice(String bestpractice) {
         this.bestPractice = bestpractice;
     }


   
}


Code:

package com.nimsoft.ca.search;

import java.sql.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.search.annotations.Analyze;
import org.hibernate.search.annotations.ContainedIn;
import org.hibernate.search.annotations.DateBridge;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Resolution;
import org.hibernate.search.annotations.Store;

@Entity
@Indexed
@Table(name = "CASE_SUMMARY__KAV")
public class CaseSummary implements java.io.Serializable{

    /**
    *
    */
   private static final long serialVersionUID = 1L;
   
   private String Id;
    private KnowledgeArticle knowledgeArticle;
    private String articleNumber;
    private String articleType;
    private String title;
    private String summary;
    private Date createdDate;
    private Date firstPublishedDate;
    private Date lastModifiedDate;
    private Date lastPublishedDate;
    private String originatingCase;
    private String publishStatus;
    private String validationStatus;
    private String resolution;
    private String isVisibleInCSP;
    private String isVisibleInApp;
   
    public CaseSummary() {
    }

    public CaseSummary(String articlenumber, String articletype, String title, String summary, Date createddate,
          Date firstpublisheddate, Date lastmodifieddate, Date lastpublisheddate, String originatingcase,
          String publishstatus,  String validationstatus, String resolution, String isvisibleincsp,
          String isvisibleinapp) {
       
       this.articleNumber = articlenumber;
       this.articleType = articletype;
        this.title = title;
        this.summary = summary;
        this.createdDate = createddate;
        this.firstPublishedDate = firstpublisheddate;
        this.lastModifiedDate = lastmodifieddate;
        this.lastPublishedDate = lastpublisheddate;
        this.originatingCase = originatingcase;
        this.publishStatus = publishstatus;
        this.validationStatus = validationstatus;
        this.resolution = resolution;
        this.isVisibleInApp = isvisibleinapp;
        this.isVisibleInCSP = isvisibleincsp;
       
              
       
    }

   @Id
   @GeneratedValue(strategy = IDENTITY)
   @Column(name = "ID", unique = true, nullable = false)
    public String getId(){
        return this.Id;
    }

    public void setId(String id){
        this.Id = id;
    }
   
    @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumn(name = "KnowledgeArticleId", nullable = false)
    @ContainedIn
   public KnowledgeArticle getKnowledgeArticle() {
      return this.knowledgeArticle;
   }

   public void setKnowledgeArticle(KnowledgeArticle knowledgearticle) {
      this.knowledgeArticle = knowledgearticle;
   }
   @Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
   @Column(name = "ARTICLETYPE", nullable = false, length=40)
    public String getArticleType() {
        return this.articleType;
    }

    public void setArticleType(String articletype) {
        this.articleType = articletype;
    }
    @Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
   @Column(name = "TITLE", nullable = false, length=255)
    public String getTitle() {
        return this.title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
   
    @Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
   @Column(name = "SUMMARY", nullable = false, length=255)
    public String getSummary() {
        return this.summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }
   
    @Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
   @Column(name = "ARTICLENUMBER", nullable = false, length=30)
    public String getArticleNumber() {
        return this.articleNumber;
    }

    public void setArticleNumber(String articlenumber) {
        this.articleNumber = articlenumber;
    }

   @Column(name = "CREATEDDATE", nullable = true)
    public Date getCreatedDate() {
        return this.createdDate;
    }

    public void setCreatedDate(Date createddate) {
        this.createdDate = createddate;
    }
   

   @Column(name = "FIRSTPUBLISHEDDATE", nullable = true)
    public Date getFirstPublishedDate() {
        return this.firstPublishedDate;
    }

    public void setFirstPublishedDate(Date firstpublisheddate) {
        this.firstPublishedDate = firstpublisheddate;
    }
   

      @Column(name = "LASTMODIFIEDDATE", nullable = true)
       public Date getlastModifiedDate() {
           return this.lastModifiedDate;
       }
   
       public void setLastModifiedDate(Date lastmodifieddate) {
           this.lastModifiedDate = lastmodifieddate;
       }
       

   @Column(name = "LASTPUBLISHEDDATE", nullable = true)
     public Date getlastPublishedDate() {
         return this.lastPublishedDate;
     }
 
     public void setLastPublishedDate(Date lastpublisheddate) {
         this.lastPublishedDate = lastpublisheddate;
     }

    @Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
   @Column(name = "ORIGINATING_CASE__C", nullable = false, length=80)
     public String getOriginatingCase() {
         return this.originatingCase;
     }
 
     public void setOriginatingCase(String originatingcase) {
         this.originatingCase = originatingcase;
     }

    @Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
   @Column(name = "PUBLISHSTATUS", nullable = false, length=30)
     public String getPublishStatus() {
         return this.publishStatus;
     }
 
     public void setPublishStatus(String publishstatus) {
         this.publishStatus = publishStatus;
     }
     
    @Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
   @Column(name = "VALIDATIONSTATUS", nullable = false, length=40)
     public String getValidationStatus() {
         return this.validationStatus;
     }
 
     public void setValidationStatus(String validationstatus) {
         this.validationStatus = validationstatus;
     }

     @Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
   @Column(name = "ISVISIBLEINCSP", nullable = false, length=30)
     public String getIsVisibleInCSP() {
         return this.isVisibleInCSP;
     }
 
     public void setIsVisibleInCSP(String isvisibleincsp) {
         this.isVisibleInCSP = isvisibleincsp;
     }
   
     
    @Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
   @Column(name = "RESOLUTION__C", nullable = false)
     public String getResolution() {
         return this.resolution;
     }
 
     public void setResolution(String resolution) {
         this.resolution = resolution;
     }


   
}




Code:

package com.nimsoft.ca.search;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.hibernate.search.query.dsl.QueryBuilder;

import org.hibernate.Session;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;




import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

public class ArticleSearch extends HttpServlet {
   
   private static void doIndex() throws InterruptedException {       
      Session session = HibernateUtil.getSession();                 
      FullTextSession fullTextSession = Search.getFullTextSession(session);       
      fullTextSession.createIndexer().startAndWait();                 
      fullTextSession.close();   
      }         
   
 
    private static List<KnowledgeArticle> searchKnowledgeArticles(String queryString) {       
      Session session = HibernateUtil.getSession();       
      FullTextSession fullTextSession = Search.getFullTextSession(session);                 
      QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(KnowledgeArticle.class).get();       
      org.apache.lucene.search.Query luceneQuery = queryBuilder.keyword().onFields("articleNumber").matching(queryString).createQuery();         
      // wrap Lucene query in a javax.persistence.Query       
      org.hibernate.Query fullTextQuery = fullTextSession.createFullTextQuery(luceneQuery, KnowledgeArticle.class);                 
      List<KnowledgeArticle> knowledgeArticlesList = fullTextQuery.list();                 
      fullTextSession.close();                 
      return knowledgeArticlesList;   
      } 
   private static List<BestPractice> searchBestPractice(String queryString) {       
      Session session = HibernateUtil.getSession();       
      FullTextSession fullTextSession = Search.getFullTextSession(session); 
      QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(BestPractice.class).get(); 
      org.apache.lucene.search.Query luceneQuery = queryBuilder.keyword().onFields("articleNumber","title","summary","articleType","originatingCase","publishStatus","validationStatus","bestPractice").matching(queryString).createQuery();         
      // wrap Lucene query in a javax.persistence.Query
      
      org.hibernate.search.FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(luceneQuery, BestPractice.class);                 
      org.apache.lucene.search.Sort sort = new Sort(new SortField("title",SortField.STRING));
      
      fullTextQuery.setSort(sort);
      List<BestPractice> bestPracticeList = fullTextQuery.list();                 
      fullTextSession.close();                 
      return bestPracticeList;   
      } 
   
   private static List<CaseSummary> searchCaseSummary(String queryString) {       
      Session session = HibernateUtil.getSession();       
      FullTextSession fullTextSession = Search.getFullTextSession(session);                 
      QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(CaseSummary.class).get();       
      org.apache.lucene.search.Query luceneQuery = queryBuilder.keyword().onFields("articleNumber","title","summary","articleType","originatingCase","publishStatus","validationStatus","resolution").matching(queryString).createQuery();         
      // wrap Lucene query in a javax.persistence.Query       
      org.hibernate.Query fullTextQuery = fullTextSession.createFullTextQuery(luceneQuery, CaseSummary.class);                 
      List<CaseSummary> caseSummaryList = fullTextQuery.list();                 
      fullTextSession.close();                 
      return caseSummaryList;   
      } 
   
  protected void doGet(HttpServletRequest request,
      HttpServletResponse response) throws ServletException, IOException
  {
    // reading the user input
    String searchstring= request.getParameter("searchstring");   
    String articlenumber = request.getParameter("articlenumber");
    String lastPublishedDays = request.getParameter("lastPublishedDays");
    String isVisibleInCSP = request.getParameter("isVisibleInCSP");
    String isVisibleInApp = request.getParameter("isVisibleInApp");
    String validationStatus = request.getParameter("validationStatus");
    String checkCtr = request.getParameter("check_ctr");
    Enumeration keys = request.getParameterNames(); 
    String[] valueArray = request.getParameterValues("articletypes");
   
     
    PrintWriter out = response.getWriter();
 
    out.println ("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">");
    out.println ("<html>");
    out.println ("<head>");
    out.println ("<link href=\"styles.css\" rel=\"stylesheet\">");
    out.println ("<meta charset=\"utf-8\">\n");
    out.println ("<title>KnowledgeBAse SEarch - for Nimsoft Support Articles</title>\n");
    out.println ("<SCRIPT LANGUAGE=\"JavaScript\">\n");
    out.println ("<!--\n");
    out.println (" function Check(chk)\n");
    out.println ("{\n");
    out.println (" if(document.myform.check_ctr.checked==true){\n");
    out.println ("for (i = 0; i < chk.length; i++)\n");
    out.println ("chk[i].checked = true ;\n");
    out.println (" }else\n");
    out.println ("{\n");
    out.println ("for (i = 0; i < chk.length; i++)\n");
    out.println ("chk[i].checked = false ;\n");
    out.println ("}\n");
    out.println ("}\n");
    out.println ("// End -->\n");
    out.println ("</script>\n");
    out.println ("</head>\n");
    out.println ("<body>\n");
    out.println ("<article>");
    out.println ("<section>");
    out.println ("<table border=\"0\">\n");
    out.println ("<tr>\n");
    out.println ("<td>\n");
    out.println ("<img src=\"CA_r_Primary_RGB_web_72x64.png\"></img>\n");
    out.println ("</td>\n");
    out.println ("<td>\n");
    out.println ("<header>\n");
    out.println ("<h2> KnowledgeBAse SEarch - Nimsoft Support Articles </h2>\n");
    out.println ("</header>\n");
    out.println ("</td>\n");
    out.println ("</tr>\n");
    out.println ("</table>\n");
    out.println ("<p>\n");
   
    out.println ("<form name=\"myform\" action=\"ArticleSearchServlet\">\n");
    out.println ("<b>Specify a Search String and Press Submit</b></br> (searches all published articles and all article types -- keyword search terms or'ed)</br>");
    out.println ("<input type=\"text\" size=120 name=\"searchstring\"></br>");

    out.println ("<br/><b>OR<br/><br/>Choose Article Types and/or Days Since Last Published and Press Submit</b><br/>");
    out.println ("</br><input type=\"checkbox\" name=\"check_ctr\" value=\"yes\" checked onClick=\"Check(document.myform.articletypes)\"><b>All</b></td><br/>\n");
    out.println ("<input type=\"checkbox\" name=\"articletypes\" value=\"bestpractices\" checked  /> Best Practices\n");
    out.println ("<input type=\"checkbox\" name=\"articletypes\" value=\"casesummaries\" checked /> Case Summaries\n");
    out.println ("<input type=\"checkbox\" name=\"articletypes\" value=\"currenthotfixesandalerts\" checked />Current Hotfixes & Alerts\n");
    out.println ("<input type=\"checkbox\" name=\"articletypes\" value=\"customersolutions\" checked />Customer Solutions \n");
    out.println ("<input type=\"checkbox\" name=\"articletypes\" value=\"frequentlyaskedquestions\" checked />Frequently Asked Questions\n</br>");
    out.println ("<input type=\"checkbox\" name=\"articletypes\" value=\"howtoprocedures\" checked />How-To Procedures\n");
    out.println ("<input type=\"checkbox\" name=\"articletypes\" value=\"internaltrainings\" checked />Internal Training\n");
    out.println ("<input type=\"checkbox\" name=\"articletypes\" value=\"productdocumentation\" checked />Product Documentation\n");
    out.println ("<input type=\"checkbox\" name=\"articletypes\" value=\"technicalbriefs\" checked />Technical Briefs\n");
    out.println ("<input type=\"checkbox\" name=\"articletypes\" value=\"troubleshooting\" checked />Troubleshooting\n");






    out.println ("</br></br><labelfor=\"lastPublishedDays\"><b>Days since last published</b></label>");
    out.println ("<input type=\"number\" size=\"4\" maxlength=\"4\" value=\"60\" name=\"lastPublishedDays\"> ");

/*   
    out.println ("<br/><labelfor=\"isVisibleInCSP\">Is Visible on Customer Portal </label>\n");
    out.println ("<select name=isVisibleInCSP>");
    out.println ("<option value=\"true\">True</option>");
    out.println ("<option value=\"false\">False</option>");
    out.println ("</select>");
   
   
    out.println ("<br/><labelfor=\"validationStatus\">Validation Status </label>\n");
    out.println ("<select name=validationStatus>");
    out.println ("<option value=\"authored\">Authored</option>");
    out.println ("<option value=\"publishedImmediate\">Published Immediate</option>");
    out.println ("<option value=\"publishedReviewed\">Published And Reviewed</option>");
    out.println ("</select>");*/
   
    out.println ("<br/><br/><input type=\"submit\" value=\"Submit\">\n");
    out.println ("</form></br>\n");
    out.println ("</section>");
    out.println ("</article>");
// TODO figure out when to reindex
   /*try {
      doIndex();
   } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
   } */ 

      
   


      //}   
   Session session = null; 
   if (articlenumber == null){
      
      if (searchstring != null){ // first time call to servlet was returning nullValueException
         
                   
         out.println("<table border=\"1\">");
         out.println("<tr><td><b>Article Number</b></td><td><b>Title</b></td><td><b>Article Type</b></td><td><b>First Published</b></td><td><b>Last Published</b></td></tr>" );           

   
                  if (searchstring.isEmpty()){ // Search String is Empty so we do a hibernate query to return all articles that meet specified criteria.
                     try {           
                        session = HibernateUtil.getSession();   
                         //out.println ("<b>Your Search String was empty and your other selections were ... </b></br></br>");
                      
                        List<KnowledgeArticle> knowledgeArticles = session.createQuery("from KnowledgeArticle where archiveddate is null and isdeleted ='false' and LastPublishedDate > DATEADD(d,-" +
                        lastPublishedDays + ",GETDATE()) order by lastpublisheddate desc").list();
                   
                        int kbCount = 0;
                        if (knowledgeArticles.isEmpty()){
                           out.println("<tr><td colspan=\"5\">No Knowledge Articles Returned" + "</td></tr>");
                        }
                        
                        int casesummfound= 0;
                        int bestpracfound = 0;
                        int casesummreturned= 0;
                        int bestpracreturned = 0;
                        for (KnowledgeArticle knowledgeArticle : knowledgeArticles) {
                        
                           
                             
//output rows base on article types chosen
                           
                           if (!knowledgeArticle.getCaseSummaries().isEmpty()){
                              casesummfound = casesummfound + 1;

                                 for (int i=0;i < valueArray.length ;i++){
                                    if (valueArray[i].toString().equalsIgnoreCase("casesummaries")){
                                       List<CaseSummary> caseSummaries = new ArrayList(knowledgeArticle.getCaseSummaries());
                                         for (CaseSummary caseSummary:caseSummaries){
                                        
                                              out.println("<tr><td>" + caseSummary.getArticleNumber() + "</td><td>"  + "<a href=\"ArticleSearchServlet?articlenumber=" + caseSummary.getArticleNumber()
                                                    + "\">" + caseSummary.getTitle() + "</a>" + "</td><td>" + caseSummary.getArticleType() + "</td><td>" + caseSummary.getFirstPublishedDate()
                                                    + "</td><td>" + caseSummary.getlastPublishedDate() + "</td></tr>");
                                              casesummreturned = casesummreturned + 1;
                                          
                                       }
                                   }
                              }
                           }
                           
                           if (!knowledgeArticle.getBestPractices().isEmpty()){
                              bestpracfound = bestpracfound + 1;
                              for (int i=0;i < valueArray.length ;i++){
                                    if (valueArray[i].toString().equalsIgnoreCase("bestpractices")){
                                       List<BestPractice> bestPractices = new ArrayList(knowledgeArticle.getBestPractices());
                                            
                                       for (BestPractice bestPractice:bestPractices){
                                      
                                              out.println("<tr><td>" + bestPractice.getArticleNumber() + "</td><td>"  + "<a href=\"ArticleSearchServlet?articlenumber=" + bestPractice.getArticleNumber()
                                                    + "\">" + bestPractice.getTitle() + "</a>" + "</td><td>" + bestPractice.getArticleType() + "</td><td>" + bestPractice.getFirstPublishedDate()
                                                    + "</td><td>" + bestPractice.getlastPublishedDate() + "</td></tr>");
                                              bestpracreturned = bestpracreturned + 1;
                                          
                                       }
                                   }
                              }
                           }
                          }
                     
                     
                  
                        out.println("<tr><td colspan=\"5\">Of <b>" + knowledgeArticles.size() + " </b>Knowledge Articles published in the last <b>" + lastPublishedDays + " </b>days, <b>" + bestpracfound  + "</b> Best Practice articles were found and <b>" + bestpracreturned + "</b> Best Practice articles are listed above</td></tr>");
                        out.println("<tr><td colspan=\"5\">Of <b>" + knowledgeArticles.size() + " </b>Knowledge Articles published in the last <b>" + lastPublishedDays + " </b>days, <b>" + casesummfound  + "</b> Case Summary articles were found and <b>" + casesummreturned + "</b> Case Summary articles are listed above</td></tr>");
                           
                        out.println ("</table");
                     }
                     catch (Exception ex) {           
                        ex.printStackTrace();       
                     } finally{           
                        if(session != null) {               
                           session.close();           
                        }       
                     }
                  }
                  //
                  else { // search string is NOT empty
                     try {           
                        session = HibernateUtil.getSession();
                        
// Create a search for each article type chosen
                        
                                 
                        
                        
                        List<BestPractice> bestPractices = searchBestPractice(searchstring);

                        if (bestPractices.isEmpty()){
                           out.println("<tr><td colspan=\"5\">No Best Practice Articles Found for Search Criteria :" + searchstring + "</td></tr>");
                        }
                        else {
                                 
                        
                           for (BestPractice bestPractice : bestPractices) {
   
                                 out.println("<tr><td>" + bestPractice.getArticleNumber() + "</td><td>" + "<a href=\"ArticleSearchServlet?articlenumber=" + bestPractice.getArticleNumber() + "\">"
                                 + bestPractice.getTitle() + "</a>" + "</td><td>" + bestPractice.getArticleType() + "</td><td>" + bestPractice.getFirstPublishedDate() + "</td><td>" +
                                 bestPractice.getlastPublishedDate() + "</td></tr>");
                                    
                                    
                             }
                           out.println("<tr><td colspan=\"5\"><b>" + bestPractices.size() + "</b> Best Practice articles found with search string : <b>" + searchstring + "</b> and <b>" + bestPractices.size() + "</b> Best Practice articles listed above</td></tr>");

                        }
                        
                        List<CaseSummary> caseSummaries = searchCaseSummary(searchstring);

                        if (caseSummaries.isEmpty()){
                           out.println("<tr><td colspan=\"5\">No Case Summary Articles Found for Search Criteria :" + searchstring + "</td></tr>");
                        }
                        else {
                                 
                        
                           for (CaseSummary caseSummary : caseSummaries) {
   
                                 out.println("<tr><td>" + caseSummary.getArticleNumber() + "</td><td>" + "<a href=\"ArticleSearchServlet?articlenumber=" + caseSummary.getArticleNumber() + "\">" +
                                 caseSummary.getTitle() + "</a>" + "</td><td>" + caseSummary.getArticleType() + "</td><td>" + caseSummary.getFirstPublishedDate() + "</td><td>" +
                                       caseSummary.getlastPublishedDate() + "</td></tr>");
                                    
                                    
                             }
                           out.println("<tr><td colspan=\"5\"><b>" + caseSummaries.size() + "</b> Case Summary articles found with search string : <b>" + searchstring + "</b> and <b>" + caseSummaries.size() + "</b> Case Summary articles listed above</td></tr>");

                           out.println ("</table");
                        }
                     }
                     catch (Exception ex) {           
                        ex.printStackTrace();       
                     } finally{           
                        if(session != null) {               
                           session.close();           
                        }       
                     }
                  }
                //}//
                
             //}
         //}
      }
   
   out.println ("</font> \n\n");
    out.println ("</body> \n\n");
    out.println ("</html>\n");

   
   
   }
   else { //articleNumber is NOT NULL so we will render the article

      try { 

         session = HibernateUtil.getSession();
         
      
   
      
            out.println("<tr><td><hr width=\"100%\"><b><center><h2>Article Details and Content</h2></center></b><br><hr width=\"100%\"><br></td></tr>");
            System.out.println("something to debug to");
            List<KnowledgeArticle> knowledgeArticles = searchKnowledgeArticles(articlenumber);

                 for (KnowledgeArticle knowledgeArticle:knowledgeArticles){
                    if (knowledgeArticle.getBestPractices().size() > 0){
                       List<BestPractice> bestPractices = new ArrayList<BestPractice>(knowledgeArticle.getBestPractices());
                    
                       for (BestPractice bestPractice:bestPractices){
                                               
                        out.println("<table border=\"1\">");
                        
                         out.println ("<tr><td><b>Article Number</b></td><td>");
                         out.println (bestPractice.getArticleNumber() + "<br/></td></tr>");
                        
                         out.println ("<tr><td><b>Article Type</b></td><td>");
                         out.println (bestPractice.getArticleType() + "<br/></td></tr>");
                        
                         out.println ("<tr><td><b>Created Date</b></td><td>");
                         out.println (bestPractice.getCreatedDate() + "</td><tr>");
                        
                         out.println ("<tr><td><b>First Published Date</b></td><td>");
                         out.println (bestPractice.getFirstPublishedDate() + "</td><tr>");
                        
                         out.println ("<tr><td><b>Last Modified Date</b></td><td>");
                         out.println (bestPractice.getlastModifiedDate() + "</td><tr>");
                        
                         out.println ("<tr><td><b>Last Published</b></td><td>");
                         out.println (bestPractice.getlastPublishedDate() + "</td><tr>");
                        
                         out.println ("<tr><td><b>Originating Case</b></td><td>");
                         out.println (bestPractice.getOriginatingCase() + "</td><tr>");
                        
                         out.println ("<tr><td><b>Publish Status</b></td><td>");
                         out.println (bestPractice.getPublishStatus() + "</td><tr>");
                        
                         out.println ("<tr><td><b>Validation Status</b></td><td>");
                         out.println (bestPractice.getValidationStatus() + "</td><tr>");
                        
               
                         out.println ("</table>");
                        
                        
                         out.println ("<br/><b>Title:</b>");
                        out.println(bestPractice.getTitle() + "<br/><br/>");
                        
                        out.println ("<br/><b>Summary:</b>");
                        out.println(bestPractice.getSummary() + "<br/><br/>");
                        
                         out.println ("<br/><b>Resolution:</b>");
                        out.println(bestPractice.getBestPractice() + "<br/><br/>"); 
                       }
                    
               
                    }   
                    else if (knowledgeArticle.getCaseSummaries().size() > 0){
                       List<CaseSummary> caseSummaries = new ArrayList<CaseSummary>(knowledgeArticle.getCaseSummaries());
                    
                       for (CaseSummary caseSummary:caseSummaries){
                                               
                        out.println("<table border=\"1\">");
                        
                         out.println ("<tr><td><b>Article Number</b></td><td>");
                         out.println (caseSummary.getArticleNumber() + "<br/></td></tr>");
                        
                         out.println ("<tr><td><b>Article Type</b></td><td>");
                         out.println (caseSummary.getArticleType() + "<br/></td></tr>");
                        
                         out.println ("<tr><td><b>Created Date</b></td><td>");
                         out.println (caseSummary.getCreatedDate() + "</td><tr>");
                        
                         out.println ("<tr><td><b>First Published Date</b></td><td>");
                         out.println (caseSummary.getFirstPublishedDate() + "</td><tr>");
                        
                         out.println ("<tr><td><b>Last Modified Date</b></td><td>");
                         out.println (caseSummary.getlastModifiedDate() + "</td><tr>");
                        
                         out.println ("<tr><td><b>Last Published</b></td><td>");
                         out.println (caseSummary.getlastPublishedDate() + "</td><tr>");
                        
                         out.println ("<tr><td><b>Originating Case</b></td><td>");
                         out.println (caseSummary.getOriginatingCase() + "</td><tr>");
                        
                         out.println ("<tr><td><b>Publish Status</b></td><td>");
                         out.println (caseSummary.getPublishStatus() + "</td><tr>");
                        
                         out.println ("<tr><td><b>Validation Status</b></td><td>");
                         out.println (caseSummary.getValidationStatus() + "</td><tr>");
                        
               
                         out.println ("</table>");
                        
                        
                         out.println ("<br/><b>Title:</b>");
                        out.println(caseSummary.getTitle() + "<br/><br/>");
                        
                         out.println ("<br/><b>Summary:</b>");
                        out.println(caseSummary.getSummary() + "<br/><br/>");
                        
                         out.println ("<br/><b>Resolution:</b>");
                         System.out.println("Before :" + caseSummary.getResolution());
                         String mytext = caseSummary.getResolution().replaceAll("/(<br>/\t)+/", " ");
                         System.out.println("After : " + mytext);
                        out.println(caseSummary.getResolution() + "<br/><br/>"); 
                        mytext = mytext.replaceAll("( )+", " ");
                       }
                    
               
                    }
               }
               }

         
      catch (Exception ex) {           
         ex.printStackTrace();       
      } finally{           
         if(session != null) {               
            session.close();           
         }       
      }
   
   }
   
   
   
  } 
}



Code:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
        <session-factory>       
        <!-- Database connection settings -->
        <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
        <property name="connection.url">jdbc:sqlserver://138.42.135.63;databaseName=Salesforce Backups;</property>
        <property name="connection.username">sa</property>
        <property name="connection.password">Pr1st1n3</property>           
        <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>             
        <property name="show_sql">false</property>   
        <property name="format_sql">false</property>

        <property name="hibernate.search.default.directory_provider">filesystem</property>
        <property name="hibernate.search.default.indexBase">C:\lucene\indexes</property>

      <mapping class="com.nimsoft.ca.search.KnowledgeArticle"/>
      <mapping class="com.nimsoft.ca.search.BestPractice"/>
        <mapping class="com.nimsoft.ca.search.CaseSummary"/>
       

    </session-factory>
</hibernate-configuration>



Top
 Profile  
 
 Post subject: Re: search across ALL data in 1 to many
PostPosted: Wed Apr 17, 2013 9:20 pm 
Newbie

Joined: Fri Apr 05, 2013 3:28 pm
Posts: 9
I changed my query to the following:
Code:

Session session = HibernateUtil.getSession();       
      FullTextSession fullTextSession = Search.getFullTextSession(session);                 
      QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(KnowledgeArticle.class).get();       
      org.apache.lucene.search.Query luceneQuery = queryBuilder.keyword()
            .fuzzy()
            .withThreshold( .8f )
            .withPrefixLength( 1 )
.onFields("articleNumber","bestPractices.title","caseSummaries.title","bestPractices.summary","caseSummaries.summary","caseSummaries.resolution","bestPractices.bestPractice").matching(queryString).createQuery();         
   
      // wrap Lucene query in a javax.persistence.Query       
      org.hibernate.search.FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(luceneQuery, KnowledgeArticle.class, BestPractice.class, CaseSummary.class);             


and am able to search all of the fields that I am interested in.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.