2017-09-09 1 views
1

목록의 길이로 검색을 정렬하려고합니다. 나는 필드가 지금처럼 보일 수 있습니다이 필드를 기준으로 정렬 할Elasticsearch 목록의 길이를 기준으로 정렬하는 방법

"authors": { 
    "type": "text", "norms": { "enabled": False }, "analyzer": "autocomplete_with_asciifolding", "search_analyzer": "with_and_without_accent_search", "position_increment_gap": 100, 
    "fields": { 
     "raw" : { # This field is needed for your_paper suggestions search 
      "type" : "string", 
      "analyzer" : 'lower_keyword' 
     } 
    } 
} 

에 의해 정의 [ '홍길동', '마이크 스미스']를 elasticsearch 문서에 따라

및 일부 Google I를 검색

doc['sort'] = { 
      "_script": { 
       "type": "number", 
       "script": "doc['authors'].length", 
       "order": "asc" 
      } 
     } 

나는 또한 시도 의사 [ '저자']. values.length() 및 문서 [ '저자']. values.size()을 찾았지만, 그들 모두는

TransportError: TransportError(500, u'search_phase_execution_exception') 
0 발생

목록 길이별로 정렬하는 방법을 알고 싶습니까?

답변

1

성능상의 이유로 이 (가) 있으므로 doc['authors']에 액세스 할 수 없습니다. authors.raw 대신으로 정렬

"authors": { 
    "type": "text", "norms": { "enabled": False }, "analyzer": "autocomplete_with_asciifolding", "search_analyzer": "with_and_without_accent_search", "position_increment_gap": 100, 
    "fields": { 
     "raw" : { # This field is needed for your_paper suggestions search 
      "type" : "string", 
      "analyzer" : 'lower_keyword', 
      "fielddata": True 
     } 
    } 
} 

그리고 :

는 당신은, 예를 들어, 내부 필드 raw에 진정한 = fielddata 설정할 수 있습니다

doc['sort'] = { 
      "_script": { 
       "type": "number", 
       "script": "doc['authors.raw'].length", 
       "order": "asc" 
      } 
     } 

또 다른 해결책은 새로운 내부 필드를 생성하는 것입니다 형식이 keyword이고 정렬하십시오. 그러나 나는 분석기가 케이스를 낮추어주기 때문에 raw을 사용하도록 제안했습니다.

관련 문제