2015-01-30 1 views
0

Sitecore CMS를 사용하여 상속 한 응용 프로그램이 있습니다. 우리는 최근 Sitecore를 업그레이드하여 Lucene.Net의 최신 버전을 사용해야했습니다. 우리 옛날 코드 중 일부가 망가졌습니다. 문제는 코드가 무엇을하려했는지 알 수 없다는 것입니다. Lucene 쿼리에 익숙하지 않습니다. 특히, RangeQueries가 TermRangeQueries가되어야한다는 것을 알고 있지만 BooleanQuery에 대한 대안을 찾을 수 없어 TermRangeQuery를 입력으로 받아 들일 수 없기 때문에이 코드를 다시 작성하면 어려워졌습니다. .Lucene의 새 버전으로 마이그레이션 할 때 RangeQuery를 사용하는 BooleanQuery를 어떻게 변환 할 수 있습니까?

 BooleanQuery lQuery = new BooleanQuery(); 
    lQuery.Add(new TermQuery(new Term("_shorttemplateid", string.Format("{0}", ShortID.Encode(templateId).ToLowerInvariant()))), 
       Lucene.Net.Search.Occur.MUST); 
    if (string.IsNullOrEmpty(endDateItemFieldName)) 
    { 
     lQuery.Add(
      new RangeQuery(
       new Term(startDateItemFieldName, startDateTime), 
       new Term(startDateItemFieldName, endDateTime), true), 
       Lucene.Net.Search.Occur.MUST); 
    } 
    else 
    { 
     lQuery.Add(
      new RangeQuery(
       new Term(startDateItemFieldName, startDate.ToString(DATE_TIME_FORMAT)), 
       new Term(startDateItemFieldName, string.Format("{0}{1}", endDate.ToString("yyyyMMdd"), endTimeStamp)), true), 
       Lucene.Net.Search.Occur.SHOULD); 
     lQuery.Add(
      new RangeQuery(
       new Term(endDateItemFieldName, startDate.ToString(DATE_TIME_FORMAT)), 
       new Term(endDateItemFieldName, string.Format("{0}{1}", endDate.ToString("yyyyMMdd"), endTimeStamp)), true), 
       Lucene.Net.Search.Occur.MUST); 
    } 
+0

Sitecore의 어떤 버전이로 업그레이드 했습니까? –

+0

우리는 6.6에서 7.2로 갈 것입니다. – hushidh

답변

0
이 작동 7+ Sitecore와 새로운 검색 API 어떻게 읽을 시간이 걸릴 먼저

:

http://www.sitecore.net/Learn/Blogs/Technical-Blogs/Sitecore-7-Development-Team/Posts/2013/06/Sitecore-7-POCO-Explained.aspx

https://www.sitecore.net/Learn/Blogs/Technical-Blogs/Sitecore-7-Development-Team/Posts/2013/04/Sitecore-7-Patterns-for-Global-Search-Context-Reuse.aspx

둘째, 당신의 예제 코드를 재 작성하려면 생성 다음 :

public class CustomSearchResult : Sitecore.ContentSearch.SearchTypes.SearchResultItem 
{ 
    [IndexField("START DATE FIELD NAME")] 
    public virtual DateTime EndDate {get; set;} 

    [IndexField("END DATE FIELD NAME")] 
    public virtual DateTime StartDate {get; set;} 
} 
(210)

이제, 당신은 다음과 같이 검색을 수행 할 수 있습니다

using Sitecore.ContentSearch; 
using Sitecore.ContentSearch.Linq; 
using Sitecore.ContentSearch.SearchTypes; 
using Sitecore.ContentSearch.Linq.Utilities 

using (var context = ContentSearchManager.GetIndex("sitecore_web_index").CreateSearchContext()) 
      { 
       var results = context.GetQueryable<CustomSearchResult>().Where(i => i.TemplateId == Sitecore.Data.ID.Parse("TEMPLATE GUID") && i.StartDate > StartDateObject && i.EndDate < EndDateObject).GetResults().Hits.Select(i => i.Document.GetItem()).ToList(); 
       return results; 
      } 

StartDateObjectEndDateObject 유형 날짜 시간이어야 있습니다.

희망이 도움이됩니다.

+0

