summaryrefslogtreecommitdiff
path: root/docs/java-api/facets.asciidoc
blob: 34353c6018e5450bdf6b8bb1e9931326a0b934aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
[[java-facets]]
== Facets

Elasticsearch provides a full Java API to play with facets. See the
{ref}/search-facets.html[Facets guide].

Use the factory for facet builders (`FacetBuilders`) and add each facet
you want to compute when querying and add it to your search request:

[source,java]
--------------------------------------------------
SearchResponse sr = node.client().prepareSearch()
        .setQuery( /* your query */ )
        .addFacet( /* add a facet */ )
        .execute().actionGet();
--------------------------------------------------

Note that you can add more than one facet. See
{ref}/search-search.html[Search Java API] for details.

To build facet requests, use `FacetBuilders` helpers. Just import them
in your class:

[source,java]
--------------------------------------------------
import org.elasticsearch.search.facet.FacetBuilders.*;
--------------------------------------------------


=== Facets


[[java-facet-terms]]
==== Terms Facet

Here is how you can use
{ref}/search-facets-terms-facet.html[Terms Facet]
with Java API.


===== Prepare facet request

Here is an example on how to create the facet request:

[source,java]
--------------------------------------------------
FacetBuilders.termsFacet("f")
    .field("brand")
    .size(10);
--------------------------------------------------


===== Use facet response

Import Facet definition classes:

[source,java]
--------------------------------------------------
import org.elasticsearch.search.facet.terms.*;
--------------------------------------------------

[source,java]
--------------------------------------------------
// sr is here your SearchResponse object
TermsFacet f = (TermsFacet) sr.getFacets().facetsAsMap().get("f");

f.getTotalCount();      // Total terms doc count
f.getOtherCount();      // Not shown terms doc count
f.getMissingCount();    // Without term doc count

// For each entry
for (TermsFacet.Entry entry : f) {
    entry.getTerm();    // Term
    entry.getCount();   // Doc count
}
--------------------------------------------------


[[java-facet-range]]
==== Range Facet

Here is how you can use
{ref}/search-facets-range-facet.html[Range Facet]
with Java API.


===== Prepare facet request

Here is an example on how to create the facet request:

[source,java]
--------------------------------------------------
FacetBuilders.rangeFacet("f")
    .field("price")         // Field to compute on
    .addUnboundedFrom(3)    // from -infinity to 3 (excluded)
    .addRange(3, 6)         // from 3 to 6 (excluded)
    .addUnboundedTo(6);     // from 6 to +infinity
--------------------------------------------------


===== Use facet response

Import Facet definition classes:

[source,java]
--------------------------------------------------
import org.elasticsearch.search.facet.range.*;
--------------------------------------------------

[source,java]
--------------------------------------------------
// sr is here your SearchResponse object
RangeFacet f = (RangeFacet) sr.getFacets().facetsAsMap().get("f");

// For each entry
for (RangeFacet.Entry entry : f) {
    entry.getFrom();    // Range from requested
    entry.getTo();      // Range to requested
    entry.getCount();   // Doc count
    entry.getMin();     // Min value
    entry.getMax();     // Max value
    entry.getMean();    // Mean
    entry.getTotal();   // Sum of values
}
--------------------------------------------------


[[histogram]]
==== Histogram Facet

Here is how you can use
{ref}/search-facets-histogram-facet.html[Histogram
Facet] with Java API.


===== Prepare facet request

Here is an example on how to create the facet request:

[source,java]
--------------------------------------------------
HistogramFacetBuilder facet = FacetBuilders.histogramFacet("f")
    .field("price")
    .interval(1);
--------------------------------------------------


===== Use facet response

Import Facet definition classes:

[source,java]
--------------------------------------------------
import org.elasticsearch.search.facet.histogram.*;
--------------------------------------------------

[source,java]
--------------------------------------------------
// sr is here your SearchResponse object
HistogramFacet f = (HistogramFacet) sr.getFacets().facetsAsMap().get("f");

// For each entry
for (HistogramFacet.Entry entry : f) {
    entry.getKey();     // Key (X-Axis)
    entry.getCount();   // Doc count (Y-Axis)
}
--------------------------------------------------


