2013-07-28 17 views
10

特定のフィールドの長さに基づいてElasticSearchドキュメントをフィルタリングする方法はありますか?ElasticSearch:フィールドの長さに基づいてドキュメントをフィルタリングしますか?

たとえば、フィールド "body"を持つドキュメントがたくさんあり、bodyの文字数が1000を超える場合のみ結果を返すことができます。ESでこれを行う方法はありますかインデックスに長さを持つ余分な列を追加しますか?

答えて

7

このように、スクリプトフィルタを使用してください:あなたはまた、カスタムトークナイザを作成し、同様にmultifieldsプロパティにそれを使用することができますthe query DSL guide on script filters

+1

[Elasticsearch 2.1のドキュメント](https://www.elastic.co/ guide/en/elasticsearch/reference/current/modules-scripting.html)は '.length'フィールドについては言及していませんが、これはまだ動作しますか? – robinst

+0

おそらく、明示的にスクリプトサポートを有効にすると、これはまだ機能します(v1.4ではデフォルトでスクリプトが無効になっていたと思います)。 MVELではなくGroovyスクリプトが使用されていますので、これをチェックしてみてください。 – Phil

+0

http://stackoverflow.com/questions/23023233/elasticsearch-statistical-facet-on-length-of-string-fieldには、「script」:「doc ['body']。value.length()」を使用できます。 ""私のために働いた1.7.5 – nezda

0

を参照するためのもの 申し訳

"filtered" : { 
    "query" : { 
     ... 
    }, 
    "filter" : { 
     "script" : { 
      "script" : "doc['body'].length > 1000" 
     } 
    } 
} 

EDIT、以下:

PUT test_index 
{ 
    "settings": { 
    "analysis": { 
     "analyzer": { 
     "character_analyzer": { 
      "type": "custom", 
      "tokenizer": "character_tokenizer" 
     } 
     }, 
     "tokenizer": { 
     "character_tokenizer": { 
      "type": "nGram", 
      "min_gram": 1, 
      "max_gram": 1 
     } 
     } 
    } 
    }, 
    "mappings": { 
    "person": { 
     "properties": { 
     "name": { 
      "type": "text", 
      "fields": { 
      "keyword": { 
       "type": "keyword" 
      }, 
      "words_count": { 
       "type": "token_count", 
       "analyzer": "standard" 
      }, 
      "length": { 
       "type": "token_count", 
       "analyzer": "character_analyzer" 
      } 
      } 
     } 
     } 
    } 
    } 
} 

PUT test_index/person/1 
{ 
    "name": "John Smith" 
} 

PUT test_index/person/2 
{ 
    "name": "Rachel Alice Williams" 
} 

GET test_index/person/_search 
{ 
    "query": { 
    "term": { 
     "name.length": 10 
    } 
    } 
} 
関連する問題