Sample UBI SQL queries

You can run sample User Behavior Insights (UBI) SQL queries in the OpenSearch Dashboards Query Workbench.

To query a demo workbench with synthetic data, see http://chorus-opensearch-edition.dev.o19s.com:5601/app/opensearch-query-workbench.

Queries with zero results

Queries can be executed on events on either the server (ubi_queries) or client (ubi_events) side.

Server-side queries

The UBI-activated search server logs the queries and their results, so in order to find all queries with no results, search for empty query_response_hit_ids:

  1. select
  2. count(*)
  3. from ubi_queries
  4. where query_response_hit_ids is null

Client-side events

Although it’s relatively straightforward to find queries with no results on the server side, you can also get the same result by querying the event attributes that were logged on the client side. Both client- and server-side queries return the same results. Use the following query to search for queries with no results:

  1. select
  2. count(0)
  3. from ubi_events
  4. where event_attributes.result_count > 0

Trending queries can be found by using either of the following queries.

Server-side

  1. select
  2. user_query, count(0) Total
  3. from ubi_queries
  4. group by user_query
  5. order by Total desc

Client-side

  1. select
  2. message, count(0) Total
  3. from ubi_events
  4. where
  5. action_name='on_search'
  6. group by message
  7. order by Total desc

Both queries return the distribution of search strings, as shown in the following table.

MessageTotal
User Behavior Insights127
best Laptop78
camera21
backpack17
briefcase14
camcorder11
cabinet9
bed9
box8
bottle8
calculator8
armchair7
bench7
blackberry6
bathroom6
User Behavior Insights Mac5
best Laptop Dell5
User Behavior Insights VTech5
ayoolaolafenwa5
User Behavior Insights Dell4
best Laptop Vaddio4
agrega modelos intuitivas4
bеуоnd4
abraza metodologías B2C3

Event type distribution counts

To create a pie chart widget visualizing the most common events, run the following query:

  1. select
  2. action_name, count(0) Total
  3. from ubi_events
  4. group by action_name
  5. order by Total desc

The results include a distribution across actions, as shown in the following table.

action_nameTotal
on_search5425
brand_filter3634
global_click3571
view_search_results3565
product_sort3558
type_filter3505
product_hover820
item_click708
purchase407
declined_product402
add_to_cart373
page_exit142
user_feedback123
404_redirect123

The following query shows the distribution of margins across user actions:

  1. select
  2. action_name,
  3. count(0) total,
  4. AVG( event_attributes.object.object_detail.cost ) average_cost,
  5. AVG( event_attributes.object.object_detail.margin ) average_margin
  6. from ubi_events
  7. group by action_name
  8. order by average_cost desc

The results include actions and the distribution across average costs and margins, as shown in the following table.

action_nametotalaverage_costaverage_margin
declined_product3958457.126190.96
item_click6907789.405862.70
add_to_cart3746470.224617.09
purchase3585933.835110.69
global_click3555  
product_sort3711  
product_hover779  
page_exit107  
on_search5438  
brand_filter3722  
user_feedback120  
404_redirect110  
view_search_results3639  
type_filter3691  

Sample search journey

To find a search in the query log, run the following query:

  1. select
  2. client_id, query_id, user_query, query_response_hit_ids, query_response_id, timestamp
  3. from ubi_queries where query_id = '7ae52966-4fd4-4ab1-8152-0fd0b52bdadf'

The following table shows the results of the preceding query.

client_idquery_iduser_queryquery_response_hit_idsquery_response_idtimestamp
a15f1ef3-6bc6-4959-9b83-6699a4d298457ae52966-4fd4-4ab1-8152-0fd0b52bdadfnotebook08827803916596e92c90c-1eee-4dd6-b820-c522fd4126f32024-06-04 19:02:45.728