[[date-histogram]]
==== Date Histogram Facet

Here is how you can use
{ref}/search-facets-date-histogram-facet.html[Date
Histogram Facet] with Java API.


===== Prepare facet request

Here is an example on how to create the facet request:

[source,java]
--------------------------------------------------
FacetBuilders.dateHistogramFacet("f")
    .field("date")      // Your date field
    .interval("year");  // You can also use "quarter", "month", "week", "day",
                        // "hour" and "minute" or notation like "1.5h" or "2w"
--------------------------------------------------


===== Use facet response

Import Facet definition classes:

[source,java]
--------------------------------------------------
import org.elasticsearch.search.facet.datehistogram.*;
--------------------------------------------------

[source,java]
--------------------------------------------------
// sr is here your SearchResponse object
DateHistogramFacet f = (DateHistogramFacet) sr.getFacets().facetsAsMap().get("f");

// For each entry
for (DateHistogramFacet.Entry entry : f) {
    entry.getTime();    // Date in ms since epoch (X-Axis)
    entry.getCount();   // Doc count (Y-Axis)
}
--------------------------------------------------


[[filter]]
==== Filter Facet (not facet filter)

Here is how you can use
{ref}/search-facets-filter-facet.html[Filter Facet]
with Java API.

If you are looking on how to apply a filter to a facet, have a look at
link:#facet-filter[facet filter] using Java API.


===== Prepare facet request

Here is an example on how to create the facet request:

[source,java]
--------------------------------------------------
FacetBuilders.filterFacet("f",
    FilterBuilders.termFilter("brand", "heineken"));    // Your Filter here
--------------------------------------------------

See <<query-dsl-filters,Filters>> to
learn how to build filters using Java.


===== Use facet response

Import Facet definition classes:

[source,java]
--------------------------------------------------
import org.elasticsearch.search.facet.filter.*;
--------------------------------------------------

[source,java]
--------------------------------------------------
// sr is here your SearchResponse object
FilterFacet f = (FilterFacet) sr.getFacets().facetsAsMap().get("f");

f.getCount();   // Number of docs that matched
--------------------------------------------------


[[query]]
==== Query Facet

Here is how you can use
{ref}/search-facets-query-facet.html[Query Facet]
with Java API.


===== Prepare facet request

Here is an example on how to create the facet request:

[source,java]
--------------------------------------------------
FacetBuilders.queryFacet("f",
    QueryBuilders.matchQuery("brand", "heineken"));
--------------------------------------------------


===== Use facet response

Import Facet definition classes:

[source,java]
--------------------------------------------------
import org.elasticsearch.search.facet.query.*;
--------------------------------------------------

[source,java]
--------------------------------------------------
// sr is here your SearchResponse object
QueryFacet f = (QueryFacet) sr.getFacets().facetsAsMap().get("f");

f.getCount();   // Number of docs that matched
--------------------------------------------------

See <<query-dsl-queries,Queries>> to
learn how to build queries using Java.


[[statistical]]
==== Statistical

Here is how you can use
{ref}/search-facets-statistical-facet.html[Statistical
Facet] with Java API.


===== Prepare facet request

Here is an example on how to create the facet request:

[source,java]
--------------------------------------------------
FacetBuilders.statisticalFacet("f")
   .field("price");
--------------------------------------------------


===== Use facet response

Import Facet definition classes:

[source,java]
--------------------------------------------------
import org.elasticsearch.search.facet.statistical.*;
--------------------------------------------------

[source,java]
--------------------------------------------------
// sr is here your SearchResponse object
StatisticalFacet f = (StatisticalFacet) sr.getFacets().facetsAsMap().get("f");

f.getCount();           // Doc count
f.getMin();             // Min value
f.getMax();             // Max value
f.getMean();            // Mean
f.getTotal();           // Sum of values
f.getStdDeviation();    // Standard Deviation
f.getSumOfSquares();    // Sum of Squares
f.getVariance();        // Variance
--------------------------------------------------


[[terms-stats]]
==== Terms Stats Facet

Here is how you can use
{ref}/search-facets-terms-stats-facet.html[Terms
Stats Facet] with Java API.


===== Prepare facet request

