explore.asciidoc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. [role="xpack"]
  2. [testenv="platinum"]
  3. [[graph-explore-api]]
  4. == Graph explore API
  5. The graph explore API enables you to extract and summarize information about
  6. the documents and terms in an {es} data stream or index.
  7. The easiest way to understand the behaviour of this API is to use the
  8. Graph UI to explore connections. You can view the most recent request submitted
  9. to the `_explore` endpoint from the *Last request* panel. For more information,
  10. see {kibana-ref}/graph-getting-started.html[Getting Started with Graph].
  11. For additional information about working with the explore API, see the Graph
  12. {kibana-ref}/graph-troubleshooting.html[Troubleshooting] and
  13. {kibana-ref}/graph-limitations.html[Limitations] topics.
  14. NOTE: The graph explore API is enabled by default. To disable access to the
  15. graph explore API and the Kibana {kibana-ref}/graph-getting-started.html[Graph
  16. UI], add `xpack.graph.enabled: false` to `elasticsearch.yml`.
  17. [discrete]
  18. === Request
  19. `POST <target>/_graph/explore`
  20. [discrete]
  21. === Description
  22. An initial request to the `_explore` API contains a seed query that identifies
  23. the documents of interest and specifies the fields that define the vertices
  24. and connections you want to include in the graph. Subsequent `_explore` requests
  25. enable you to _spider out_ from one more vertices of interest. You can exclude
  26. vertices that have already been returned.
  27. [discrete]
  28. === Request Body
  29. [role="child_attributes"]
  30. ====
  31. query::
  32. A seed query that identifies the documents of interest. Can be any valid
  33. Elasticsearch query. For example:
  34. +
  35. [source,js]
  36. --------------------------------------------------
  37. "query": {
  38. "bool": {
  39. "must": {
  40. "match": {
  41. "query.raw": "midi"
  42. }
  43. },
  44. "filter": [
  45. {
  46. "range": {
  47. "query_time": {
  48. "gte": "2015-10-01 00:00:00"
  49. }
  50. }
  51. }
  52. ]
  53. }
  54. }
  55. --------------------------------------------------
  56. vertices::
  57. Specifies or more fields that contain the terms you want to include in the
  58. graph as vertices. For example:
  59. +
  60. [source,js]
  61. --------------------------------------------------
  62. "vertices": [
  63. {
  64. "field": "product"
  65. }
  66. ]
  67. --------------------------------------------------
  68. +
  69. .Properties for `vertices`
  70. [%collapsible%open]
  71. ======
  72. field::: Identifies a field in the documents of interest.
  73. include::: Identifies the terms of interest that form the starting points
  74. from which you want to spider out. You do not have to specify a seed query
  75. if you specify an include clause. The include clause implicitly querys for
  76. documents that contain any of the listed terms listed.
  77. In addition to specifying a simple array of strings, you can also pass
  78. objects with `term` and `boost` values to boost matches on particular terms.
  79. exclude:::
  80. The `exclude` clause prevents the specified terms from being included in
  81. the results.
  82. size:::
  83. Specifies the maximum number of vertex terms returned for each
  84. field. Defaults to 5.
  85. min_doc_count:::
  86. Specifies how many documents must contain a pair of terms before it is
  87. considered to be a useful connection. This setting acts as a certainty
  88. threshold. Defaults to 3.
  89. shard_min_doc_count:::
  90. This advanced setting controls how many documents on a particular shard have
  91. to contain a pair of terms before the connection is returned for global
  92. consideration. Defaults to 2.
  93. ======
  94. connections::
  95. Specifies or more fields from which you want to extract terms that are
  96. associated with the specified vertices. For example:
  97. +
  98. [source,js]
  99. --------------------------------------------------
  100. "connections": { <3>
  101. "vertices": [
  102. {
  103. "field": "query.raw"
  104. }
  105. ]
  106. }
  107. --------------------------------------------------
  108. +
  109. NOTE: Connections can be nested inside the `connections` object to
  110. explore additional relationships in the data. Each level of nesting is
  111. considered a _hop_, and proximity within the graph is often described in
  112. terms of _hop depth_.
  113. +
  114. .Properties for `connections`
  115. [%collapsible%open]
  116. ======
  117. query:::
  118. An optional _guiding query_ that constrains the Graph API as it
  119. explores connected terms. For example, you might want to direct the Graph
  120. API to ignore older data by specifying a query that identifies recent
  121. documents.
  122. vertices:::
  123. Contains the fields you are interested in. For example:
  124. +
  125. [source,js]
  126. --------------------------------------------------
  127. "vertices": [
  128. {
  129. "field": "query.raw",
  130. "size": 5,
  131. "min_doc_count": 10,
  132. "shard_min_doc_count": 3
  133. }
  134. ]
  135. --------------------------------------------------
  136. ======
  137. controls:: Direct the Graph API how to build the graph.
  138. +
  139. .Properties for `controls`
  140. [%collapsible%open]
  141. ======
  142. use_significance:::
  143. The `use_significance` flag filters associated terms so only those that are
  144. significantly associated with your query are included. For information about
  145. the algorithm used to calculate significance, see the
  146. {ref}/search-aggregations-bucket-significantterms-aggregation.html[significant_terms
  147. aggregation]. Defaults to `true`.
  148. sample_size:::
  149. Each _hop_ considers a sample of the best-matching documents on each
  150. shard. Using samples improves the speed of execution and keeps
  151. exploration focused on meaningfully-connected terms. Very small values
  152. (less than 50) might not provide sufficient weight-of-evidence to identify
  153. significant connections between terms. Very large sample sizes can dilute
  154. the quality of the results and increase execution times.
  155. Defaults to 100 documents.
  156. timeout:::
  157. The length of time in milliseconds after which exploration will be halted
  158. and the results gathered so far are returned. This timeout is honored on
  159. a best-effort basis. Execution might overrun this timeout if, for example,
  160. a long pause is encountered while FieldData is loaded for a field.
  161. sample_diversity:::
  162. To avoid the top-matching documents sample being dominated by a single
  163. source of results, it is sometimes necessary to request diversity in
  164. the sample. You can do this by selecting a single-value field and setting
  165. a maximum number of documents per value for that field. For example:
  166. +
  167. [source,js]
  168. --------------------------------------------------
  169. "sample_diversity": {
  170. "field": "category.raw",
  171. "max_docs_per_value": 500
  172. }
  173. --------------------------------------------------
  174. ======
  175. ====
  176. // [discrete]
  177. // === Authorization
  178. [discrete]
  179. === Examples
  180. [discrete]
  181. [[basic-search]]
  182. ==== Basic exploration
  183. An initial search typically begins with a query to identify strongly related terms.
  184. [source,console]
  185. --------------------------------------------------
  186. POST clicklogs/_graph/explore
  187. {
  188. "query": { <1>
  189. "match": {
  190. "query.raw": "midi"
  191. }
  192. },
  193. "vertices": [ <2>
  194. {
  195. "field": "product"
  196. }
  197. ],
  198. "connections": { <3>
  199. "vertices": [
  200. {
  201. "field": "query.raw"
  202. }
  203. ]
  204. }
  205. }
  206. --------------------------------------------------
  207. <1> Seed the exploration with a query. This example is searching
  208. clicklogs for people who searched for the term "midi".
  209. <2> Identify the vertices to include in the graph. This example is looking for
  210. product codes that are significantly associated with searches for "midi".
  211. <3> Find the connections. This example is looking for other search
  212. terms that led people to click on the products that are associated with
  213. searches for "midi".
  214. The response from the explore API looks like this:
  215. [source,js]
  216. --------------------------------------------------
  217. {
  218. "took": 0,
  219. "timed_out": false,
  220. "failures": [],
  221. "vertices": [ <1>
  222. {
  223. "field": "query.raw",
  224. "term": "midi cable",
  225. "weight": 0.08745858139552132,
  226. "depth": 1
  227. },
  228. {
  229. "field": "product",
  230. "term": "8567446",
  231. "weight": 0.13247784285434397,
  232. "depth": 0
  233. },
  234. {
  235. "field": "product",
  236. "term": "1112375",
  237. "weight": 0.018600718471158982,
  238. "depth": 0
  239. },
  240. {
  241. "field": "query.raw",
  242. "term": "midi keyboard",
  243. "weight": 0.04802242866755111,
  244. "depth": 1
  245. }
  246. ],
  247. "connections": [ <2>
  248. {
  249. "source": 0,
  250. "target": 1,
  251. "weight": 0.04802242866755111,
  252. "doc_count": 13
  253. },
  254. {
  255. "source": 2,
  256. "target": 3,
  257. "weight": 0.08120623870976627,
  258. "doc_count": 23
  259. }
  260. ]
  261. }
  262. --------------------------------------------------
  263. <1> An array of all of the vertices that were discovered. A vertex is an indexed
  264. term, so the field and term value are provided. The `weight` attribute specifies
  265. a significance score. The `depth` attribute specifies the hop-level at which
  266. the term was first encountered.
  267. <2> The connections between the vertices in the array. The `source` and `target`
  268. properties are indexed into the vertices array and indicate which vertex term led
  269. to the other as part of exploration. The `doc_count` value indicates how many
  270. documents in the sample set contain this pairing of terms (this is
  271. not a global count for all documents in the data stream or index).
  272. [discrete]
  273. [[optional-controls]]
  274. ==== Optional controls
  275. The default settings are configured to remove noisy data and
  276. get the "big picture" from your data. This example shows how to specify
  277. additional parameters to influence how the graph is built.
  278. For tips on tuning the settings for more detailed forensic evaluation where
  279. every document could be of interest, see the
  280. {kibana-ref}/graph-troubleshooting.html[Troubleshooting] guide.
  281. [source,console]
  282. --------------------------------------------------
  283. POST clicklogs/_graph/explore
  284. {
  285. "query": {
  286. "match": {
  287. "query.raw": "midi"
  288. }
  289. },
  290. "controls": {
  291. "use_significance": false, <1>
  292. "sample_size": 2000, <2>
  293. "timeout": 2000, <3>
  294. "sample_diversity": { <4>
  295. "field": "category.raw",
  296. "max_docs_per_value": 500
  297. }
  298. },
  299. "vertices": [
  300. {
  301. "field": "product",
  302. "size": 5, <5>
  303. "min_doc_count": 10, <6>
  304. "shard_min_doc_count": 3 <7>
  305. }
  306. ],
  307. "connections": {
  308. "query": { <8>
  309. "bool": {
  310. "filter": [
  311. {
  312. "range": {
  313. "query_time": {
  314. "gte": "2015-10-01 00:00:00"
  315. }
  316. }
  317. }
  318. ]
  319. }
  320. },
  321. "vertices": [
  322. {
  323. "field": "query.raw",
  324. "size": 5,
  325. "min_doc_count": 10,
  326. "shard_min_doc_count": 3
  327. }
  328. ]
  329. }
  330. }
  331. --------------------------------------------------
  332. <1> Disable `use_significance` to include all associated terms, not just the
  333. ones that are significantly associated with the query.
  334. <2> Increase the sample size to consider a larger set of documents on
  335. each shard.
  336. <3> Limit how long a graph request runs before returning results.
  337. <4> Ensure diversity in the sample by setting a limit on the number of documents
  338. per value in a particular single-value field, such as a category field.
  339. <5> Control the maximum number of vertex terms returned for each field.
  340. <6> Set a certainty threshold that specifies how many documents have to contain
  341. a pair of terms before we consider it to be a useful connection.
  342. <7> Specify how many documents on a shard have to contain a pair of terms before
  343. the connection is returned for global consideration.
  344. <8> Restrict which document are considered as you explore connected terms.
  345. [discrete]
  346. [[spider-search]]
  347. ==== Spidering operations
  348. After an initial search, you typically want to select vertices of interest and
  349. see what additional vertices are connected. In graph-speak, this operation is
  350. referred to as "spidering". By submitting a series of requests, you can
  351. progressively build a graph of related information.
  352. To spider out, you need to specify two things:
  353. * The set of vertices for which you want to find additional connections
  354. * The set of vertices you already know about that you want to exclude from the
  355. results of the spidering operation.
  356. You specify this information using `include`and `exclude` clauses. For example,
  357. the following request starts with the product `1854873` and spiders
  358. out to find additional search terms associated with that product. The terms
  359. "midi", "midi keyboard", and "synth" are excluded from the results.
  360. [source,console]
  361. --------------------------------------------------
  362. POST clicklogs/_graph/explore
  363. {
  364. "vertices": [
  365. {
  366. "field": "product",
  367. "include": [ "1854873" ] <1>
  368. }
  369. ],
  370. "connections": {
  371. "vertices": [
  372. {
  373. "field": "query.raw",
  374. "exclude": [ <2>
  375. "midi keyboard",
  376. "midi",
  377. "synth"
  378. ]
  379. }
  380. ]
  381. }
  382. }
  383. --------------------------------------------------
  384. <1> The vertices you want to start from are specified
  385. as an array of terms in an `include` clause.
  386. <2> The `exclude` clause prevents terms you already know about from being
  387. included in the results.