2015-10-23 106 views
1

我想在Liferay 6.2中實現基於Lucene的索引和搜索。我有一個自定義服務構建器實體,我希望能夠搜索此實體的所有字段。但問題在於,它不會搜索所有索引字段,除非我在搜索框中明確鍵入field:value。現在看來似乎只是在Liferay的默認這是liferay lucene搜索不工作

Field.ASSET_CATEGORY_TITLES, Field.ASSET_TAG_NAMES, Field.COMMENTS, 
Field.CONTENT, Field.DESCRIPTION, Field.PROPERTIES, Field.TITLE, 
Field.URL, Field.USER_NAME 

搜索這是我的實體

<entity name="Sample" local-service="true" remote-service="true" table="sample"> 
     <column name="uuid_" type="String" /> 
     <column name="sampleDbId" type="long" primary="true" /> 
     <column name="sampleCollectionDbId" type="long" /> 
     <column name="biobankDbId" type="long" /> 
     <column name="hashedSampleId" type="String" /> 
     <column name="hashedIndividualId" type="String" /> 
     <column name="materialType" type="String" /> 
     <column name="container" type="String" /> 
     <column name="storageTemperature" type="String" /> 
     <column name="sampledTime" type="Date"/> 
     <column name="anatomicalPartOntology" type="String" /> 
     <column name="anatomicalPartOntologyVersion" type="String" /> 
     <column name="anatomicalPartOntologyCode" type="String" /> 
     <column name="anatomicalPartOntologyDescription" type="String" /> 
     <column name="anatomicalPartFreeText" type="String" /> 
     <column name="sex" type="String" /> 
     <column name="ageLow" type="long" /> 
     <column name="ageHigh" type="long" /> 
     <column name="ageUnit" type="String" /> 
     <column name="diseaseOntology" type="String" /> 
     <column name="diseaseOntologyVersion" type="String" /> 
     <column name="diseaseOntologyCode" type="String" /> 
     <column name="diseaseOntologyDescription" type="String" /> 
     <column name="diseaseFreeText" type="String" /> 
     <column name="countryOfOrigin" type="String" /> 
     <finder name="uuid" return-type="Collection"> 
      <finder-column name="uuid_" /> 
     </finder> 

</entity> 

我已經成功地索引的所有,我想搜索的領域,我可以看到它在盧克。

我索引它在我的索引如下:

public Hits search(long companyId, String keywords) throws SearchException{ 
     System.out.println("-----SampleLocalServiceImpl search called------"); 

     SearchContext searchContext = new SearchContext(); 
     searchContext.setAndSearch(false); 


     Map<String, Serializable> attributes = new HashMap<String, Serializable>(); 

     attributes.put("sampleCollectionName", keywords); 
     attributes.put("biobankName", keywords); 
     attributes.put("materialType", keywords); 
     attributes.put("container", keywords); 
     attributes.put("storageTemperature", keywords); 
     attributes.put("sampledTime", keywords); 
     attributes.put("anatomicalPartOntology", keywords); 
     attributes.put("anatomicalPartOntologyVersion", keywords); 
     attributes.put("anatomicalPartOntologyCode", keywords); 
     attributes.put("anatomicalPartOntologyDescription", keywords); 
     attributes.put("anatomicalPartFreeText", keywords); 
     attributes.put("sex", keywords); 
     attributes.put("ageLow", keywords); 
     attributes.put("ageHigh", keywords); 
     attributes.put("ageUnit", keywords); 
     attributes.put("diseaseOntology", keywords); 
     attributes.put("diseaseOntologyVersion", keywords); 
     attributes.put("diseaseOntologyCode", keywords); 
     attributes.put("diseaseOntologyDescription", keywords); 
     attributes.put("diseaseFreeText", keywords); 
     attributes.put("countryOfOrigin", keywords); 

     searchContext.setAttributes(attributes); 
     searchContext.setCompanyId(companyId); 
     searchContext.setKeywords(keywords); 
     System.out.println(searchContext.getAttributes()); 

     QueryConfig queryConfig = new QueryConfig(); 

     queryConfig.setHighlightEnabled(false); 
     queryConfig.setScoreEnabled(false); 
     //searchContext.setAttribute("materialType:", keywords); 
     searchContext.setQueryConfig(queryConfig); 

     System.out.println(searchContext.getCompanyId()); 
     Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(
       Sample.class); 
     System.out.println("-----SampleLocalServiceImpl search called------"+indexer.getFullQuery(searchContext)); 
     System.out.println("-----SampleLocalServiceImpl search called------"+indexer); 


     return indexer.search(searchContext); 
    } 

我打電話從我的jsp這種搜索方式:

