explore.asciidoc 12 KB

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