How filter search in elasticlunr.js - javascript

i am trying to search an index generated by #gatsby-contrib/elasticlunr the generated index looks like this:
{
"version": "0.9.5",
"fields": ["title", "path", "section"],
"ref": "id",
"documentStore": {
"docs": {
"fe565569-77a5-566d-bb47-96a6094c22c5": {
"id": "fe565569-77a5-566d-bb47-96a6094c22c5",
"title": "schema",
"path": "/graphql/schema",
"section": "graphql"
},
"cd1cdd40-4bb7-5ff6-9908-6c9ad692e75c": {
"id": "cd1cdd40-4bb7-5ff6-9908-6c9ad692e75c",
"title": "Component",
"path": "/react/component",
"section": "react"
},
"c1aecadb-3d1e-5d49-87f3-2b6f2c73433c": {
"id": "c1aecadb-3d1e-5d49-87f3-2b6f2c73433c",
"title": "Component",
"path": "/react/component2",
"section": "react"
},
"07159f12-dafb-53f6-b1ad-5032d56d25bb": {
"id": "07159f12-dafb-53f6-b1ad-5032d56d25bb",
"title": "Lazy",
"path": "/react/suspense",
"section": "react"
},
"380309db-ffa1-5f24-a192-36ac36b90a06": {
"id": "380309db-ffa1-5f24-a192-36ac36b90a06",
"title": "suspense",
"path": "/react/lazy",
"section": "react"
},
"380309db-ffa1-5f24-a192-36ac36b90uuuu": {
"id": "380309db-ffa1-5f24-a192-36ac36b9uuuu",
"title": "super",
"path": "/graphql/super",
"section": "graphql"
}
}
}
.....
}
is there a way i can get only get results whose section == react alone, and not all docs that match a search query in terms of a field.
e.g
when i search with a term su with expand: true in the config and set a filter section = 'graphql' it should return :
"380309db-ffa1-5f24-a192-36ac36b90uuuu": {
"id": "380309db-ffa1-5f24-a192-36ac36b9uuuu",
"title": "super",
"path": "/graphql/super",
"section": "graphql"
}
but what i am currently getting is :
{
"380309db-ffa1-5f24-a192-36ac36b90a06": {
"id": "380309db-ffa1-5f24-a192-36ac36b90a06",
"title": "suspense",
"path": "/react/lazy",
"section": "react"
},
"380309db-ffa1-5f24-a192-36ac36b90uuuu": {
"id": "380309db-ffa1-5f24-a192-36ac36b9uuuu",
"title": "super",
"path": "/graphql/super",
"section": "graphql"
}
}
Thanks!

You can't filter like this with elasticlunr.js. It's a Lightweight full-text search engine.
Also elasticlunr does not support regex
Alternatively you can filter result by using loop again.
var finalResult ={};
for (var key in label) {
var result = label[key];
// graphql
if (result.doc.section === 'graphql' && !!result.doc.title.match(/su/g)) {
finalResult[key] = result;
}
console.log(finalResult);
}
// output
console.log(finalResult);
Thank you

Related

How to dynamically create and iterate Json Object inside a Json Array using Postman - Javascript