@Override 
    protected Document doGetDocument(Object obj) throws Exception { 
     // TODO Auto-generated method stub 
     System.out.println("-----doGetDocument called------"); 
     Sample sample = (Sample)obj; 

     Document document = getBaseModelDocument(PORTLET_ID, sample); 

     if(sample.getSampleCollectionDbId() > 0){ 
      document.addText("sampleCollectionName", SampleCollectionLocalServiceUtil.getSampleCollection(sample.getSampleCollectionDbId()).getName()); 
     } 
     document.add(new Field("biobankName", BiobankGeneralInformationLocalServiceUtil.getBiobankGeneralInformation(sample.getBiobankDbId()).getBiobankName())); 
     document.add(new Field("materialType", sample.getMaterialType())); 
     //document.addText("biobankName", BiobankGeneralInformationLocalServiceUtil.getBiobankGeneralInformation(sample.getBiobankDbId()).getBiobankName()); 
     //document.addKeyword("materialType", sample.getMaterialType()); 
     document.addKeyword("container", sample.getContainer()); 
     document.addText("storageTemperature", sample.getStorageTemperature()); 
     document.addDate("sampledTime", sample.getSampledTime()); 
     document.addText("anatomicalPartOntology", sample.getAnatomicalPartOntology()); 
     document.addKeyword("anatomicalPartOntologyVersion", sample.getAnatomicalPartOntologyVersion()); 
     document.addKeyword("anatomicalPartOntologyCode", sample.getAnatomicalPartOntologyCode()); 
     document.addText("anatomicalPartOntologyDescription", sample.getAnatomicalPartOntologyDescription()); 
     document.addText("anatomicalPartFreeText", sample.getAnatomicalPartFreeText()); 
     document.addKeyword("sex", sample.getSex()); 
     document.addNumber("ageLow", sample.getAgeLow()); 
     document.addNumber("ageHigh", sample.getAgeHigh()); 
     document.addText("ageUnit", sample.getAgeUnit()); 
     document.addText("diseaseOntology", sample.getDiseaseOntology()); 
     document.addKeyword("diseaseOntologyVersion", sample.getDiseaseOntologyVersion()); 
     document.addKeyword("diseaseOntologyCode", sample.getDiseaseOntologyCode()); 
     document.addText("diseaseOntologyDescription", sample.getDiseaseOntologyDescription()); 
     document.addText("diseaseFreeText", sample.getDiseaseFreeText()); 
     document.addKeyword("countryOfOrigin", sample.getCountryOfOrigin()); 

     return document; 
    } 

我也以我的localserviceimpl類中定義的點擊搜索方法通過localserviceutil類。

因此,當我在搜索框中鍵入blood,我沒有得到任何結果,但是當我輸入materialType:blood時,我得到了結果。

  • 對於emtpy搜索,查詢日誌給我 +(+((+(entryClassName:com.xxx.portlet.xxx.model.Sample))))(使整個結果集)
  • 如果我型血,查詢日誌給我

    +(+((+(entryClassName:com.xxx.portlet.xxx.model.Sample)))) +(assetCategoryTitles:*blood* assetTagNames:*blood* comments:blood content:blood description:blood properties:blood title:blood url:blood userName:*blood*)

    (查詢結果爲空)

  • 如果輸入materialType:blood,查詢日誌會給我 +(+((+(entryClassName:com.xxx.portlet.xxx.model.Sample)))) +(materialType:*blood* materialType:blood)(覈對結果集)

我想要的是用戶能夠在框中鍵入自由文本並獲取匹配結果。但目前這不起作用,因爲自由文本搜索只是搜索liferay默認字段。我怎樣才能解決這個問題?

該問題也發佈在here

+0

是什麼讓你覺得'SearchContext.attributes'和'Document.fields'映射到相同?您應該檢查索引器的「搜索」方法,以瞭解它如何將搜索上下文映射到字段。 –

+0

您的意思是我正在擴展的BaseIndexer。 – SASM

+0

我最終看到'LoceneceneIndexSearcher.search'被稱爲使用Facet的地方。我必須使用Facet來完成這項工作嗎?我正在關注搜索和索引的liferay文檔,其中沒有使用任何方面。 – SASM

回答

1

localserviceimpl類別的Hits search method中設置searchContext.setAttributes(attributes);是不夠的。我也必須覆蓋我的indexer類中的postProcessSearchQuery方法。因此,在我的indexer課程中添加以下方法後,我開始工作。

@Override 
    public void postProcessSearchQuery(BooleanQuery searchQuery, SearchContext searchContext) 
     throws Exception { 
     System.out.println("-----postProcessSearchQuery called------"); 

     addSearchTerm(searchQuery, searchContext, "sampleCollectionName", true); 
     addSearchTerm(searchQuery, searchContext, "biobankName", true); 
     addSearchTerm(searchQuery, searchContext, "materialType", true); 
     addSearchTerm(searchQuery, searchContext, "container", true); 
     addSearchTerm(searchQuery, searchContext, "storageTemperature", true); 
     ..... 
     ..... 

     LinkedHashMap<String, Object> params = 
      (LinkedHashMap<String, Object>)searchContext.getAttribute("params"); 

     if (params != null) { 
      String expandoAttributes = (String)params.get("expandoAttributes"); 

      if (Validator.isNotNull(expandoAttributes)) { 
       addSearchExpando(searchQuery, searchContext, expandoAttributes); 
      } 
     } 
    }