explore.asciidoc 13 KB

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