Recently I've encountered a strange behaviour regarding elasticsearch with rails4/mongoid4/tire. I managed to do a temporary fix, but I want to know if there is a cleaner solution and where exactly the problem lies (is it an elasticsearch issue?)
Relevant part of my Gemfile
gem 'rails', '4.0.0'
gem "mongoid", github: 'mongoid/mongoid'
gem 'tire'
Elasticsearch version:
"version" : {
"number" : "0.90.2",
"snapshot_build" : false,
"lucene_version" : "4.3.1"
}
My model:
relevant part of my model consists of Ad class:
class Ad
include Mongoid::Document
field :title, type: String
[... other stuff...]
end
and Ad subclasses, one of which is:
class AdInAutomotiveAutomobile < Ad
field :make
field :model
field :body_type
tire.index_name 'ads'
[... other stuff ...]
end
using inheritance doesn't seem to have any importance, but I'm mentioning it just for the record
The problem
Inserting new Ad doesn't update the mapping of 'ads' index
{
"ads": {
"ad_in_automotive_automobile": {
"properties": {
"$oid": {
"type": "string"
}
}
}
}
}
Logs output, trimmed down:
# 2013-08-02 15:40:58:387 [ad_in_automotive_automobile/51fbb6b26f87e9ab1d000001] ("ads")
#
curl -X POST "http://localhost:9200/ads/ad_in_automotive_automobile/51fbb6b26f87e9ab1d000001" -d '{
"_id": {
"$oid": "51fbb6b26f87e9ab1d000001"
},
"active": null,
"body_type": "hatchback",
"c_at": "2013-08-02T13:40:57.647Z",
"category_id": {
"$oid": "51e8020c6f87e9b8e0000001"
},
"color": null,
"description": null,
"engine_displacement": null,
"expire_at": null,
"fuel_type": null,
"locale": null,
"make": "ford",
"meta": {},
"mileage": null,
"model": "focus",
"power": null,
"price": null,
"title": "foo",
"transmission": null,
"u_at": "2013-08-02T13:40:57.647Z",
"year": null,
"category_slug": "automotive-automobile"
}'
# 2013-08-02 15:40:58:388 [201]
#
#
{
"ok": true,
"_index": "ads",
"_type": "ad_in_automotive_automobile",
"_id": "51fbb6b26f87e9ab1d000001",
"_version": 1
}
The solution
Somehow, this:
"_id":{"$oid":"51fbb6b26f87e9ab1d000001"}
is stopping elasticsearch from updating the mapping
So I've 'fixed' this in #to_indexed_json method:
def to_indexed_json
to_json(methods: [:category_slug]).gsub( /\{\"\$oid\"\:(\".{24}\")\}/ ) { $1 }
end
Which results in:
# 2013-08-02 15:50:08:689 [ad_in_automotive_automobile/51fbb8fb6f87e9ab1d000002] ("ads")
#
curl -X POST "http://localhost:9200/ads/ad_in_automotive_automobile/51fbb8fb6f87e9ab1d000002" -d '{
"_id": "51fbb8fb6f87e9ab1d000002",
"active": null,
"body_type": "hatchback",
"c_at": "2013-08-02T13:50:08.593Z",
"category_id": "51e8020c6f87e9b8e0000001",
"color": null,
"description": null,
"engine_displacement": null,
"expire_at": null,
"fuel_type": null,
"locale": null,
"make": "ford",
"meta": {},
"mileage": null,
"model": "focus",
"power": null,
"price": null,
"title": "foo",
"transmission": null,
"u_at": "2013-08-02T13:50:08.593Z",
"year": null,
"category_slug": "automotive-automobile"
}'
# 2013-08-02 15:50:08:690 [201]
#
#
{
"ok": true,
"_index": "ads",
"_type": "ad_in_automotive_automobile",
"_id": "51fbb8fb6f87e9ab1d000002",
"_version": 1
}
And now the mapping is OK:
{
"ads": {
"ad_in_automotive_automobile": {
"properties": {
"$oid": {
"type": "string"
},
"body_type": {
"type": "string"
},
"c_at": {
"type": "date",
"format": "dateOptionalTime"
},
"category_id": {
"type": "string"
},
"category_slug": {
"type": "string"
},
"make": {
"type": "string"
},
"meta": {
"type": "object"
},
"model": {
"type": "string"
},
"title": {
"type": "string"
},
"u_at": {
"type": "date",
"format": "dateOptionalTime"
}
}
}
}
}
The question(s), once again
Why does it happen?
What part of stack is responsible for that?
Can it be fixed in cleaner way?
I'm the guy from the comment, it looks like this is fixed in tire HEAD, look at this issue https://github.com/karmi/tire/issues/775. I havent verified the fix since I monkey patched the class. This is the patch in case you want to go that way:
require "tire"
module Tire
class Index
def get_id_from_document(document)
case
when document.is_a?(Hash)
document[:_id] || document['_id'] || document[:id] || document['id']
when document.respond_to?(:id) && document.id != document.object_id
document.id.to_s # was document.id.as_json
end
end
end
end
Related
Can someone help me with a solution to update an array object inside the MongoDB document, I've tried a couple of methods but still it's to updating, here is my document that I want to update the array in the document.
{
"title": "Products",
"description": "test",
"image": "bdd8510d75f6e83ad308d5f306afccef_image.jpg",
"_created_at": "2021-06-07T20:51:08.316Z",
"ratingCount": 0,
"ratingTotal": 0,
"placeListSave": [
{
"objectId": "g70brr45pfi",
"name": "Kale",
"email": "null",
"strBrandLogo": "84de8865e3223d1ca61386355895aa04_image.jpg",
"storeNumber": "56",
"phone": "0815342119",
"createdAt": "2021-06-10T10:19:53.384Z",
"image": "ad1fb7602c2188223fd891a52373cb9d_image.jpg"
},
{
"objectId": "0qokn33p773",
"name": "Apple",
"email": null,
"strBrandLogo": null,
"storeNumber": "01",
"phone": "011 393 8600",
"createdAt": "2021-06-11T03:11:17.342Z",
"image": "8cfcbf2bcb5e3b4ea8ade44d3825bb52_image.jpg"
}
]
}
So I only want to update the apple object and change the data, I've tried the following code but doesn't seem to work.
`
var db = client.db("test");
try {
db.collection("ShoppingCentres").updateOne({
"title": req.body.product,
"placeListSave.objectId": req.body.id,
}, {
$set: {
"placeListSave.$.email": req.body.email,
"placeListSave.$.storeNumber": req.body.storeNumber,
"placeListSave.$.phone": req.body.phone,
"placeListSave.name": req.body.name,
},
});
res.json("client");
} catch (e) {
console.log("verify", e);
}
});`
arrayFilters seems suitable here:
db.collection.update({
"title": "Products",
"placeListSave.objectId": "0qokn33p773",
},
{
$set: {
"placeListSave.$[x].email": "test#some.email",
"placeListSave.$[x].storeNumber": "test",
"placeListSave.$[x].phone": "test",
"placeListSave.$[x].name": "test"
}
},
{
arrayFilters: [
{
"x.objectId": "0qokn33p773"
}
]
})
explained:
Add array filter called "x" with the objectId for the element that you need to update and use this filter in the $set stage to update the necessary elements.
Hint: To speed up the update you will need to add index on title field or compound index on title+placeListSave.objectId
playground
From this link:
https://developer.deutschebahn.com/store/apis/info?name=Fahrplan-Free&version=v1&provider=DBOpenData#!/default/get_departureBoard_id
I can successfully call the ARRIVALboard information:
[
{
"name": "ICE 1689",
"type": "ICE",
"boardId": null,
"stopId": 8000152,
"stopName": "Hannover Hbf",
"dateTime": "2021-01-19T00:00",
"origin": "Hamburg-Altona",
"track": "8",
"detailsId": "78642%2F27599%2F82706%2F15139%2F80%3fstation_evaId%3D8000152"
}
, which is complete.
However, when I call the DEPARTUREboard information I get everything apart from the 'destination' JSON field.
{
"name": "ICE 272",
"type": "ICE",
"boardId": null,
"stopId": 8000152,
"stopName": "Hannover Hbf",
"dateTime": "2021-01-19T00:05",
"track": "7",
"detailsId": "972312%2F330581%2F159824%2F244192%2F80%3fstation_evaId%3D8000152"
}
, i.e., the 'destination' field is missing according to the Model schema
I guess this is a user error but I can't work out how to fix this!
I practice to use json-rpc to create test case, and I want to assoicate a test paln with test case, but I don't know the parameter of the plan.
Can anyone give me some suggestions?? Thanks.
My example like this
Test plan ID : 3
Test plan name: test
Using postman request
{
"jsonrpc":"2.0",
"method":"TestCase.create",
"params":{"values":{"summary":"jsonrpctest","case_status":2,"category":2,"priority":1,"text":"20201005test","plan":[3,"test"]}},
"id":1
}
Response
{
"id": 1,
"jsonrpc": "2.0",
"result": {
"id": 191,
"create_date": "2020-10-06 04:44:13",
"is_automated": false,
"script": "",
"arguments": "",
"extra_link": null,
"summary": "jsonrpctest",
"requirement": null,
"notes": "",
"text": "20201005test",
"case_status_id": 2,
"case_status": "CONFIRMED",
"category_id": 2,
"category": "--default--",
"priority_id": 1,
"priority": "P1",
"author_id": 1,
"author": "ardyn",
"default_tester_id": null,
"default_tester": null,
"reviewer_id": null,
"reviewer": null,
"plan": [],
"component": [],
"tag": []
}
}
https://kiwitcms.readthedocs.io/en/latest/api/index.html says
"Server side RPC methods are documented in tcms.rpc.api."
Which is
https://kiwitcms.readthedocs.io/en/latest/modules/tcms.rpc.api.html
And there is the TestPlan.add_case() method:
https://kiwitcms.readthedocs.io/en/latest/modules/tcms.rpc.api.testplan.html#tcms.rpc.api.testplan.add_case
How can I get the data out of this array stored in a variant column in Snowflake. I don't care if it's a new table, a view or a query. There is a second column of type varchar(256) that contains a unique ID.
If you can just help me read the "confirmed" data and the "editorIds" data I can probably take it from there. Many thanks!
Output example would be
UniqueID ConfirmationID EditorID
u3kd9 xxxx-436a-a2d7 nupd
u3kd9 xxxx-436a-a2d7 9l34c
R3nDo xxxx-436a-a3e4 5rnj
yP48a xxxx-436a-a477 jTpz8
yP48a xxxx-436a-a477 nupd
[
{
"confirmed": {
"Confirmation": "Entry ID=xxxx-436a-a2d7-3525158332f0: Confirmed order submitted.",
"ConfirmationID": "xxxx-436a-a2d7-3525158332f0",
"ConfirmedOrders": 1,
"Received": "8/29/2019 4:31:11 PM Central Time"
},
"editorIds": [
"xxsJYgWDENLoX",
"JR9bWcGwbaymm3a8v",
"JxncJrdpeFJeWsTbT"
] ,
"id": "xxxxx5AvGgeSHy8Ms6Ytyc-1",
"messages": [],
"orderJson": {
"EntryID": "xxxxx5AvGgeSHy8Ms6Ytyc-1",
"Orders": [
{
"DropShipFlag": 1,
"FromAddressValue": 1,
"OrderAttributes": [
{
"AttributeUID": 548
},
{
"AttributeUID": 553
},
{
"AttributeUID": 2418
}
],
"OrderItems": [
{
"EditorId": "aC3f5HsJYgWDENLoX",
"ItemAssets": [
{
"AssetPath": "https://xxxx573043eac521.png",
"DP2NodeID": "10000",
"ImageHash": "000000000000000FFFFFFFFFFFFFFFFF",
"ImageRotation": 0,
"OffsetX": 50,
"OffsetY": 50,
"PrintedFileName": "aC3f5HsJYgWDENLoX-10000",
"X": 50,
"Y": 52.03909266409266,
"ZoomX": 100,
"ZoomY": 93.75
}
],
"ItemAttributes": [
{
"AttributeUID": 2105
},
{
"AttributeUID": 125
}
],
"ItemBookAttribute": null,
"ProductUID": 52,
"Quantity": 1
}
],
"SendNotificationEmailToAccount": true,
"SequenceNumber": 1,
"ShipToAddress": {
"Addr1": "Addr1",
"Addr2": "0",
"City": "City",
"Country": "US",
"Name": "Name",
"State": "ST",
"Zip": "00000"
}
}
]
},
"orderNumber": null,
"status": "order_placed",
"submitted": {
"Account": "350000",
"ConfirmationID": "xxxxx-436a-a2d7-3525158332f0",
"EntryID": "xxxxx-5AvGgeSHy8Ms6Ytyc-1",
"Key": "D83590AFF0CC0000B54B",
"NumberOfOrders": 1,
"Orders": [
{
"LineItems": [],
"Note": "",
"Products": [
{
"Price": "00.30",
"ProductDescription": "xxxxxint 8x10",
"Quantity": 1
},
{
"Price": "00.40",
"ProductDescription": "xxxxxut Black 8x10",
"Quantity": 1
},
{
"Price": "00.50",
"ProductDescription": "xxxxx"
},
{
"Price": "00.50",
"ProductDescription": "xxxscount",
"Quantity": 1
}
],
"SequenceNumber": "1",
"SubTotal": "00.70",
"Tax": "1.01",
"Total": "00.71"
}
],
"Received": "8/29/2019 4:31:10 PM Central Time"
},
"tracking": null,
"updatedOn": 1.598736670503000e+12
}
]
So, this is how I'd query that exact JSON assuming the data is in column var in table x:
SELECT x.var[0]:confirmed:ConfirmationID::varchar as ConfirmationID,
f.value::varchar as EditorID
FROM x,
LATERAL FLATTEN(input => var[0]:editorIds) f
;
Since your sample output doesn't match the JSON that you provided, I will assume that this is what you need.
Also, as a note, your JSON includes outer [ ] which indicates that the entire JSON string is inside an array. This is the reason for var[0] in my query. If you have multiple records inside that array, then you should remove that. In general, you should exclude those and instead load each record into the table separately. I wasn't sure whether you could make that change, so I just wanted to make note.
Given my Profile data looks like below, I want to find the profile for combination of userName and productId
and only return the profile with the respective contract for this product.
{
"firstName": "John",
"lastName": "Doe",
"userName": "john.doe#gmail.com",
"language": "NL",
"timeZone": "Europe/Amsterdam",
"contracts": [
{
"contractId": "DEMO1-CONTRACT",
"productId": "ticket-api",
"startDate": ISODate('2016-06-29T09:06:42.391Z'),
"roles": [
{
"name": "Manager",
"permissions": [
{
"activity": "ticket",
"permission": "createTicket"
},
{
"activity": "ticket",
"permission": "updateTicket"
},
{
"activity": "ticket",
"permission": "closeTicket"
}
]
}
]
},
{
"contractId": "DEMO2-CONTRACT",
"productId": "comment-api",
"startDate": ISODate('2016-06-29T10:27:45.899Z'),
"roles": [
{
"name": "Manager",
"permissions": [
{
"activity": "comment",
"permission": "createComment"
},
{
"activity": "comment",
"permission": "updateComment"
},
{
"activity": "comment",
"permission": "deleteComment"
}
]
}
]
}
]
}
I managed to find the solution how to do this from the command line. But I don't seem to find a way how to accomplish this with Morphia (latest version).
db.Profile.aggregate([
{ $match: {"userName": "john.doe#gmail.com"}},
{ $project: {
contracts: {$filter: {
input: '$contracts',
as: 'contract',
cond: {$eq: ['$$contract.productId', "ticket-api"]}
}}
}}
])
This is what I have so far. Any help is most appreciated
Query<Profile> matchQuery = getDatastore().createQuery(Profile.class).field(Profile._userName).equal(userName);
getDatastore()
.createAggregation(Profile.class)
.match(matchQuery)
.project(Projection.expression(??))
Note... meanwhile I found another solution which does not use an aggregation pipeline.
public Optional<Profile> findByUserNameAndContractQuery(String userName, String productId) {
DBObject contractQuery = BasicDBObjectBuilder.start(Contract._productId, productId).get();
Query<Profile> query =
getDatastore()
.createQuery(Profile.class)
.field(Profile._userName).equal(userName)
.filter(Profile._contracts + " elem", contractQuery)
.retrievedFields(true, Profile._contracts + ".$");
return Optional.ofNullable(query.get());
}
I finally found the best way (under assumption I only want to return max. 1 element from array) to filter embedded array.
db.Profile.aggregate([
{ $match: {"userName": "john.doe#gmail.com"}},
{ $unwind: "$contracts"},
{ $match: {"contracts.productId": "comment-api"}}
])
To match according to your first design you could try the projection settings with morphia aggregation pipeline.
Query<Profile> matchQuery = getDatastore().createQuery(Profile.class).field(Profile._userName).equal(userName);
getDatastore()
.createAggregation(Profile.class)
.match(matchQuery)
.project(Projection.expression("$filter", new BasicDBObject()
.append("input", "$contracts")
.append("as", "contract")
.append("cond", new BasicDBObject()
.append("$eq", Arrays.asList('$$contract.productId', "ticket-api")));
Also see the example written by the morphia crew around line 88 at https://github.com/mongodb/morphia/blob/master/morphia/src/test/java/org/mongodb/morphia/aggregation/AggregationTest.java.