The query field in query_id has the following nested structure:

  1. {
  2. "query": {
  3. "size": 25,
  4. "query": {
  5. "query_string": {
  6. "query": "(title:\"notebook\" OR attr_t_device_type:\"notebook\" OR name:\"notebook\")",
  7. "fields": [],
  8. "type": "best_fields",
  9. "default_operator": "or",
  10. "max_determinized_states": 10000,
  11. "enable_position_increments": true,
  12. "fuzziness": "AUTO",
  13. "fuzzy_prefix_length": 0,
  14. "fuzzy_max_expansions": 50,
  15. "phrase_slop": 0,
  16. "analyze_wildcard": false,
  17. "escape": false,
  18. "auto_generate_synonyms_phrase_query": true,
  19. "fuzzy_transpositions": true,
  20. "boost": 1.0
  21. }
  22. },
  23. "ext": {
  24. "query_id": "7ae52966-4fd4-4ab1-8152-0fd0b52bdadf",
  25. "user_query": "notebook",
  26. "client_id": "a15f1ef3-6bc6-4959-9b83-6699a4d29845",
  27. "object_id_field": "primary_ean",
  28. "query_attributes": {
  29. "application": "ubi-demo"
  30. }
  31. }
  32. }
  33. }

In the event log, ubi_events, search for the events that correspond to the preceding query (whose query ID is 7ae52966-4fd4-4ab1-8152-0fd0b52bdadf):

  1. select
  2. application, query_id, action_name, message_type, message, client_id, timestamp
  3. from ubi_events
  4. where query_id = '7ae52966-4fd4-4ab1-8152-0fd0b52bdadf'
  5. order by timestamp

The results include all events associated with the user’s query, as shown in the following table.

applicationquery_idaction_namemessage_typemessageclient_idtimestamp
ubi-demo7ae52966-4fd4-4ab1-8152-0fd0b52bdadfon_searchQUERYnotebooka15f1ef3-6bc6-4959-9b83-6699a4d298452024-06-04 19:02:45.777
ubi-demo7ae52966-4fd4-4ab1-8152-0fd0b52bdadfproduct_hoverINFOorquesta soluciones uno-a-unoa15f1ef3-6bc6-4959-9b83-6699a4d298452024-06-04 19:02:45.816
ubi-demo7ae52966-4fd4-4ab1-8152-0fd0b52bdadfitem_clickINFOinnova relaciones centrado al usuarioa15f1ef3-6bc6-4959-9b83-6699a4d298452024-06-04 19:02:45.86
ubi-demo7ae52966-4fd4-4ab1-8152-0fd0b52bdadfadd_to_cartCONVERSIONengineer B2B platformsa15f1ef3-6bc6-4959-9b83-6699a4d298452024-06-04 19:02:45.905
ubi-demo7ae52966-4fd4-4ab1-8152-0fd0b52bdadfpurchaseCONVERSIONPurchase item 0884420136132a15f1ef3-6bc6-4959-9b83-6699a4d298452024-06-04 19:02:45.913

User sessions

To find more of the same user’s sessions (with the client ID a15f1ef3-6bc6-4959-9b83-6699a4d29845), run the following query:

  1. select
  2. application, event_attributes.session_id, query_id,
  3. action_name, message_type, event_attributes.dwell_time,
  4. event_attributes.object.object_id,
  5. event_attributes.object.description,
  6. timestamp
  7. from ubi_events
  8. where client_id = 'a15f1ef3-6bc6-4959-9b83-6699a4d29845'
  9. order by query_id, timestamp

The results are truncated to show a sample of sessions, as shown in the following table.

