Explore API

The Graph explore API enables you to extract and summarize information about the documents and terms in your Elasticsearch index.

The easiest way to understand the behaviour of this API is to use the Graph UI to explore connections. You can view the most recent request submitted to the _explore endpoint from the Last request panel. For more information, see Getting Started with Graph.

For additional information about working with the explore API, see the Graph Troubleshooting and Limitations topics.

The graph explore API is enabled by default. To disable access to the graph explore API and the Kibana Graph UI, add xpack.graph.enabled: false to elasticsearch.yml.

Request

POST <index>/_graph/explore

Description

An initial request to the _explore API contains a seed query that identifies the documents of interest and specifies the fields that define the vertices and connections you want to include in the graph. Subsequent _explore requests enable you to spider out from one more vertices of interest. You can exclude vertices that have already been returned.

Request Body

query

A seed query that identifies the documents of interest. Can be any valid Elasticsearch query. For example:

  1. "query": {
  2. "bool": {
  3. "must": {
  4. "match": {
  5. "query.raw": "midi"
  6. }
  7. },
  8. "filter": [
  9. {
  10. "range": {
  11. "query_time": {
  12. "gte": "2015-10-01 00:00:00"
  13. }
  14. }
  15. }
  16. ]
  17. }
  18. }

vertices

Specifies or more fields that contain the terms you want to include in the graph as vertices. For example:

  1. "vertices": [
  2. {
  3. "field": "product"
  4. }
  5. ]

Properties for vertices

  • field

    Identifies a field in the documents of interest.

    include

    Identifies the terms of interest that form the starting points from which you want to spider out. You do not have to specify a seed query if you specify an include clause. The include clause implicitly querys for documents that contain any of the listed terms listed. In addition to specifying a simple array of strings, you can also pass objects with term and boost values to boost matches on particular terms.

    exclude

    The exclude clause prevents the specified terms from being included in the results.

    size

    Specifies the maximum number of vertex terms returned for each field. Defaults to 5.

    min_doc_count

    Specifies how many documents must contain a pair of terms before it is considered to be a useful connection. This setting acts as a certainty threshold. Defaults to 3.

    shard_min_doc_count

    This advanced setting controls how many documents on a particular shard have to contain a pair of terms before the connection is returned for global consideration. Defaults to 2.

connections

Specifies or more fields from which you want to extract terms that are associated with the specified vertices. For example:

  1. "connections": {
  2. "vertices": [
  3. {
  4. "field": "query.raw"
  5. }
  6. ]
  7. }

Connections can be nested inside the connections object to explore additional relationships in the data. Each level of nesting is considered a hop, and proximity within the graph is often described in terms of hop depth.

Properties for connections

  • query

    An optional guiding query that constrains the Graph API as it explores connected terms. For example, you might want to direct the Graph API to ignore older data by specifying a query that identifies recent documents.

    vertices

    Contains the fields you are interested in. For example:

    1. "vertices": [
    2. {
    3. "field": "query.raw",
    4. "size": 5,
    5. "min_doc_count": 10,
    6. "shard_min_doc_count": 3
    7. }
    8. ]

controls

Direct the Graph API how to build the graph.

Properties for controls

  • use_significance

    The use_significance flag filters associated terms so only those that are significantly associated with your query are included. For information about the algorithm used to calculate significance, see the significant_terms aggregation. Defaults to true.

    sample_size

    Each hop considers a sample of the best-matching documents on each shard. Using samples improves the speed of execution and keeps exploration focused on meaningfully-connected terms. Very small values (less than 50) might not provide sufficient weight-of-evidence to identify significant connections between terms. Very large sample sizes can dilute the quality of the results and increase execution times. Defaults to 100 documents.

    timeout

    The length of time in milliseconds after which exploration will be halted and the results gathered so far are returned. This timeout is honored on a best-effort basis. Execution might overrun this timeout if, for example, a long pause is encountered while FieldData is loaded for a field.

    sample_diversity

    To avoid the top-matching documents sample being dominated by a single source of results, it is sometimes necessary to request diversity in the sample. You can do this by selecting a single-value field and setting a maximum number of documents per value for that field. For example:

    1. "sample_diversity": {
    2. "field": "category.raw",
    3. "max_docs_per_value": 500
    4. }

Examples

Basic exploration

An initial search typically begins with a query to identify strongly related terms.

  1. POST clicklogs/_graph/explore
  2. {
  3. "query": {
  4. "match": {
  5. "query.raw": "midi"
  6. }
  7. },
  8. "vertices": [
  9. {
  10. "field": "product"
  11. }
  12. ],
  13. "connections": {
  14. "vertices": [
  15. {
  16. "field": "query.raw"
  17. }
  18. ]
  19. }
  20. }

Seed the exploration with a query. This example is searching clicklogs for people who searched for the term “midi”.

