JsonPath expression to filter using regex - regex

We are using a tool which uses jayway library for evaluating JSONpath expression. Javascript does NOT seem to work with it. How can I use regular expression in the JSONPath in such a case. For instance, in the below example I would like to filter all book titles whose title has the word "Sword" in it:
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}

The Jayway implementation uses the Ruby regex operator:
$.store.book[?(#.title =~ /^.*Sword.*$/)]
To ignore case:
$.store.book[?(#.title =~ /^.*sword.*$/i)]

For the record, a workaround for conditional regex in Goessner's javascript JSONpath would be to write the query as follow:
$.store.book[?(/^.*sword.*$/i.test(#.title))]
Please see here
https://github.com/jpaquit/jsonpath/tree/0.8.5-+-regexp for "=~" syntax in JS lib.

You could use capturing group or lookbehind assertion.
"title":\s*"([^"]*\bSword\b[^"]*)"
Add case-insensitive modifier i if necessary. Grab the title string from group index 1.
DEMO

Related

Preg Match GF to pull data from API CALL (Podio CRM)

I am trying to accomplish pulling all of the data that populates from this API CALL made within my CRM Podio...
The API call response is the following:
{
"status": {
"version": "1.0.0",
"code": 0,
"msg": "SuccessWithResult",
"total": 1,
"page": 1,
"pagesize": 10,
"transactionID": "ba31a62303e76d49b2063e94e2972bc6"
},
"property": [
{
"identifier": {
"Id": 34476108,
"fips": "48201",
"apn": "1288930010042",
"attomId": 34476108
},
"lot": {
"lotnum": "42",
"lotsize1": 0.2735078,
"lotsize2": 11914,
"poolind": "YES"
},
"area": {
"blockNum": "1",
"loctype": "VIEW - NONE",
"countrysecsubd": "Harris",
"countyuse1": "1001 ",
"muncode": "HA",
"munname": "HARRIS",
"subdname": "BLACKHORSE RANCH SOUTH SEC 6",
"taxcodearea": "40"
"legal1": "BLACK HORSE RANCE LOT 14 BLOCK 12 USA"
etc.
I have tried the following code to pull just the legal description but it returns the entire API response in the comments of my crm. I am trying to get all data points listed individually.
preg_match_gf("/legal1\.\:\s\/(.*)/ism",[(Variable) PropertyDetails], 1)
Any advise or insight is much appreciated!!
Thank you,
Cody

DynamoDB LIKE '%' (contains) search over an array of objects using a key from the object, NodeJS

I am trying to use a "LIKE" search on DynamoDB where I have an array of objects using nodejs.
Looking through the documentation and other related posts I have seen this can be done using the CONTAINS parameter.
My question is - Can I run a scan or query over all of my items in DynamoDB where a value in my object is LIKE "Test 2".
Here is my DynamoDB Table
This is how it looks as JSON:
{
"items": [
{
"description": "Test 1 Description",
"id": "86f550e3-3dee-4fea-84e9-30df174f27ea",
"image": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/86f550e3-3dee-4fea-84e9-30df174f27ea.jpg",
"live": 1,
"status": "new",
"title": "Test 1 Title"
},
{
"description": "Test 2 Description",
"id": "e17dbb45-63da-4567-941c-bb7e31476f6a",
"image": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/e17dbb45-63da-4567-941c-bb7e31476f6a.jpg",
"live": 1,
"status": "new",
"title": "Test 2 Title"
},
{
"description": "Test 3 Description",
"id": "14ad228f-0939-4ed4-aa7b-66ceef862301",
"image": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/14ad228f-0939-4ed4-aa7b-66ceef862301.jpg",
"live": 1,
"status": "new",
"title": "Test 3 Title"
}
],
"userId": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
}
I am trying to perform a scan / query which will look over ALL users (every row) and look at ALL items and return ALL instances where description is LIKE "Test 2".
I have tried variations of scans as per the below:
{
"TableName": "my-table",
"ConsistentRead": false,
"ExpressionAttributeNames": {
"#items": "items",
},
"FilterExpression": "contains (#items, :itemVal)",
"ExpressionAttributeValues": {
":itemVal":
{
"M": {
"description": {
"S": "Test 2 Description"
},
"id": {
"S": "e17dbb45-63da-4567-941c-bb7e31476f6a"
},
"image": {
"S": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/e17dbb45-63da-4567-941c-bb7e31476f6a.jpg"
},
"live": {
"N": "1"
},
"status": {
"S": "new"
},
"title": {
"S": "Test 2 Title"
}
}
}
}
}
The above scan works but as you can see I am passing in the whole object as an ExpressionAttributeValues, what I want to do is just pass in the description for example something like the below (which doesnt work and returns no items found).
{
"TableName": "my-table",
"ConsistentRead": false,
"ExpressionAttributeNames": {
"#items": "items.description",
},
"FilterExpression": "contains (#items, :itemVal)",
"ExpressionAttributeValues": {
":itemVal":
{
"S": "Test 2"
}
}
}
Alternatively, would it be better to create a separate table where all the items are added and they are linked via the userId? I was always under the impression there should be one table per application but in this instance I think if I had all the item data at the top level, scanning it would be a lot safer and faster.
So with nearly 200 views since posting and no responses I have come up with a solution that does not immediately solve the initial problem (I honestly do not think it can be solved) but have come up with an alternative approach.
Firstly I do not want two tables as this seems overkill, and I do not want the aws costs associated with two tables.
This has lead me to restructure the primary keys with prefixes which I can search over using the "BEGINS_WITH" dynamodb selector query.
Users will be added as U_{USER_ID} and items will be added as I_{USER_ID}_{ITEM_ID}, this way I only have one table to manage and pay for and this allows me to run BEGINS_WITH "U_" to get a list of users or "I_" to get a list of items.
I will then flatten the item data as strings so I can run "contains" searches on any of the item data. This also allows me to run a "contains {USER_ID}" search on the primary keys for items so I can get a list of items for a particular user.
Hope this helps anyone who might come up against the same issue.

Django JSONField and searching through the list of dictionaries using ILIKE

Is it possible to search against one key value in the list of dictionaries using ILIKE (icontains) operator? My json field looks like this:
object = MyModel()
object.json_data = [
{
"type": 1,
"results": [
{
"score": 1,
"comment": "Some text comment 1",
},
{
"score": 2,
"comment": "Some text comment 2",
},
{
"score": 3,
"comment": "Some text comment 3",
}
]
},
{
"type": 2,
"results": [
{
"score": 4,
"comment": "Some text comment 4",
},
{
"score": 5,
"comment": "Some text comment 5",
},
{
"score": 6,
"comment": "Some text comment 6",
}
]
}
]
object.save()
And now, how to write the query to search in a "comment" key?
MyModel.objects.filter(json_data__??__results__??__comment__icontains="text comment")
I'm using Django 1.9.
Thanks!
this works for me (note the [])
query = User.objects.filter(data__campaigns__contains=[{'key': 'value'}])
You should be able to search simply by chaining it, django style:
MyModel.objects.filter(json_data__results__contains={"comment":"text comment"})
check out the documentation for JSON field in Django 1.9:
https://docs.djangoproject.com/es/1.9/ref/contrib/postgres/fields/#querying-jsonfield
which includes contains lookup:
https://docs.djangoproject.com/es/1.9/ref/contrib/postgres/fields/#std:fieldlookup-hstorefield.contains
If this doesn't work for case-insensitive, then I would see what query it produces, and simply rework it with extra where:
MyModel.objects.extra(where=["json_data->>'results'->'comment' ILIKE %s"], params=["%text comment%"])
or you can use the specific symbols for json as stated in postgres documentation, like <#
http://www.postgresql.org/docs/9.5/static/functions-json.html

How to analyze an HTML text with compound words

I'm writing a search service based on Elasticsearch for a bunch of sites with content written in agglutinated languages like Swedish, German and Finnish.
I know that Elasticsearch offers language analyzers by default but after some testing I found their support sloppy at best.
What I got so far is:
{
"settings":{
"analysis":{
"filter":{
"swedish_stop":{
"type": "stop",
"stopwords": "_swedish_"
},
"swedish_stemmer":{
"type":"stemmer",
"language":"swedish"
},
"swedish_words":{
"type":"dictionary_decompounder",
"word_list":["very", "long", "list", "of", "words", "almost", "13", "MB"]
}
},
"analyzer":{
"custom_swedish":{
"tokenizer": "standard",
"filter":[
"lowercase",
"swedish_stop",
"swedish_stemmer",
"swedish_words"
],
"char_filter":[
"html_strip"
]
}
}
}
}
}
Do you guys have a clue?

About Graph API of Facebook Places

i can not see good documentation about Facebook Places, please tell me if you know something about it.
https://graph.facebook.com/search?q=coffee&type=place&center=37.76,122.427&distance=1000
1) In above url what is the unit of distance? (Meter, KM, Miles or something else?)
2) What is the actual meaning of distance.. is it search result comes within this range or it starts search within this range and goes beyond for more results?
3) How can we restrict search result to any specific city or country?
4) What we can do more with this API?
According to https://developers.facebook.com/docs/android/scrumptious/show-nearby-places/ the unit of distance is meters.
edit - FB keeps moving the information around their documentation, so it may not be there when you look. Search for 'places meters' and you should find the citation: https://developers.facebook.com/search/?q=places%20meters
The search excludes results outside of a circle with the radius of your specific distance.
Use the location field of the places returned by your search to filter out any unwanted places. For example, if you had these results, you could use the city filter to include only Brooklyn (or New York).
[{
"name": "Bembe",
"location": {
"street": "81 S 6th St.",
"city": "Brooklyn",
"state": "NY",
"country": "United States",
"zip": "11222",
"latitude": 40.710978587859,
"longitude": -73.965404723282
},
"id": "146207358735488"
},
{
"name": "Manhattan Bridge Orthodontics",
"location": {
"street": "145 Canal St, 2nd Floor",
"city": "New York",
"state": "NY",
"country": "United States",
"zip": "10002-5033",
"latitude": 40.709716414644,
"longitude": -73.988593034059
},
"id": "121071754616533"
}]
4.Limited only by your imagination.