철저한 설명에 감사드립니다! 한 가지 문제 - IndexField ("형식 또는 네임 스페이스 이름 'IndexFieldAttribute'를 찾을 수 없습니다 (사용 지시문이나 어셈블리 참조가 누락 되었습니까?))를 사용하려고하면 오류가 발생합니다. 어셈블리 참조가 누락되었다고 가정했지만 추가 한 내용은 아무 것도 없어 보이고 검색이 아무 것도 나타나지 않습니다. 어떤 아이디어? – hushidh

+0

포함 된 문서는 조만간 도움이 될 것입니다. 곧 웹 사이트의 전체 섹션을 완전히 다시 만들 계획이지만 잠시 동안 우리는 업그레이드가 올바르게 작동하도록 노력하고 있습니다. 우리는 이미 일정보다 몇 주가 늦었습니다! 그래서 저는 지금 찾을 수있는 가장 빠른 반창고를 찾고 있습니다. – hushidh

+0

Sitecore.ContentSearch.Linq 프로젝트에 대한 참조 추가 –

1

귀하의 예제 코드는 다음과 같은 논리를 사용하여 루씬 쿼리를 구축 :

의사 코드 : 장면 뒤에

Match all documents 

    That have a specific template ID 

    AND 

     IF an endDateItemFieldName is present 

      The start date must be between date X and Y 

     ELSE 

      The start date can be between date X and Y 
      But the end date must be between date X and Y 

하는 루씬 쿼리 결과이 비슷한 보이는 여기까지 :

+_shorttemplateid:3f2504e04f8941d39a0c0305e82c3301 start:[20020101 TO 20030101] +end:[20020101 TO 20030101] 

Sitecore 7+에서는 "Luceneness"의 대부분이 추상화되어 나와 생성되었습니다. b y LINQ 검색 공급자. 이를 통해 코드의 실질적인 리팩토링없이 검색 구현 (예 : Solr)간에 전환 할 수 있습니다. LINQ는 널리 알려져 있으므로 LINQ 공급자와 협력하는 것이 개발자가 파악하기가 훨씬 쉽습니다.

다음은 새 LINQ 공급자를 사용하는 동등한 검색 쿼리입니다.

using Sitecore.ContentSearch; 
using Sitecore.ContentSearch.Converters; 
using Sitecore.ContentSearch.SearchTypes; 
using System; 
using System.ComponentModel; 

namespace YourNamespace 
{ 
    public class DateSearchResultItem : SearchResultItem 
    { 
     [IndexField("startdate"), TypeConverter(typeof(IndexFieldDateTimeValueConverter))] 
     public DateTime StartDate { get; set; } 

     [IndexField("enddate"), TypeConverter(typeof(IndexFieldDateTimeValueConverter))] 
     public DateTime EndDate { get; set; } 
    } 
} 

그리고 예를 들어 사용 :

ISearchIndex index = ContentSearchManager.GetIndex("sitecore_web_index"); 
using (var context = index.CreateSearchContext()) 
{ 
    var results = context.GetQueryable<DateSearchResultItem>() 
     .Where(item => item.TemplateId == new ID(templateId)); 

    if (String.IsNullOrEmpty(endDateItemFieldName)) 
    { 
     results = results 
      .Where(item => item.StartDate >= startDateTime) 
      .Where(item => item.StartDate <= endDateTime); 
    } 
    else 
    { 
     results = results 
      .Where(item => item.EndDate >= startDateTime) 
      .Where(item => item.EndDate <= endDateTime); 
    } 

    var compiledQuery = results.GetResults(); 
    int totalMatches = compiledQuery.TotalSearchResults; 

    foreach (var hit in compiledQuery.Hits) 
    { 
     Item item = hit.Document.GetItem(); 
    } 
} 
+0

철저한 설명에 감사드립니다! 한 가지 문제 - IndexField ("형식 또는 네임 스페이스 이름 'IndexFieldAttribute'를 찾을 수 없습니다 (사용 지시문이나 어셈블리 참조가 누락 되었습니까?))를 사용하려고하면 오류가 발생합니다. 어셈블리 참조가 누락되었다고 가정했지만 추가 한 내용은 아무 것도 없어 보이고 검색이 아무 것도 나타나지 않습니다. 어떤 아이디어? – hushidh

+0

프로젝트가'Sitecore.Kernel','Sitecore.ContentSearch','Sitecore.ContentSearch.Linq'를 참조하고 있는지 확인할 수 있습니까? 그것들은 모두 필요한 것이어야합니다. –

+0

아, Sitecore가 누락되었습니다. 내용 검색. 참고 문헌에서 찾으십시오! – hushidh

관련 문제