0

我想计算分析的每个令牌。

首先,我尝试了以下代码:

映射

{
  "docs": {
    "mappings": {
      "doc": {
        "dynamic": "false",
        "properties": {
          "text": {
            "type": "string",
            "analyzer": "kuromoji"
          }
        }
      }
    }
  }
}

查询

{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "word-count": {
      "terms": {
        "field": "text",
        "size": "1000"
      }
    }
  },
  "size": 0
}

插入数据后查询索引,得到以下结果:

{
  "took": 41
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 10000,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "word-count": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 36634,
      "buckets": [
        {
          "key": "はい",
          "doc_count": 4734
        },
        {
          "key": "いただく",
          "doc_count": 2440
        },
        ...
      ]
    }
  }
}

不幸的是,术语聚合只提供一个 doc_count。这不是字数。_index['text']['TERM'].df()所以,我认为使用and获得近似字数的方法_index['text']['TERM'].ttf()

也许近似的字数是以下等式:

WordCount = doc_count['TERM'] / _index['text']['TERM'].df() * _index['text']['TERM'].ttf()

“期限”是存储桶中的关键。我试图编写一个脚本化的度量聚合,但我不知道如何获取存储桶中的键。

{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "doc-count": {
      "terms": {
        "field": "text",
        "size": "1000"
      }
    },
    "aggs": {
      "word-count": {
        "scripted_metric": {
           // ???
        }
      }
    }
  },
  "size": 0
}

如何获取存储桶中的密钥?如果不可能,我怎样才能得到分析的字数?

4

2 回答 2

0

你可以试试dynamic_scripting,虽然这会影响性能..

{
"query": {
"match_all": {}
},
"aggs": {
"word-count": {
  "terms": {
    "script": "_source.text",
    "size": "1000"
    }
  }
 },
"size": 0
}
于 2016-03-16T10:21:38.793 回答
0

您可以尝试使用令牌计数数据类型。只需将该类型的子字段添加到您的text字段中:

{
  "docs": {
    "mappings": {
      "doc": {
        "dynamic": "false",
        "properties": {
          "text": {
            "type": "string",
            "analyzer": "kuromoji"
          }, 
          "fields": {
            "nb_tokens": {
              "type": "token_count",
              "analyzer": "kuromoji"
            }
          }
        }
      }
    }
  }
}

然后你可以text.nb_tokens在你的聚合中使用。

于 2016-03-16T03:47:22.987 回答