Skip to content

Commit

Permalink
Ajeitando a ordenação da busca por "relevância"
Browse files Browse the repository at this point in the history
  • Loading branch information
sfohart committed Mar 7, 2014
1 parent 6f52bad commit dd9a78c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,13 @@ public class OhLohProjectEntity implements BaseEntity<Long> {
@Column(name = "average_rating")
private Double averageRating;


@Field(name = "rating_count", index=Index.YES, analyze=Analyze.YES, store=Store.NO, termVector = TermVector.YES)
@Column(name = "rating_count")
private Long ratingCount;


@Field(name = "review_count", index=Index.YES, analyze=Analyze.YES, store=Store.NO, termVector = TermVector.YES)
@Column(name = "review_count")
private Long reviewCount;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package br.ufba.dcc.mestrado.computacao.repository.impl;

import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
Expand All @@ -10,8 +13,10 @@

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.TermQuery;
import org.hibernate.search.errors.EmptyQueryException;
import org.hibernate.search.jpa.FullTextEntityManager;
Expand All @@ -22,7 +27,6 @@
import org.hibernate.search.query.facet.FacetingRequest;
import org.springframework.stereotype.Repository;

import br.ufba.dcc.mestrado.computacao.entities.ohloh.project.OhLohLicenseEntity;
import br.ufba.dcc.mestrado.computacao.entities.ohloh.project.OhLohProjectEntity;
import br.ufba.dcc.mestrado.computacao.entities.ohloh.project.OhLohTagEntity;
import br.ufba.dcc.mestrado.computacao.repository.base.OhLohProjectRepository;
Expand Down Expand Up @@ -132,24 +136,46 @@ public FullTextQuery findAllByFullTextQuery(String query) {
org.apache.lucene.search.Query luceneQuery = queryBuilder
.keyword()
.onField(SearchFieldsEnum.projectName.fieldName())
.boostedTo(SearchFieldsEnum.projectName.boost())
// .boostedTo(SearchFieldsEnum.projectName.boost())
.andField(SearchFieldsEnum.projectDescription.fieldName())
.boostedTo(SearchFieldsEnum.projectDescription.boost())
// .boostedTo(SearchFieldsEnum.projectDescription.boost())
.andField(SearchFieldsEnum.tagName.fieldName())
.boostedTo(SearchFieldsEnum.tagName.boost())
// .boostedTo(SearchFieldsEnum.tagName.boost())
.matching(query)
.createQuery();

fullTextQuery = fullTextEntityManager.createFullTextQuery(
luceneQuery,
OhLohProjectEntity.class);



configureRelevanceSort(fullTextQuery);


} catch (EmptyQueryException ex) {

}

return fullTextQuery;
}

private void configureRelevanceSort(FullTextQuery fullTextQuery) {
boolean reverse = true;
SortField userContSortField = new SortField(SearchFieldsEnum.projectUserCount.fieldName(), SortField.LONG, reverse);
SortField ratingContSortField = new SortField(SearchFieldsEnum.projectRatingCount.fieldName(), SortField.LONG, reverse);
SortField reviewContSortField = new SortField(SearchFieldsEnum.projectReviewCount.fieldName(), SortField.LONG, reverse);

List<SortField> sortFieldList = new LinkedList<>();
sortFieldList.addAll(Arrays.asList(Sort.RELEVANCE.getSort()));
sortFieldList.add(userContSortField);
sortFieldList.add(reviewContSortField);
sortFieldList.add(ratingContSortField);

Sort sort = new Sort(userContSortField);
fullTextQuery.setSort(sort);
}

/**
*
* Encontra projetos que possuam as mesmas tags que o projeto em questão
Expand Down Expand Up @@ -189,6 +215,8 @@ public FullTextQuery findRelatedProjects(OhLohProjectEntity project) throws IOEx

FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(booleanQuery, getEntityClass());

configureRelevanceSort(fullTextQuery);

return fullTextQuery;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
public enum SearchFieldsEnum {
projectName("name", 5.0f),
projectDescription("description", 2.0f),
projectUserCount("user_count",1.0f),
projectRatingCount("rating_count",1.0f),
projectReviewCount("review_count",1.0f),
linkCategory("ohLohLinks.category", 1.0f),
linkTitle("ohLohLinks.title", 1.0f),
linkURL("ohLohLinks.url", 1.0f),
Expand Down Expand Up @@ -35,6 +38,9 @@ public static String[] toArrayNames() {
String[] array = new String[] {
projectName.fieldName(),
projectDescription.fieldName(),
projectUserCount.fieldName(),
projectRatingCount.fieldName(),
projectReviewCount.fieldName(),
linkCategory.fieldName(),
linkTitle.fieldName(),
linkURL.fieldName(),
Expand Down
10 changes: 6 additions & 4 deletions pesquisa-orientada/mestrado-web/src/main/webapp/index.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@
value="#{facesContext.externalContext.applicationContextPath}/detail/projectDetail.jsf">
<f:param name="projectId" value="#{projectPreferenceInfo.project.id}" />
<h:outputText value="#{projectPreferenceInfo.project.name} " />
<h:outputText value="(#{projectPreferenceInfo.reviewsCount} avaliações)" rendered="#{projectPreferenceInfo.reviewsCount > 1}" />
<h:outputText value="(#{projectPreferenceInfo.reviewsCount} avaliação)" rendered="#{projectPreferenceInfo.reviewsCount eq 1}" />
<br />
<h:outputText styleClass="small" value="(#{projectPreferenceInfo.reviewsCount} avaliações)" rendered="#{projectPreferenceInfo.reviewsCount > 1}" />
<h:outputText styleClass="small" value="(#{projectPreferenceInfo.reviewsCount} avaliação)" rendered="#{projectPreferenceInfo.reviewsCount eq 1}" />
</h:outputLink>
<div class="progress progress-striped active" style="height: 10px">
<div class="progress-bar progress-bar-success" p:role="progressbar"
Expand Down Expand Up @@ -106,8 +107,9 @@
value="#{facesContext.externalContext.applicationContextPath}/detail/projectDetail.jsf">
<f:param name="projectId" value="#{pageViewInfo.project.id}" />
<h:outputText value="#{pageViewInfo.project.name} " />
<h:outputText value="(#{pageViewInfo.pageViewCount} visualizações)" rendered="#{pageViewInfo.pageViewCount > 1}" />
<h:outputText value="(#{pageViewInfo.pageViewCount} visualização)" rendered="#{pageViewInfo.pageViewCount eq 1}" />
<br />
<h:outputText styleClass="small" value="(#{pageViewInfo.pageViewCount} visualizações)" rendered="#{pageViewInfo.pageViewCount > 1}" />
<h:outputText styleClass="small" value="(#{pageViewInfo.pageViewCount} visualização)" rendered="#{pageViewInfo.pageViewCount eq 1}" />
</h:outputLink>
<div class="progress progress-striped active" style="height: 10px">
<div class="progress-bar progress-bar-info" p:role="progressbar"
Expand Down

0 comments on commit dd9a78c

Please sign in to comment.