简体   繁体   中英

Hibernate wild card phrase search results not optimal

I have the following annotated class search function (not complete, just trying to get it to work right now):

I am trying to get it so that when a user searches for "foo ba" the results will pop up with rows containing 'foo bar', 'foo bat' etc. but have those terms show in the same field. IE if the Title is 'foo' with an ID of 'bat' it will not show up, whereas a row with title 'foo bath' will. Right now if even one of the words shows up in any of the fields it is returned, and the sorting doesn't work at all either (but that is the next step so I'm not so worried about that for now).

    @Entity
    @Table(name="jobReq")
    @Indexed
    public class JobReq {

@Id
@DocumentId
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;

@Field(index = Index.YES)
@Column(name="jobId", nullable=false, unique=true)
private String jobId;

@Field(index = Index.YES)
@Column(name="jobTitle", nullable=false)
private String jobTitle;

@Field(index = Index.YES)
@Column(name="jobContract", nullable=false)
private String contract;

@Field(index = Index.YES)
@Column(name="jobProject", nullable=true)
private String project;

@Field(index = Index.YES)
@Column(name="jobLaborCategory", nullable=false)
private String laborCategory;

@Field(index = Index.YES)
@Column(name="jobSummary", nullable=false)
private String summary;

@Field(index = Index.YES)
@Column(name="jobDescription", nullable=false)
private String jobDescription;

@Field(index=Index.YES)
@Column(name="jobStatus", nullable=false)
private String status;

@Field(index = Index.YES)
@Column(name="TTONumber", nullable=false)
private String TTONumber;

@Field(index = Index.YES)
@Column(name="jobPostedDate", nullable=false)
@Type(type="date")
private Date postedDate;

@Field(index = Index.YES)
@Column(name="jobModifiedDate", nullable=false)
@Type(type="date")
private Date modifiedDate;


       public List<T> testingSearch() {
      Session hibernateSession = this.getSession();
      List<T> results;
        SortField field =new SortField("jobStatus", SortField.STRING);
        Sort sort = new Sort(field);
      FullTextSession fullTextSession = Search
            .getFullTextSession(hibernateSession);
      fullTextSession.beginTransaction();

      Term firstTerm = new Term("jobTitle", "entry");
      Term secondTerm = new Term("jobTitle", "ar");

      Term[] tTerms = new Term[] { firstTerm, secondTerm };
      MultiPhraseQuery multiPhrasequery = new MultiPhraseQuery();

      try {
         File index = new File("I:/com.portal.application.model.JobPosition");
         Directory indexDirectory = FSDirectory.open(index); 
         System.out.println(indexDirectory);
         PrefixTermEnum reader = new PrefixTermEnum(IndexReader.open(indexDirectory), secondTerm);
         System.out.println(reader.toString());

         List<Term> termList = new LinkedList<Term>();
         while (reader.docFreq() != -1) {
               Term t = reader.term();
               System.out.print(t);
            if (t.field().equals("jobTitle") ||  t.text().startsWith(secondTerm.text())) {
                termList.add(t);
            }
           reader.next();              
         }

         Term[] terms = termList.toArray(new Term[0]);
         multiPhrasequery.add(terms);

        org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(multiPhrasequery, this.type).setSort(sort);
             results = hibQuery.list();
        fullTextSession.getTransaction().commit();
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
         results = null;
      }

      return results;
   }

Fixed it, turns out I forgot to add in the first term to the search XD

Actual query text: FullTextQueryImpl(jobTitle:"entry (architect artist)")

Code change that fixed it all:

...........

      Term firstTerm = new Term("jobTitle", "entry");
    **  Term secondTerm = firstTerm.createTerm("ar");

      Term[] tTerms = new Term[] { firstTerm, secondTerm };
      MultiPhraseQuery multiPhrasequery = new MultiPhraseQuery();

      try {
         File index = new File("I:/com.rhc.rayport.model.JobReq");
         Directory indexDirectory = FSDirectory.open(index); 
         System.out.println(indexDirectory);
         PrefixTermEnum reader = new PrefixTermEnum(IndexReader.open(indexDirectory), secondTerm);
         System.out.println(reader.toString());

         List<Term> termList = new LinkedList<Term>();
         while (reader.docFreq() != -1) {
               Term t = reader.term();
               System.out.println(t);
            if (t.field().equals("jobTitle") ||  t.text().startsWith(secondTerm.text())) {
                termList.add(t);
            }
           reader.next();              
         }

         Term[] terms = termList.toArray(new Term[0]);
         multiPhrasequery.add(firstTerm);
       **  multiPhrasequery.add(terms);

................

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM