2017-01-28 20 views
1

私はソースフィールドを持つインデックスmp_v1を持っています:idとtags。 "タグ"フィールドには、文字列内のドキュメント内のすべてのタグが含まれます。すべてのタグからすべての記事を取得しますelasticsearch

例:

{ 
     "_index": "mp_v1", 
     "_type": "mp", 
     "_id": "5", 
     "_score": 1, 
     "_source": { 
      "id": 5, 
      "tags": "tag1 black blue" 
     } 
} 

私はすべての文書でoccurancesと弾性検索タグから取得できますか?たとえば、2つのドキュメントがある場合、最初のタグが「tag1 black blue」で、2番目のタグが「blue square」のタグで返されると、返されるはずです:blue:2、tag1:1、black:1、square:1

+0

'tags'は単一の文字列ですか?それがタイプ[配列](https://www.elastic.co/guide/en/elasticsearch/reference/current/array.html)だったら、それはケーキの一片でしょう。配列にすることができない場合は、[regex query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html)を使用して、 – dimzak

答えて

2

I ES 5.12

PUT testindex_51 
{ 
    "settings": { 
     "analysis": { 
      "analyzer": { 
      }, 
      "filter":{ 
     } 
     } 
    }, 
    "mappings": { 
     "table1": { 
      "properties": { 
       "title": { 
        "type": "text", 
        "analyzer": "whitespace", 
        "fielddata": true 
       } 
      } 
     } 
    } 
} 

POST testindex_50/table1 
{ 
    "title" : "tag1 aggs1 blue" 
} 

POST testindex_50/table1 
{ 
    "title" : "tag2 aggs2 blue" 
} 

POST testindex_50/table1/_search 
{ 
    "aggs": { 
    "tags_count": { 
     "terms": { 
     "field": "title", 
     "size": 10 
     } 
    } 
    } 
} 

回答あなたは単にfielddata有効(汚いやり方)と同じを取得するには、単純な用語の集約を使用することができます

{ 
    "took": 11, 
    "timed_out": false, 
    "_shards": { 
    "total": 5, 
    "successful": 5, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 2, 
    "max_score": 0, 
    "hits": [] 
    }, 
    "aggregations": { 
    "tags_count": { 
     "doc_count_error_upper_bound": 0, 
     "sum_other_doc_count": 0, 
     "buckets": [ 
     { 
      "key": "blue", 
      "doc_count": 2 
     }, 
     { 
      "key": "aggs1", 
      "doc_count": 1 
     }, 
     { 
      "key": "aggs2", 
      "doc_count": 1 
     }, 
     { 
      "key": "tag1", 
      "doc_count": 1 
     }, 
     { 
      "key": "tag2", 
      "doc_count": 1 
     } 
     ] 
    } 
    } 
} 
0

を実行しています。

しかし、フィールドを分割して集計を行うことをお勧めします。

関連する問題