applicationevent_attributes.session_idquery_idaction_namemessage_typeevent_attributes.dwell_timeevent_attributes.object.object_idevent_attributes.object.descriptiontimestamp
ubi-demo00731779-e290-4709-8af7-d495ae42bf480254a9b7-1d83-4083-aa46-e12dff86ec98on_searchQUERY46.6398  2024-06-04 19:06:36.239
ubi-demo00731779-e290-4709-8af7-d495ae42bf480254a9b7-1d83-4083-aa46-e12dff86ec98product_hoverINFO53.6818770065030834155USB 2.0 S-Video and Composite Video Capture Cable2024-06-04 19:06:36.284
ubi-demo00731779-e290-4709-8af7-d495ae42bf480254a9b7-1d83-4083-aa46-e12dff86ec98item_clickINFO40.6999970065030834155USB 2.0 S-Video and Composite Video Capture Cable2024-06-04 19:06:36.334
ubi-demo00731779-e290-4709-8af7-d495ae42bf480254a9b7-1d83-4083-aa46-e12dff86ec98declined_productREJECT5.05390550065030834155USB 2.0 S-Video and Composite Video Capture Cable2024-06-04 19:06:36.373
ubi-demo844ca4b5-b6f8-4f7b-a5ec-7f6d95788e0b0cf185be-91a8-49cf-9401-92ad079ce43bon_searchQUERY26.422775  2024-06-04 19:04:40.832
ubi-demo844ca4b5-b6f8-4f7b-a5ec-7f6d95788e0b0cf185be-91a8-49cf-9401-92ad079ce43bon_searchQUERY17.1094  2024-06-04 19:04:40.837
ubi-demo844ca4b5-b6f8-4f7b-a5ec-7f6d95788e0b0cf185be-91a8-49cf-9401-92ad079ce43bbrand_filterFILTER40.090374OBJECT-6c91da98-387b-45cb-8275-e90d1ea8bc54supplier_name2024-06-04 19:04:40.852
ubi-demo844ca4b5-b6f8-4f7b-a5ec-7f6d95788e0b0cf185be-91a8-49cf-9401-92ad079ce43btype_filterINFO37.658962OBJECT-32d9bb39-b17d-4611-82c1-5aaa14368060filter_product_type2024-06-04 19:04:40.856
ubi-demo844ca4b5-b6f8-4f7b-a5ec-7f6d95788e0b0cf185be-91a8-49cf-9401-92ad079ce43bproduct_sortSORT3.6380951  2024-06-04 19:04:40.923
ubi-demo844ca4b5-b6f8-4f7b-a5ec-7f6d95788e0b0cf185be-91a8-49cf-9401-92ad079ce43bview_search_resultsINFO46.436115  2024-06-04 19:04:40.942
ubi-demo844ca4b5-b6f8-4f7b-a5ec-7f6d95788e0b0cf185be-91a8-49cf-9401-92ad079ce43bview_search_resultsINFO46.436115  2024-06-04 19:04:40.959
ubi-demo844ca4b5-b6f8-4f7b-a5ec-7f6d95788e0b0cf185be-91a8-49cf-9401-92ad079ce43btype_filterINFO37.658962OBJECT-32d9bb39-b17d-4611-82c1-5aaa14368060filter_product_type2024-06-04 19:04:40.972
ubi-demo844ca4b5-b6f8-4f7b-a5ec-7f6d95788e0b0cf185be-91a8-49cf-9401-92ad079ce43bbrand_filterFILTER40.090374OBJECT-6c91da98-387b-45cb-8275-e90d1ea8bc54supplier_name2024-06-04 19:04:40.997
ubi-demo844ca4b5-b6f8-4f7b-a5ec-7f6d95788e0b0cf185be-91a8-49cf-9401-92ad079ce43btype_filterINFO37.658962OBJECT-32d9bb39-b17d-4611-82c1-5aaa14368060filter_product_type2024-06-04 19:04:41.006
ubi-demo844ca4b5-b6f8-4f7b-a5ec-7f6d95788e0b0cf185be-91a8-49cf-9401-92ad079ce43bproduct_sortSORT3.6380951  2024-06-04 19:04:41.031
ubi-demo844ca4b5-b6f8-4f7b-a5ec-7f6d95788e0b0cf185be-91a8-49cf-9401-92ad079ce43bproduct_sortSORT3.6380951  2024-06-04 19:04:41.091
ubi-demo844ca4b5-b6f8-4f7b-a5ec-7f6d95788e0b0cf185be-91a8-49cf-9401-92ad079ce43btype_filterINFO37.658962OBJECT-32d9bb39-b17d-4611-82c1-5aaa14368060filter_product_type2024-06-04 19:04:41.164
ubi-demo844ca4b5-b6f8-4f7b-a5ec-7f6d95788e0b0cf185be-91a8-49cf-9401-92ad079ce43bbrand_filterFILTER40.090374OBJECT-6c91da98-387b-45cb-8275-e90d1ea8bc54supplier_name2024-06-04 19:04:41.171
ubi-demo844ca4b5-b6f8-4f7b-a5ec-7f6d95788e0b0cf185be-91a8-49cf-9401-92ad079ce43bview_search_resultsINFO46.436115  2024-06-04 19:04:41.179
ubi-demo844ca4b5-b6f8-4f7b-a5ec-7f6d95788e0b0cf185be-91a8-49cf-9401-92ad079ce43bglobal_clickINFO42.45651OBJECT-d350cc2d-b979-4aca-bd73-71709832940f(96, 127)2024-06-04 19:04:41.224
ubi-demo844ca4b5-b6f8-4f7b-a5ec-7f6d95788e0b0cf185be-91a8-49cf-9401-92ad079ce43bview_search_resultsINFO46.436115  2024-06-04 19:04:41.24
ubi-demo844ca4b5-b6f8-4f7b-a5ec-7f6d95788e0b0cf185be-91a8-49cf-9401-92ad079ce43bview_search_resultsINFO46.436115  2024-06-04 19:04:41.285
ubi-demo844ca4b5-b6f8-4f7b-a5ec-7f6d95788e0b0cf185be-91a8-49cf-9401-92ad079ce43bglobal_clickINFO42.45651OBJECT-d350cc2d-b979-4aca-bd73-71709832940f(96, 127)2024-06-04 19:04:41.328
ubi-demo33bd0ee2-60b7-4c25-b62c-1aa1580da73c2071e273-513f-46be-b835-89f452095053on_searchQUERY52.721157  2024-06-04 19:03:50.8
ubi-demo33bd0ee2-60b7-4c25-b62c-1aa1580da73c2071e273-513f-46be-b835-89f452095053view_search_resultsINFO26.600422  2024-06-04 19:03:50.802
ubi-demo33bd0ee2-60b7-4c25-b62c-1aa1580da73c2071e273-513f-46be-b835-89f452095053product_sortSORT14.839713  2024-06-04 19:03:50.875
ubi-demo33bd0ee2-60b7-4c25-b62c-1aa1580da73c2071e273-513f-46be-b835-89f452095053brand_filterFILTER20.876852OBJECT-6c91da98-387b-45cb-8275-e90d1ea8bc54supplier_name2024-06-04 19:03:50.927
ubi-demo33bd0ee2-60b7-4c25-b62c-1aa1580da73c2071e273-513f-46be-b835-89f452095053type_filterINFO15.212905OBJECT-32d9bb39-b17d-4611-82c1-5aaa14368060filter_product_type2024-06-04 19:03:50.997
ubi-demo33bd0ee2-60b7-4c25-b62c-1aa1580da73c2071e273-513f-46be-b835-89f452095053view_search_resultsINFO26.600422  2024-06-04 19:03:51.033
ubi-demo33bd0ee2-60b7-4c25-b62c-1aa1580da73c2071e273-513f-46be-b835-89f452095053global_clickINFO11.710514OBJECT-d350cc2d-b979-4aca-bd73-71709832940f(96, 127)2024-06-04 19:03:51.108
ubi-demo33bd0ee2-60b7-4c25-b62c-1aa1580da73c2071e273-513f-46be-b835-89f452095053product_sortSORT14.839713  2024-06-04 19:03:51.144
ubi-demo33bd0ee2-60b7-4c25-b62c-1aa1580da73c2071e273-513f-46be-b835-89f452095053global_clickINFO11.710514OBJECT-d350cc2d-b979-4aca-bd73-71709832940f(96, 127)2024-06-04 19:03:51.17
ubi-demo33bd0ee2-60b7-4c25-b62c-1aa1580da73c2071e273-513f-46be-b835-89f452095053brand_filterFILTER20.876852OBJECT-6c91da98-387b-45cb-8275-e90d1ea8bc54supplier_name2024-06-04 19:03:51.205
ubi-demo33bd0ee2-60b7-4c25-b62c-1aa1580da73c2071e273-513f-46be-b835-89f452095053type_filterINFO15.212905OBJECT-32d9bb39-b17d-4611-82c1-5aaa14368060filter_product_type2024-06-04 19:03:51.228
ubi-demo33bd0ee2-60b7-4c25-b62c-1aa1580da73c2071e273-513f-46be-b835-89f452095053product_sortSORT14.839713  2024-06-04 19:03:51.232
ubi-demo33bd0ee2-60b7-4c25-b62c-1aa1580da73c2071e273-513f-46be-b835-89f452095053type_filterINFO15.212905OBJECT-32d9bb39-b17d-4611-82c1-5aaa14368060filter_product_type2024-06-04 19:03:51.292
ubi-demo33bd0ee2-60b7-4c25-b62c-1aa1580da73c2071e273-513f-46be-b835-89f452095053type_filterINFO15.212905OBJECT-32d9bb39-b17d-4611-82c1-5aaa14368060filter_product_type2024-06-04 19:03:51.301
ubi-demo33bd0ee2-60b7-4c25-b62c-1aa1580da73c23f0149a-13ae-4977-8dc9-ef61c449c140on_searchQUERY16.93674  2024-06-04 19:03:50.62
ubi-demo33bd0ee2-60b7-4c25-b62c-1aa1580da73c23f0149a-13ae-4977-8dc9-ef61c449c140global_clickINFO25.897957OBJECT-d350cc2d-b979-4aca-bd73-71709832940f(96, 127)2024-06-04 19:03:50.624
ubi-demo33bd0ee2-60b7-4c25-b62c-1aa1580da73c23f0149a-13ae-4977-8dc9-ef61c449c140product_sortSORT44.345097  2024-06-04 19:03:50.688
ubi-demo33bd0ee2-60b7-4c25-b62c-1aa1580da73c23f0149a-13ae-4977-8dc9-ef61c449c140brand_filterFILTER19.54417OBJECT-6c91da98-387b-45cb-8275-e90d1ea8bc54supplier_name2024-06-04 19:03:50.696
ubi-demo33bd0ee2-60b7-4c25-b62c-1aa1580da73c23f0149a-13ae-4977-8dc9-ef61c449c140type_filterINFO48.79312OBJECT-32d9bb39-b17d-4611-82c1-5aaa14368060filter_product_type2024-06-04 19:03:50.74
ubi-demo33bd0ee2-60b7-4c25-b62c-1aa1580da73c23f0149a-13ae-4977-8dc9-ef61c449c140brand_filterFILTER19.54417OBJECT-6c91da98-387b-45cb-8275-e90d1ea8bc54supplier_name2024-06-04 19:03:50.802

