2016-08-05 2 views
2

현재 특정 필드에서 최대 n 단어의 문서를 반환 할 방법을 찾고 있습니다.단어 계산을위한 탄성 검색 쿼리 필터

쿼리는 "이름"필드에 3 단어 미만의 문서가 포함 된 결과 집합에 대해 이와 유사하지만 word_count와 같은 것이 없습니다.

다른 사람이이를 처리하는 방법을 알고 있습니까?

GET myindex/myobject/_search 
{ 
    "query": { 
    "filtered": { 
     "filter": { 
     "bool": { 
      "must": [ 
      { 
       "word_count": { 
       "name": { 
        "lte": 3 
       } 
       } 
      } 
      ] 
     } 
     }, 
     "query": { 
     "match_all" : { } 
     } 
    } 
    } 
} 

답변

2

당신은 주어진 필드에 색인을 위해 토큰의 수를 token_count 데이터 유형을 사용할 수 있습니다 다음 해당 필드에 검색 할 수 있습니다.

# 1. create the index/mapping with a token_count field 
PUT myindex 
{ 
    "mappings": { 
    "myobject": { 
     "properties": { 
     "name": { 
      "type": "string", 
      "fields": { 
      "word_count": { 
       "type":  "token_count", 
       "analyzer": "standard" 
      } 
      } 
     } 
     } 
    } 
    } 
} 

# 2. index some documents 

PUT index/myobject/1 
{ 
    "name": "The quick brown fox" 
} 
PUT index/myobject/2 
{ 
    "name": "brown fox" 
} 

# 3. the following query will only return document 2 
POST myindex/_search 
{ 
    "query": { 
    "range": { 
     "name.word_count": {  
     "lt": 3 
     } 
    } 
    } 
} 
+0

이것은 가능한 해결책으로 들립니다. 아마 모든 문서를 다시 색인해야하지만 이것은 괜찮습니다. 도와 줘서 고마워. – Jesse

+0

예, 'name.word_count' 필드를 채우기 위해 새 색인을 생성하고 색인을 다시 생성해야합니다. – Val