Hibernate外卡短语搜索结果不是最优的

时间:2013-08-08 16:48:07

标签: java hibernate lucene hibernate-search

我有以下带注释的类搜索功能(不完整,只是试图让它立即工作):

我试图得到它,以便当用户搜索“foo ba”时,结果将弹出包含'foo bar','foo bat'等的行,但是这些术语显示在同一个字段中。 I.E.如果标题为“foo”且ID为“bat”,则不会显示,而标题为“foo bath”的行将显示。现在,如果即使其中一个单词出现在任何字段中,它也会被返回,并且排序根本不起作用(但这是下一步所以我现在对此并不那么担心)。 / p>

    @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;
   }

1 个答案:

答案 0 :(得分:0)

修正了它,结果我忘了在第一个词中添加到搜索XD

实际查询文本:FullTextQueryImpl(jobTitle:“entry(architect artist)”)

修正了所有内容的代码更改:

...........

      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);

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