Skip to content

Commit

Permalink
Fix logic for indices without time field, fix translations
Browse files Browse the repository at this point in the history
  • Loading branch information
qn895 committed Aug 23, 2022
1 parent 757c4a5 commit 3a2fff6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { each, get, sortedIndex } from 'lodash';
import { each, get, sortedIndex, first } from 'lodash';
import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { isPopulatedObject } from '@kbn/ml-is-populated-object';
import { DataPublicPluginStart, ISearchOptions } from '@kbn/data-plugin/public';
Expand Down Expand Up @@ -67,7 +67,7 @@ export const getDocumentCountStats = async (
// so that we can calculate the next p value that's appropriate for the data set
const initialDefaultProbability = probability ?? 1e-5;

const getAggsWithRandomSampling = (p: number) => ({
const getEventRateAggsWithRandomSampling = (p: number) => ({
sampler: {
aggs,
random_sampler: {
Expand All @@ -83,7 +83,7 @@ export const getDocumentCountStats = async (
index,
body: {
query,
...(hasTimeField ? { aggs: aggregations } : {}),
...(aggregations !== undefined ? { aggs: aggregations } : {}),
...(isPopulatedObject(runtimeFieldMap) ? { runtime_mappings: runtimeFieldMap } : {}),
},
track_total_hits: !hasTimeField,
Expand All @@ -93,7 +93,27 @@ export const getDocumentCountStats = async (
const firstResp = await search
.search(
{
params: getSearchParams(getAggsWithRandomSampling(initialDefaultProbability)),
params: getSearchParams(
hasTimeField
? getEventRateAggsWithRandomSampling(initialDefaultProbability)
: // If there's no time field, we just make a value_count aggregation on any field
// just so we can calculate what's the optimal probability from the sampled doc count
{
sampler: {
random_sampler: {
probability: initialDefaultProbability,
},
aggs: {
price_percentiles: {
value_count: {
field:
first(params.aggregatableFields) ?? first(params.nonAggregatableFields),
},
},
},
},
}
),
},
searchOptions
)
Expand All @@ -102,23 +122,11 @@ export const getDocumentCountStats = async (
if (firstResp === undefined) {
throw Error(
`An error occurred with the following query ${JSON.stringify(
getSearchParams(getAggsWithRandomSampling(initialDefaultProbability))
getSearchParams(getEventRateAggsWithRandomSampling(initialDefaultProbability))
)}`
);
}

if (!hasTimeField && isDefined(firstResp.rawResponse)) {
return {
...result,
took: firstResp.rawResponse.took,
totalCount:
(typeof firstResp.rawResponse.hits?.total === 'number'
? firstResp.rawResponse.hits?.total
: firstResp.rawResponse.hits?.total?.value) ?? 0,
probability: 1,
};
}

if (isDefined(probability)) {
return {
...result,
Expand All @@ -136,13 +144,25 @@ export const getDocumentCountStats = async (
const newProbability =
(initialDefaultProbability * numDocs) / (numSampled - 2 * Math.sqrt(numSampled));

if (!hasTimeField) {
return {
...result,
took: firstResp.rawResponse.took,
totalCount:
(typeof firstResp.rawResponse.hits?.total === 'number'
? firstResp.rawResponse.hits?.total
: firstResp.rawResponse.hits?.total?.value) ?? 0,
probability: numSampled < 109 ? 1 : newProbability,
};
}

// If the number of docs sampled is indicative of query with < 10 million docs
// proceed to make a vanilla aggregation without any sampling
if (numSampled === 0 || newProbability === Infinity || numSampled < 109) {
const vanillaAggResp = await search
.search(
{
params: getSearchParams(getAggsWithRandomSampling(1)),
params: getSearchParams(getEventRateAggsWithRandomSampling(1)),
},
searchOptions
)
Expand All @@ -162,7 +182,7 @@ export const getDocumentCountStats = async (
const secondResp = await search
.search(
{
params: getSearchParams(getAggsWithRandomSampling(closestProbability)),
params: getSearchParams(getEventRateAggsWithRandomSampling(closestProbability)),
},
searchOptions
)
Expand Down
6 changes: 0 additions & 6 deletions x-pack/plugins/translations/translations/fr-FR.json
Original file line number Diff line number Diff line change
Expand Up @@ -9745,8 +9745,6 @@
"xpack.dataVisualizer.dataGrid.field.metricDistributionChart.tooltipValueBetweenLabel": "{percent} % des documents ont des valeurs comprises entre {minValFormatted} et {maxValFormatted}",
"xpack.dataVisualizer.dataGrid.field.metricDistributionChart.tooltipValueEqualLabel": "{percent} % des documents ont une valeur de {valFormatted}",
"xpack.dataVisualizer.dataGrid.field.removeFilterAriaLabel": "Exclure le {fieldName} : \"{value}\"",
"xpack.dataVisualizer.dataGrid.field.topValues.calculatedFromSampleDescription": "Calculé à partir d'un échantillon de {topValuesSamplerShardSize} documents par partition",
"xpack.dataVisualizer.dataGrid.fieldExpandedRow.choroplethMapTopValues.calculatedFromSampleDescription": "Calculé à partir d'un échantillon de {topValuesSamplerShardSize} documents par partition",
"xpack.dataVisualizer.dataGrid.fieldExpandedRow.numberContent.displayingPercentilesLabel": "Affichage de {minPercent} - {maxPercent} centiles",
"xpack.dataVisualizer.dataGrid.fieldText.fieldMayBePopulatedDescription": "Il peut être rempli, par exemple, à l'aide d'un paramètre {copyToParam} dans le mapping du document ou être réduit à partir du champ {sourceParam} après une indexation par l'utilisation des paramètres {includesParam} et {excludesParam}.",
"xpack.dataVisualizer.dataGrid.fieldText.fieldNotPresentDescription": "Ce champ n'était pas présent dans le champ {sourceParam} des documents interrogés.",
Expand Down Expand Up @@ -9784,7 +9782,6 @@
"xpack.dataVisualizer.nameCollisionMsg": "\"{name}\" existe déjà, veuillez fournir un nom unique",
"xpack.dataVisualizer.randomSamplerSettingsPopUp.probabilityLabel": "Probabilité utilisée : {samplingProbability} %",
"xpack.dataVisualizer.searchPanel.ofFieldsTotal": "sur un total de {totalCount}",
"xpack.dataVisualizer.searchPanel.sampleSizeOptionLabel": "Taille de l'échantillon (par partition) : {wrappedValue}",
"xpack.dataVisualizer.searchPanel.totalDocCountLabel": "Total des documents : {prepend}{strongTotalCount}",
"xpack.dataVisualizer.searchPanel.totalDocCountNumber": "{totalCount, plural, other {#}}",
"xpack.dataVisualizer.addCombinedFieldsLabel": "Ajouter un champ combiné",
Expand Down Expand Up @@ -10063,13 +10060,10 @@
"xpack.dataVisualizer.removeCombinedFieldsLabel": "Retirer le champ combiné",
"xpack.dataVisualizer.samplingOptionsButton": "Options d’échantillonnage",
"xpack.dataVisualizer.searchPanel.allFieldsLabel": "Tous les champs",
"xpack.dataVisualizer.searchPanel.allOptionLabel": "Tout rechercher",
"xpack.dataVisualizer.searchPanel.invalidSyntax": "Syntaxe non valide",
"xpack.dataVisualizer.searchPanel.numberFieldsLabel": "Champs de numéros",
"xpack.dataVisualizer.searchPanel.queryBarPlaceholder": "La sélection d'une taille d'échantillon plus petite réduira les temps d'exécution de la requête et la charge sur le cluster.",
"xpack.dataVisualizer.searchPanel.queryBarPlaceholderText": "Rechercher… (par exemple, status:200 AND extension:\"PHP\")",
"xpack.dataVisualizer.searchPanel.randomSamplerMessage": "Des valeurs approximatives sont indiquées dans le décompte de documents et le graphique, qui utilisent des agrégations par échantillonnage aléatoire.",
"xpack.dataVisualizer.searchPanel.sampleSizeAriaLabel": "Sélectionner le nombre de documents à échantillonner",
"xpack.dataVisualizer.searchPanel.showEmptyFields": "Afficher les champs vides",
"xpack.dataVisualizer.title": "Charger un fichier",
"xpack.embeddableEnhanced.actions.panelNotifications.manyDrilldowns": "Le panneau comporte {count} recherches",
Expand Down
6 changes: 0 additions & 6 deletions x-pack/plugins/translations/translations/ja-JP.json
Original file line number Diff line number Diff line change
Expand Up @@ -9734,8 +9734,6 @@
"xpack.dataVisualizer.dataGrid.field.metricDistributionChart.tooltipValueBetweenLabel": "{percent}% のドキュメントに {minValFormatted} から {maxValFormatted} の間の値があります",
"xpack.dataVisualizer.dataGrid.field.metricDistributionChart.tooltipValueEqualLabel": "{percent}% のドキュメントに {valFormatted} の値があります",
"xpack.dataVisualizer.dataGrid.field.removeFilterAriaLabel": "{fieldName}の除外:\"{value}\"",
"xpack.dataVisualizer.dataGrid.field.topValues.calculatedFromSampleDescription": "1 つのシャードにつき {topValuesSamplerShardSize} のドキュメントのサンプルで計算されています",
"xpack.dataVisualizer.dataGrid.fieldExpandedRow.choroplethMapTopValues.calculatedFromSampleDescription": "1 つのシャードにつき {topValuesSamplerShardSize} のドキュメントのサンプルで計算されています",
"xpack.dataVisualizer.dataGrid.fieldExpandedRow.numberContent.displayingPercentilesLabel": "{minPercent} - {maxPercent} パーセンタイルを表示中",
"xpack.dataVisualizer.dataGrid.fieldText.fieldMayBePopulatedDescription": "たとえば、ドキュメントマッピングで {copyToParam} パラメーターを使ったり、{includesParam} と {excludesParam} パラメーターを使用してインデックスした後に {sourceParam} フィールドから切り取ったりして入力される場合があります。",
"xpack.dataVisualizer.dataGrid.fieldText.fieldNotPresentDescription": "このフィールドはクエリが実行されたドキュメントの {sourceParam} フィールドにありませんでした。",
Expand Down Expand Up @@ -9772,7 +9770,6 @@
"xpack.dataVisualizer.nameCollisionMsg": "「{name}」はすでに存在します。一意の名前を入力してください。",
"xpack.dataVisualizer.randomSamplerSettingsPopUp.probabilityLabel": "使用された確率:{samplingProbability}%",
"xpack.dataVisualizer.searchPanel.ofFieldsTotal": "合計 {totalCount}",
"xpack.dataVisualizer.searchPanel.sampleSizeOptionLabel": "サンプルサイズ(シャード単位):{wrappedValue}",
"xpack.dataVisualizer.searchPanel.totalDocCountLabel": "合計ドキュメント数:{prepend}{strongTotalCount}",
"xpack.dataVisualizer.searchPanel.totalDocCountNumber": "{totalCount, plural, other {#}}",
"xpack.dataVisualizer.addCombinedFieldsLabel": "結合されたフィールドを追加",
Expand Down Expand Up @@ -10051,13 +10048,10 @@
"xpack.dataVisualizer.removeCombinedFieldsLabel": "結合されたフィールドを削除",
"xpack.dataVisualizer.samplingOptionsButton": "抽出オプション",
"xpack.dataVisualizer.searchPanel.allFieldsLabel": "すべてのフィールド",
"xpack.dataVisualizer.searchPanel.allOptionLabel": "すべて検索",
"xpack.dataVisualizer.searchPanel.invalidSyntax": "無効な構文",
"xpack.dataVisualizer.searchPanel.numberFieldsLabel": "数値フィールド",
"xpack.dataVisualizer.searchPanel.queryBarPlaceholder": "小さいサンプルサイズを選択することで、クエリの実行時間を短縮しクラスターへの負荷を軽減できます。",
"xpack.dataVisualizer.searchPanel.queryBarPlaceholderText": "検索…(例:status:200 AND extension:\"PHP\")",
"xpack.dataVisualizer.searchPanel.randomSamplerMessage": "近似値は、ランダムサンプラーアグリゲーションを使用する、合計ドキュメント数およびグラフに表示されます。",
"xpack.dataVisualizer.searchPanel.sampleSizeAriaLabel": "サンプリングするドキュメント数を選択してください",
"xpack.dataVisualizer.searchPanel.showEmptyFields": "空のフィールドを表示",
"xpack.dataVisualizer.title": "ファイルをアップロード",
"xpack.embeddableEnhanced.actions.panelNotifications.manyDrilldowns": "パネルには{count}個のドリルダウンがあります",
Expand Down
6 changes: 0 additions & 6 deletions x-pack/plugins/translations/translations/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -9749,8 +9749,6 @@
"xpack.dataVisualizer.dataGrid.field.metricDistributionChart.tooltipValueBetweenLabel": "{percent}% 的文档具有介于 {minValFormatted} 和 {maxValFormatted} 之间的值",
"xpack.dataVisualizer.dataGrid.field.metricDistributionChart.tooltipValueEqualLabel": "{percent}% 的文档的值为 {valFormatted}",
"xpack.dataVisualizer.dataGrid.field.removeFilterAriaLabel": "筛除 {fieldName}:“{value}”",
"xpack.dataVisualizer.dataGrid.field.topValues.calculatedFromSampleDescription": "基于每个分片的 {topValuesSamplerShardSize} 文档样例计算",
"xpack.dataVisualizer.dataGrid.fieldExpandedRow.choroplethMapTopValues.calculatedFromSampleDescription": "基于每个分片的 {topValuesSamplerShardSize} 文档样例计算",
"xpack.dataVisualizer.dataGrid.fieldExpandedRow.numberContent.displayingPercentilesLabel": "正在显示 {minPercent} - {maxPercent} 百分位数",
"xpack.dataVisualizer.dataGrid.fieldText.fieldMayBePopulatedDescription": "例如,可以使用文档映射中的 {copyToParam} 参数进行填充,也可以在索引后通过使用 {includesParam} 和 {excludesParam} 参数从 {sourceParam} 字段中修剪。",
"xpack.dataVisualizer.dataGrid.fieldText.fieldNotPresentDescription": "查询的文档的 {sourceParam} 字段中不存在此字段。",
Expand Down Expand Up @@ -9788,7 +9786,6 @@
"xpack.dataVisualizer.nameCollisionMsg": "“{name}”已存在,请提供唯一名称",
"xpack.dataVisualizer.randomSamplerSettingsPopUp.probabilityLabel": "使用的概率:{samplingProbability}%",
"xpack.dataVisualizer.searchPanel.ofFieldsTotal": ",共 {totalCount} 个",
"xpack.dataVisualizer.searchPanel.sampleSizeOptionLabel": "样本大小(每分片):{wrappedValue}",
"xpack.dataVisualizer.searchPanel.totalDocCountLabel": "文档总数:{prepend}{strongTotalCount}",
"xpack.dataVisualizer.searchPanel.totalDocCountNumber": "{totalCount, plural, other {#}}",
"xpack.dataVisualizer.addCombinedFieldsLabel": "添加组合字段",
Expand Down Expand Up @@ -10067,13 +10064,10 @@
"xpack.dataVisualizer.removeCombinedFieldsLabel": "移除组合字段",
"xpack.dataVisualizer.samplingOptionsButton": "采样选项",
"xpack.dataVisualizer.searchPanel.allFieldsLabel": "所有字段",
"xpack.dataVisualizer.searchPanel.allOptionLabel": "搜索全部",
"xpack.dataVisualizer.searchPanel.invalidSyntax": "语法无效",
"xpack.dataVisualizer.searchPanel.numberFieldsLabel": "字段数目",
"xpack.dataVisualizer.searchPanel.queryBarPlaceholder": "选择较小的样例大小将减少查询运行时间和集群上的负载。",
"xpack.dataVisualizer.searchPanel.queryBarPlaceholderText": "搜索……(例如,status:200 AND extension:\"PHP\")",
"xpack.dataVisualizer.searchPanel.randomSamplerMessage": "总文档计数和图表中将显示近似值,它们使用随机采样器聚合。",
"xpack.dataVisualizer.searchPanel.sampleSizeAriaLabel": "选择要采样的文档数目",
"xpack.dataVisualizer.searchPanel.showEmptyFields": "显示空字段",
"xpack.dataVisualizer.title": "上传文件",
"xpack.embeddableEnhanced.actions.panelNotifications.manyDrilldowns": "面板有 {count} 个向下钻取",
Expand Down

0 comments on commit 3a2fff6

Please sign in to comment.