Identify the vertices to include in the graph. This example is looking for product codes that are significantly associated with searches for “midi”.

Find the connections. This example is looking for other search terms that led people to click on the products that are associated with searches for “midi”.

The response from the explore API looks like this:

  1. {
  2. "took": 0,
  3. "timed_out": false,
  4. "failures": [],
  5. "vertices": [
  6. {
  7. "field": "query.raw",
  8. "term": "midi cable",
  9. "weight": 0.08745858139552132,
  10. "depth": 1
  11. },
  12. {
  13. "field": "product",
  14. "term": "8567446",
  15. "weight": 0.13247784285434397,
  16. "depth": 0
  17. },
  18. {
  19. "field": "product",
  20. "term": "1112375",
  21. "weight": 0.018600718471158982,
  22. "depth": 0
  23. },
  24. {
  25. "field": "query.raw",
  26. "term": "midi keyboard",
  27. "weight": 0.04802242866755111,
  28. "depth": 1
  29. }
  30. ],
  31. "connections": [
  32. {
  33. "source": 0,
  34. "target": 1,
  35. "weight": 0.04802242866755111,
  36. "doc_count": 13
  37. },
  38. {
  39. "source": 2,
  40. "target": 3,
  41. "weight": 0.08120623870976627,
  42. "doc_count": 23
  43. }
  44. ]
  45. }

An array of all of the vertices that were discovered. A vertex is an indexed term, so the field and term value are provided. The weight attribute specifies a significance score. The depth attribute specifies the hop-level at which the term was first encountered.

The connections between the vertices in the array. The source and target properties are indexed into the vertices array and indicate which vertex term led to the other as part of exploration. The doc_count value indicates how many documents in the sample set contain this pairing of terms (this is not a global count for all documents in the index).

Optional controls

The default settings are configured to remove noisy data and get the “big picture” from your data. This example shows how to specify additional parameters to influence how the graph is built.

For tips on tuning the settings for more detailed forensic evaluation where every document could be of interest, see the Troubleshooting guide.

  1. POST clicklogs/_graph/explore
  2. {
  3. "query": {
  4. "match": {
  5. "query.raw": "midi"
  6. }
  7. },
  8. "controls": {
  9. "use_significance": false,
  10. "sample_size": 2000,
  11. "timeout": 2000,
  12. "sample_diversity": {
  13. "field": "category.raw",
  14. "max_docs_per_value": 500
  15. }
  16. },
  17. "vertices": [
  18. {
  19. "field": "product",
  20. "size": 5,
  21. "min_doc_count": 10,
  22. "shard_min_doc_count": 3
  23. }
  24. ],
  25. "connections": {
  26. "query": {
  27. "bool": {
  28. "filter": [
  29. {
  30. "range": {
  31. "query_time": {
  32. "gte": "2015-10-01 00:00:00"
  33. }
  34. }
  35. }
  36. ]
  37. }
  38. },
  39. "vertices": [
  40. {
  41. "field": "query.raw",
  42. "size": 5,
  43. "min_doc_count": 10,
  44. "shard_min_doc_count": 3
  45. }
  46. ]
  47. }
  48. }

Disable use_significance to include all associated terms, not just the ones that are significantly associated with the query.

Increase the sample size to consider a larger set of documents on each shard.

Limit how long a graph request runs before returning results.

Ensure diversity in the sample by setting a limit on the number of documents per value in a particular single-value field, such as a category field.

Control the maximum number of vertex terms returned for each field.

Set a certainty threshold that specifies how many documents have to contain a pair of terms before we consider it to be a useful connection.

Specify how many documents on a shard have to contain a pair of terms before the connection is returned for global consideration.

Restrict which document are considered as you explore connected terms.

Spidering operations

After an initial search, you typically want to select vertices of interest and see what additional vertices are connected. In graph-speak, this operation is referred to as “spidering”. By submitting a series of requests, you can progressively build a graph of related information.

To spider out, you need to specify two things:

  • The set of vertices for which you want to find additional connections
  • The set of vertices you already know about that you want to exclude from the results of the spidering operation.

You specify this information using include`and `exclude clauses. For example, the following request starts with the product 1854873 and spiders out to find additional search terms associated with that product. The terms “midi”, “midi keyboard”, and “synth” are excluded from the results.

  1. POST clicklogs/_graph/explore
  2. {
  3. "vertices": [
  4. {
  5. "field": "product",
  6. "include": [ "1854873" ]
  7. }
  8. ],
  9. "connections": {
  10. "vertices": [
  11. {
  12. "field": "query.raw",
  13. "exclude": [
  14. "midi keyboard",
  15. "midi",
  16. "synth"
  17. ]
  18. }
  19. ]
  20. }
  21. }

The vertices you want to start from are specified as an array of terms in an include clause.

The exclude clause prevents terms you already know about from being included in the results.