List user sessions for users who logged out without submitting any queries

The following query searches for users who don’t have an associated query_id. Note that this may happen if the client side does not pass the returned query to other events.

  1. select
  2. client_id, session_id, count(0) EventTotal
  3. from ubi_events
  4. where action_name='logout' and query_id is null
  5. group by client_id, session_id
  6. order by EventTotal desc

The following table shows the client ID, session ID, and that there was 1 event,logout.

client_idsession_idEventTotal
100_15c182f2-05db-4f4f-814f-46dc0de6b9ea1c36712c-44b8-4fdd-8f0d-fdfeab5bd794_12901
175_e5f262f1-0db3-4948-b349-c5b95ff31259816f94d6-8966-4a8b-8984-a2641d5865b2_22511
175_e5f262f1-0db3-4948-b349-c5b95ff31259314dc1ff-ef38-4da4-b4b1-061f62dddcbb_22481
175_e5f262f1-0db3-4948-b349-c5b95ff312591ce5dc30-31bb-4759-9451-5a99b28ba91b_22551
175_e5f262f1-0db3-4948-b349-c5b95ff3125910ac0fc0-409e-4ba0-98e9-edb323556b1a_22491
174_ab59e589-1ae4-40be-8b29-8efd9fc15380dfa8b38a-c451-4190-a391-2e1ec3c8f196_22281
174_ab59e589-1ae4-40be-8b29-8efd9fc1538068666e11-087a-4978-9ca7-cbac6862273e_22331
174_ab59e589-1ae4-40be-8b29-8efd9fc153805ca7a0df-f750-4656-b9a5-5eef1466ba09_22341
174_ab59e589-1ae4-40be-8b29-8efd9fc15380228c1135-b921-45f4-b087-b3422e7ed437_22361
173_39d4cbfd-0666-4e77-84a9-965ed785db49f9795e2e-ad92-4f15-8cdd-706aa1a3a17b_22061
173_39d4cbfd-0666-4e77-84a9-965ed785db49f3c18b61-2c8a-41b3-a023-11eb2dd6c93c_22071
173_39d4cbfd-0666-4e77-84a9-965ed785db49e12f700c-ffa3-4681-90d9-146022e26a18_22101
173_39d4cbfd-0666-4e77-84a9-965ed785db49da1ff1f6-26f1-49d4-bd0d-d32d199e270e_22081
173_39d4cbfd-0666-4e77-84a9-965ed785db49a1674e9d-d2dd-4da9-a4d1-dd12a401e8e7_22161
172_875f04d6-2c35-45f4-a8ac-bc5b675425f6cc8e6174-5c1a-48c5-8ee8-1226621fe9f7_22031
171_7d810730-d6e9-4079-ab1c-db7f98776985927fcfed-61d2-4334-91e9-77442b077764_21891
16_581fe410-338e-457b-a790-85af2a64235683a68f57-0fbb-4414-852b-4c4601bf6cf2_1561
16_581fe410-338e-457b-a790-85af2a6423567881141b-511b-4df9-80e6-5450415af42c_1621
16_581fe410-338e-457b-a790-85af2a6423561d64478e-c3a6-4148-9a64-b6f4a73fc684_1581