I have a payload like mentioned below :
{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters": {
"batter": [
{
"id": "1001",
"type": "Regular"
},
{
"id": "1002",
"type": "Chocolate"
},
{
"id": "1003",
"type": "Blueberry"
},
{
"id": "1004",
"type": "Devil’sFood"
}
]
},
"topping": [
{
"id": "5001",
"type": "None"
},
{
"id": "5002",
"type": "Glazed"
},
{
"id": "5005",
"type": "Sugar"
},
{
"id": "5007",
"type": "PowderedSugar"
},
{
"id": "5006",
"type": "ChocolatewithSprinkles"
},
{
"id": "5003",
"type": "Chocolate"
},
{
"id": "5004",
"type": "Maple"
}
]
}
**I want to increment the json objects dynamically(it can be a duplicate as well) which is inside the array topping based on the array size. For example if mention the array size as topping[10] it is suppose to create a payload of 10 objects and push those 10 objects of similar type inside the array topping ** Is it possible to dynamically create json objects and post the request in postman??
Kind note : The size of the array should be parameterized. Please let me know.
Please find the image highlighted in green. I want to dynamically increase the payload(topping array size based on the index using postman
You could do this:
Tab Pre-request
let req = {
"id": "0001",
"type": "donut",
"topping": []
};
let numberOfTopping = 5;
for (let i = 0; i < numberOfTopping; i++) {
let toppingItem = {
"id": `${_.random(5001, 5010)}`,
"type": `${_.sample(["Glazed", "Sugar", "None"])}`
};
req.topping[i] = toppingItem;
}
pm.variables.set("req", JSON.stringify(req));
Tab body
Result
{
"id": "0001",
"type": "donut",
"topping": [
{
"id": "5006",
"type": "Glazed"
},
{
"id": "5001",
"type": "Sugar"
},
{
"id": "5006",
"type": "Glazed"
},
{
"id": "5006",
"type": "None"
},
{
"id": "5008",
"type": "Sugar"
}
]
}

json-schema-faker including extra url in results

I'm setting up json-schema-faker to create some mock data for a project.
When I generate the data it includes an extra bit with the following as the last item in the generated data.
"id": "http://json-schema.org/schema#"
Here is my schema (from a file named 'mockDataSchema.js'):
exports.schema = {
"type": "object",
"properties": {
"users": {
"type": "array",
"minItems": 2,
"maxItems": 2,
"items": {
"type": "object",
"properties": {
"id": {
"type": "number",
"unique": true,
"minimum": 1
},
"firstName": {
"type": "string",
"faker": "name.firstName"
},
"lastName": {
"type": "string",
"faker": "name.lastName"
},
"email": {
"type": "string",
"faker": "internet.email"
}
},
"required": ["id", "firstName", "lastName", "email"]
}
}
},
"required": ["users"]
}
The code to generate the data ('generate-mock-data.js'):
var jsf = require('json-schema-faker')
var schema = require('./mockDataSchema')
var fs = require('fs')
var chalk = require('chalk')
const json = JSON.stringify(jsf(schema))
console.log(json)
fs.writeFile('./src/api/db.json', json, function(err) {
if (err) {
return console.log(chalk.red(err))
} else {
console.log(chalk.green('Mock data generated.'))
}
})
Add the data that is returned:
{
"schema": {
"users": [
{ "id": 25582343, "firstName": "Brycen", "lastName": "Dickens", "email": "Angelica_Jakubowski#hotmail.com" },
{ "id": 39817508, "firstName": "Marisa", "lastName": "Terry", "email": "Arlo.Hermann0#yahoo.com" }
]
},
"id": "http://json-schema.org/schema#"
}
I've been unable to determine why it includes the "id": "http://json-schema.org/schema#"
I would like to get rid of that line. I'll be using this with 'json-server' to provide a mock api and it chokes on that line.
Figured this out.
exports.schema = {
was causing jscon-schema-faker to interpret '.schema' as part of the schema.
Changed to
module.exports = {
and the trailing schema link wnet away.

Why does my Preview Window doesn't show FormElement Content

I am trying to add a sap.ui.layout.form.Form with FormContainer and FormElements to a Preview Dialog. However, my Form and its Elements doesn't get rendered.
Click Here:
{
"Type": "sap.ui.core.mvc.JSONView",
"content": [
{
"Type": "sap.m.Button",
"id": "testitemid0",
"text": "MyTestButton",
"press": "asdf"
},
{
"Type": "sap.ui.layout.form.Form",
"id": "testitemid1",
"formContainers": [
{
"Type": "sap.ui.layout.form.FormContainer",
"id": "testitemid2",
"formElements": [
{
"Type": "sap.ui.layout.form.FormElement",
"id": "testitemid3",
"label": {
"Type": "sap.m.Label",
"id": "testitemid4",
"text": "My Test Label"
},
"fields": [
{
"Type": "sap.m.Input",
"id": "testitemid5",
"value": "My Test Input",
"placeholder": ""
}
]
}
]
}
]
}
]
}
Any Idea why it doesn't render the Form?
My sap.ui.layout.form.Form-Element simply lacks a layout:
{
"Type": "sap.ui.layout.form.Form",
"id": "testitemid1",
"layout": {
"Type": "sap.ui.layout.form.GridLayout"
},
...
}
JSBin

Passing function argument to retrieve data from an object

I am have some trouble with a script I am working on. I have been provided with an object with multiple items from a product catalog.
What I am trying to do is to write a function which to which will allow me to render this data easily.
<script type="application/javascript">
SKUinfo =
{
"s238554": {
"Age": {
"Description": "Age 18+",
"Thumbnail": "/productImages/assets/img/icon18.gif"
},
"Barcode": {
"Barcode": "50622132430794"
},
"Currency": "£",
"Description": "Description goes here",
"Id": 44305,
"Packshots": [
"/productImages/238556/1min.jpg",
"/productImages/238556/2med.jpg",
"/productImages/238556/3max.jpg"
],
"Pegis": [],
"Platform": {
"Button": "Xbox 360",
"ID": 0
},
"Publisher": {
"Description": null
},
"Release": "/Date(1392940800000+0000)/",
"Screenshots": [
{
"ScreenshotMax": "/productImages/238556/5scrmax1.jpg",
"ScreenshotMin": "/productImages/238556/4scrmin1.jpg"
}
],
"Title": "Product title 2 goes here",
"Variants": [
{
"Id": 58242,
"MaxOrderQuantity": 3,
"Presellable": true,
"Price": 29.97,
"PriceCultureFormat": "29.97",
"PriceWithCurrencyFormat": "£29.97",
"Sku": 238556,
"Type": {
"Description": "New"
}
},
],
"Vendor": {
"Description": ""
},
},
"s238556": {
"Age": {
"Description": "Age 18+",
"Thumbnail": "/productImages/assets/img/pegi/icon18.gif"
},
"Barcode": {
"Barcode": "5060134530794"
},
"Currency": "£",
"Description": "Description here",
"Id": 654654,
"Packshots": [
"/productImages/238556/1min.jpg",
"/productImages/238556/2med.jpg",
"/productImages/238556/3max.jpg"
],
"Pegis": [],
"Platform": {
"Button": "PlayStation 3",
"ID": 0
},
"Publisher": {
"Description": null
},
"Release": "/Date(1392940800000+0000)/",
"Screenshots": [
{
"ScreenshotMax": "/productImages/238556/5scrmax1.jpg",
"ScreenshotMin": "/productImages/238556/4scrmin1.jpg"
},
{
"ScreenshotMax": "/productImages/238556/7scrmax2.jpg",
"ScreenshotMin": "/productImages/238556/6scrmin2.jpg"
},
],
"Title": "Product title 2 goes here",
"Variants": [
{
"Id": 58242,
"MaxOrderQuantity": 3,
"Presellable": true,
"Price": 29.97,
"PriceCultureFormat": "29.97",
"PriceWithCurrencyFormat": "£29.97",
"Sku": 238556,
"Type": {
"Description": "New"
}
},
],
"Vendor": {
"Description": ""
},
"VideoHTML": "html here",
"status": {
"Response": "product found",
"Success": true
}
}
}
</script>
The above example is the output I get for two products.
If I try to get access to this data this is where I have a problem
<script type="application/javascript">
function getSKU(s)
{
console.log(SKUinfo.s.Title);
}
getSKU(s238554);
</script>
I imagine this is being caused when I am passing the argument s back to the function getSKU a the node selection in the data object. In this I would expect the console output to be the Title from SKU s238554.
What I get however, is: Uncaught ReferenceError: s238554 is not defined
I would appreciate any guidance that can be offered as I am a javascript novice.
Access your property by used[] on SKUinfo.s.Title like SKUinfo[s].Title
And also pass your property name within the quotes 's238554' as it's not variable.
Something like this.
function getSKU(s){
console.log(SKUinfo[s].Title);
}
getSKU('s238554'); // s238554 within quotes.

Facebook Open Graph not returning all results for Books.Read?

When using the Facebook Open Graph explorer, I run the following, to get a list of my books read.
/706685881/books.reads?limit=25&offset=0
This returns only 5 results, even though I have more books read than that, and I've explicitly requested up to 25 results. Following the paging link to the (theoretical) more results, shows a page with no more results.
here's my output of the query:
{
"data": [
{
"data": {
"book": {
"id": "452418224806835",
"url": "https://www.facebook.com/pages/Fantastic-Voyage/452418224806835",
"type": "books.book",
"title": "Fantastic Voyage"
}
},
"id": "10151871872740882"
},
{
"data": {
"book": {
"id": "106151566083577",
"url": "https://www.facebook.com/pages/The-Andromeda-Strain/106151566083577",
"type": "books.book",
"title": "The Andromeda Strain"
}
},
"id": "10151870390740882"
},
{
"data": {
"progress": [
{
"timestamp": 1388668020,
"percent_complete": 0
},
{
"timestamp": 1392949225,
"percent_complete": 100
}
],
"book": {
"id": "10150233066083545",
"url": "http://www.goodreads.com/book/show/10664113-a-dance-with-dragons",
"type": "good_reads:book",
"title": "A Dance with Dragons (A Song of Ice and Fire, #5)"
}
},
"id": "10151867499170882"
},
{
"data": {
"progress": [
{
"timestamp": 1391350736,
"percent_complete": 0
},
{
"timestamp": 1392569208,
"percent_complete": 100
}
],
"book": {
"id": "1390736374489290",
"url": "http://www.goodreads.com/book/show/18405477-city-of-the-sun",
"type": "good_reads:book",
"title": "City of the Sun"
}
},
"id": "10151860185710882"
},
{
"data": {
"book": {
"id": "526531387368738",
"url": "http://apps.facebook.com/bookscout/?currworkid=9000000000133742",
"type": "books.book",
"title": "Gates of Fire"
}
},
"id": "10151226827780882"
}
],
"paging": {
"next": "https://graph.facebook.com/706685881/books.reads?fields=data&limit=25&offset=25&__after_id=10151226827780882"
}
}
Anybody seeing what I am doing wrong?

Categories

Resources