Here is an example on how to create the facet request:

[source,java]
--------------------------------------------------
FacetBuilders.termsStatsFacet("f")
    .keyField("brand")
    .valueField("price");
--------------------------------------------------


===== Use facet response

Import Facet definition classes:

[source,java]
--------------------------------------------------
import org.elasticsearch.search.facet.termsstats.*;
--------------------------------------------------

[source,java]
--------------------------------------------------
// sr is here your SearchResponse object
TermsStatsFacet f = (TermsStatsFacet) sr.getFacets().facetsAsMap().get("f");
f.getTotalCount();      // Total terms doc count
f.getOtherCount();      // Not shown terms doc count
f.getMissingCount();    // Without term doc count

// For each entry
for (TermsStatsFacet.Entry entry : f) {
    entry.getTerm();            // Term
    entry.getCount();           // Doc count
    entry.getMin();             // Min value
    entry.getMax();             // Max value
    entry.getMean();            // Mean
    entry.getTotal();           // Sum of values
}
--------------------------------------------------


[[geo-distance]]
==== Geo Distance Facet

Here is how you can use
{ref}/search-facets-geo-distance-facet.html[Geo
Distance Facet] with Java API.


===== Prepare facet request

Here is an example on how to create the facet request:

[source,java]
--------------------------------------------------
FacetBuilders.geoDistanceFacet("f")
    .field("pin.location")              // Field containing coordinates we want to compare with
    .point(40, -70)                     // Point from where we start (0)
    .addUnboundedFrom(10)               // 0 to 10 km (excluded)
    .addRange(10, 20)                   // 10 to 20 km (excluded)
    .addRange(20, 100)                  // 20 to 100 km (excluded)
    .addUnboundedTo(100)                // from 100 km to infinity (and beyond ;-) )
    .unit(DistanceUnit.KILOMETERS);     // All distances are in kilometers. Can be MILES
--------------------------------------------------


===== Use facet response

Import Facet definition classes:

[source,java]
--------------------------------------------------
import org.elasticsearch.search.facet.geodistance.*;
--------------------------------------------------

[source,java]
--------------------------------------------------
// sr is here your SearchResponse object
GeoDistanceFacet f = (GeoDistanceFacet) sr.getFacets().facetsAsMap().get("f");

// For each entry
for (GeoDistanceFacet.Entry entry : f) {
    entry.getFrom();            // Distance from requested
    entry.getTo();              // Distance to requested
    entry.getCount();           // Doc count
    entry.getMin();             // Min value
    entry.getMax();             // Max value
    entry.getTotal();           // Sum of values
    entry.getMean();            // Mean
}
--------------------------------------------------


[[facet-filter]]
=== Facet filters (not Filter Facet)

By default, facets are applied on the query resultset whatever filters
exists or are.

If you need to compute facets with the same filters or even with other
filters, you can add the filter to any facet using
`AbstractFacetBuilder#facetFilter(FilterBuilder)` method:

[source,java]
--------------------------------------------------
FacetBuilders
    .termsFacet("f").field("brand") // Your facet
    .facetFilter( // Your filter here
        FilterBuilders.termFilter("colour", "pale")
    );
--------------------------------------------------

For example, you can reuse the same filter you created for your query:

[source,java]
--------------------------------------------------
// A common filter
FilterBuilder filter = FilterBuilders.termFilter("colour", "pale");

TermsFacetBuilder facet = FacetBuilders.termsFacet("f")
    .field("brand")
    .facetFilter(filter);                           // We apply it to the facet

SearchResponse sr = node.client().prepareSearch()
    .setQuery(QueryBuilders.matchAllQuery())
    .setFilter(filter)                              // We apply it to the query
    .addFacet(facet)
    .execute().actionGet();
--------------------------------------------------

See documentation on how to build
<<query-dsl-filters,Filters>>.


[[scope]]
=== Scope

By default, facets are computed within the query resultset. But, you can
compute facets from all documents in the index whatever the query is,
using `global` parameter:

[source,java]
--------------------------------------------------
TermsFacetBuilder facet = FacetBuilders.termsFacet("f")
    .field("brand")
    .global(true);
--------------------------------------------------