You may want to identify users who logged out multiple times without submitting a query. The following query lets you see which users do this the most:

  1. select
  2. client_id, count(0) EventTotal
  3. from ubi_events
  4. where action_name='logout' and query_id is null
  5. group by client_id
  6. order by EventTotal desc

The following table shows user client IDs and the number of logouts without any queries.

client_idEventTotal
87_5a6e1f8c-4936-4184-a24d-beddd05c92748
127_829a4246-930a-4b24-8165-caa07ee3fa477
49_5da537a3-8d94-48d1-a0a4-dcad21c126156
56_6c7c2525-9ca5-4d5d-8ac0-acb43769ac0b6
140_61192c8e-c532-4164-ad1b-1afc58c265b76
149_3443895e-6f81-4706-8141-1ebb0c2470ca6
196_4359f588-10be-4b2c-9e7f-ee846a75a3f66
173_39d4cbfd-0666-4e77-84a9-965ed785db495
52_778ac7f3-8e60-444e-ad40-d24516bf4ce25
51_6335e0c3-7bea-4698-9f83-25c9fb984e125
175_e5f262f1-0db3-4948-b349-c5b95ff312595
61_feb3a495-c1fb-40ea-8331-81cee53a5eb95
181_f227264f-cabd-4468-bfcc-4801baeebd395
185_435d1c63-4829-45f3-abff-352ef6458f0e5
100_15c182f2-05db-4f4f-814f-46dc0de6b9ea5
113_df32ed6e-d74a-4956-ac8e-6d43d8d603175
151_0808111d-07ce-4c84-a0fd-7125e4e330205
204_b75e374c-4813-49c4-b111-4bf4fdab6f265
29_ec2133e5-4d9b-4222-aa7c-2a9ae0880ddd5
41_f64abc69-56ea-4dd3-a991-7d1fd292a5305