How to create index on single property using neo4j ruby - ruby-on-rails-3

I am trying to create index on ActiveNode properties. when i tried with the code which is specified below , index is creating but it is throwing some warning like "The constraint option is no longer supported (Defined on Neo4j for permalink)" . How to create index on property without warning and error on nodes ?
class Neo4j
include Neo4j::ActiveNode
property :permalink, type: String, index: :exact
end
And whenever i create a active node it should be automatically created after node creation , i am not appreciating functions with queries .
I am using "neo4j-core" gem on ruby on rails and version , edition is below:
{
"edition": "community",
"version": "3.4.1"
}

Related

ArangoDB document nested property naming convention and indexing limitations

Problem: indexing nested property in arangodb
Context: ArangoDB, community edition, running localy
Description:
I have a collection CONFIGURATIONS_NODES hosting documents with the following structure
{
"_sectionName": "MySection",
"_configurationName": "abc",
"nodeData": {
"_id": "61cc20793b83b2001c24a9ad",
"configuration_name": "xyz"
}
If I run a query
"for v in CONFIGURATIONS_NODES filter v.nodeData._id=="61cc20793b83b2001c24a9ad" return v". It finds the document as expected but alas its a full scan.
If I try to create an index on the nested nodeData._id property the index creation fails. I was able to create an index on the nested property nodeData.configuration_name. so it seems the issue relates only to the nested _id
The documentation states:
You cannot use the _id system attribute in user-defined indexes, but indexing _key, _rev, _from, and _to is possible.
The name _id conflicts with the internal _id attribute and is treated specially. Even though it is not mentioned in the documentation, this means that attributes named _id cannot be indexed, not even in nested subobjects.

Add a field to a document in raven 3.5 where the property doesn't exist

In my instance of raven3.5 I have a collection and some documents have an extra property due to a change in the document structure. Now I need to query on that property but many older documents don't have it.
How can I patch the collection and add the property to documents that don't have it? Or create a query that will get the documents that don't have the specific property?
Thanks.
The below is what I know works for RavenDB Server 4.2 and up:
Option 1 - using static index
Create a static index ('MyIndex') with the following map method:
from i in docs.Items
select new {
Name = i.Name
}
Then query the index as follows:
Return all 'Items' that do not have 'Name':
from index 'MyIndex'
where true and not exists(Name)
Option 2 - using auto-index:
First, run the following query which will 'create' the auto-index for you:
from Items
where exists(Name)
After that, you can query the following to get results:
from Items
where true and not exists(Name)
=================================================
A patch script is made up of the query part and the update part
So use the following to add the missing property to docs that don't have it:
from Items
where true and not exists(Name)
update {
this.Name = "value"
}

Neo4j Spatial index error duplicates

I'm having an error when inserting in Neo4j a spatial object with Spring Data:
Caused by: java.lang.RuntimeException: Error adding element 20 wkt POINT(-0.131483 51.513861) to index LocationIndex
at org.neo4j.rest.graphdb.ExecutingRestAPI.addToIndex(ExecutingRestAPI.java:470)
at org.neo4j.rest.graphdb.RestAPIFacade.addToIndex(RestAPIFacade.java:168)
at org.neo4j.rest.graphdb.index.RestIndex.add(RestIndex.java:60)
That's basically because there was a different entry before (that doesn't exist anymore, with the same location). The uniqueness of the constraint means two different objects cannot have the same location, but if you delete the previous one, it seems to remain in the index and collides.
Is there any way of re-indexing the spatial index in neo4j? That means, delete everything and re-index only the existing data.
This is my class field in the Spring Data entity class:
#Indexed(indexType = IndexType.POINT, indexName = "LocationIndex")
private String wkt;
Should I add something to delete from the index when the object is deleted? Or it must be done manually, how?
EDIT
{
"message": "GeometryNode not indexed with an RTree: 21",
"exception": "RuntimeException",
"fullname": "java.lang.RuntimeException",
"stacktrace": [
"org.neo4j.gis.spatial.rtree.RTreeIndex.findLeafContainingGeometryNode(RTreeIndex.java:794)",
"org.neo4j.gis.spatial.rtree.RTreeIndex.remove(RTreeIndex.java:111)",
"org.neo4j.gis.spatial.rtree.RTreeIndex.remove(RTreeIndex.java:100)",
"org.neo4j.gis.spatial.EditableLayerImpl.update(EditableLayerImpl.java:56)",
"org.neo4j.gis.spatial.indexprovider.LayerNodeIndex.add(LayerNodeIndex.java:143)",
"org.neo4j.gis.spatial.indexprovider.LayerNodeIndex.add(LayerNodeIndex.java:41)",
"org.neo4j.server.rest.web.DatabaseActions.addToNodeIndex(DatabaseActions.java:686)",
"org.neo4j.server.rest.web.RestfulGraphDatabase.addToNodeIndex(RestfulGraphDatabase.java:1022)",
"java.lang.reflect.Method.invoke(Method.java:606)",
"org.neo4j.server.rest.transactional.TransactionalRequestDispatcher.dispatch(TransactionalRequestDispatcher.java:139)",
"java.lang.Thread.run(Thread.java:745)"
]
}
Found that he is trying to acquire from DB the Index node to do something. The ID of the Node I'm trying to update in this case is 20, see that he is trying to get 21.
That's because of this (found in neo4j github):
private long extractNodeId( String uri ) throws BadInputException
{
try
{
return Long.parseLong( uri.substring( uri.lastIndexOf( "/" ) + 1 ) );
}
catch ( NumberFormatException | NullPointerException ex )
{
throw new BadInputException( ex );
}
}
I don't understand how this is suppose to work, because in my Database I've seen nodes with ID different than OriginalNode + 1 which point correctly to the OriginalNode's ID and spatial finds them.
Is it only a matter of update? If I create OriginalNode with wkt it creates both nodes all right, but this error only shows up when I'm trying to add wkt information to an existing node.
Thanks!

Using Mongo Update and $set to insert field (throwing error)

Ok, this is probably a stupid question but I have been reading and trying different queries and for some reason I cannot get this to work without throwing an error. This is my first time working with MongoDB and it is in an RoR project. We set up charities to have a twitter handle field, but it was not put into the model originally. So we populated the DB with charities, but now none of them have the twitter handle field. I added it to the model so now all others created will have it.
My issue is when I try to update the charities already in my DB I keep getting an error pointing at $set:
namespace :add_tw_handles_fields_2013_6_13 do
desc "add_tw_handle"
task :add_tw_handle => :environment do |t, args|
# db.charity.update( { featured: false }, { $set: { tw_handle : "test"}}, false, true)
# got your 6
Charity.update({ },
{
$set: { "tw_handle": "test"}
},
{ multi: true }
})
end
end
I tried the 2 synax calls above, I was reading in these 2 docs http://docs.mongodb.org/manual/reference/method/db.collection.update/ http://docs.mongodb.org/manual/core/update/#Updating-The%24positionaloperator.
I always get this error tho:
add_tw_handles_fields_2013_6_13.rake:16: syntax error, unexpected ':', expecting tASSOC
$set: {
As far as I can tell that is the correct syntax. I am running this in the script so I don't think I need the db. before my Model name (as shown in the uncommented update) right? I am new to this, but I literally copied and pasted the example and filled out my info, and nothing. I then tried adding a query, but there is never an error until it gets to $set: and I have no idea why. It is exactly as is shown in the Mongo docs linked above.
Any insight into what my issue would be greatly appreciated.
Thanks,
Alan
The error you're getting is from Ruby, not MongoDB, because you're trying to use MongoDB's JSON syntax inside of Ruby, which Ruby does not like. :) Your update query looks fine but you need to translate it to Ruby syntax which is a bit different.
coll.update( { }, { "$set" => { "tw_handle" => "test" } } );
will work assuming coll is your Collection object.
See here for a good tutorial (written by the MongoDB Ruby team) on using the Ruby driver.
Ok so after looking around and talking to the other dev on this project this is what I have found:
Inside a rails script you want to access the objects through rails not the mongoDB directly:
Following example is for running a script from within lib/tasks
Uses Rails activerecord to update the entry through the framework
m = ModelName.find("51b610972f52760fcc003331")
m.update_attributes( :attribute_name => "what you want to assign" )
m.save
Finds the object that has the id given from your model in rails (accesses mongo db directly)
object = ModelName.find({
"$in" => {
"_id" => "51b610972f52760fcc003331"
}
})
object.first.update_attributes(:attribute_name => "what you want to assign")
From within the console
Within the rails console you can use the first segments syntax using the activerecord models to access the objects you are querying for. But if you want to go directly into the mongo console the syntax is slightly different then above.
after going to the root directory of your project launch mongo in the console and find an object from one of your collections based on its id:
>mongo
>show dbs
>use dbsname
>show collections
>db.collection_name.find({ _id: { $in: [ ObjectId("51b610972f52760fcc003331") ] }})
Hopefully this is helpful to others just learning, syntaxs are different in each. I was told that mongo console runs with javascript, where the rails console (and within the project) is using the activerecord to run the call then manipulate the MongoDB. The commenter above was only addressing accessing the mongoDB directly from within a script. So hopefully this is a little more complete in the different ways you could run into this issue.

How do I install and setup the RavenDb index replication

rI've looked at the questions and indeed the RavenDb docs. There's a little at RavenDb Index Replication Docs but there doesn't seem any guidance on how/when/where to create the IndexReplicationDestination
Our use case is very simple (it's a spike). We currently create new objects (Cows) and store them in Raven. We have a couple of queries created dynamically using LINQ (e.g. from c in session.Query<Cows> select c).
Now I can't see where I should define the index to replicate. Any ideas? I've got hold of the bundle and added it to the server directory (I'm assuming it should be in RavenDB.1.0.499\server\Plugins where RavenDB.1.0.499\server contains Raven.Server.exe)
Edit: Thanks Ayende... the answer below and in the ravendb groups helped. There was a facepalm moment. Regardless here's some detail that may help someone else. It really is very easy and indeed 'just works':
a) Ensure that the plugins are being picked up. You can view these in the statistics - available via the /localhost:8080/stats url (assuming default settings). You should see entries in 'Extensions' regarding to the IndexReplication bundle.
If not present ensure the versions of the DLLs (bundle and server) are the same
b) Ensure the index you want to replicate has been created. They can be created via Client API or HTTP API.
Client API:
public class Cows_List : AbstractIndexCreationTask<Cow>
{
public Cows_List()
{
Map = cows => from c in cows select new { c.Status };
Index( x => x.Status, FieldIndexing.Analyzed);
}
}
HTTP API (in studio):
//Cows/List
docs.Cows
.Select(q => new {Status = q.Status})
c) create the replication document. The clue here is DOCUMENT. Like everything stored, it too is a document. So after creating it must be stored in the Db :
var replicationDocument = new Raven.Bundles.IndexReplication.Data.IndexReplicationDestination
{
Id = "Raven/IndexReplication/Cows_List", ColumnsMapping = { {"Status", "Status"} },
ConnectionStringName = "Reports", PrimaryKeyColumnName = "Id",
TableName = "cowSummaries"
};
session.Store(replicationDocument);
sesson.SaveChanges();
d) Ensure you have the following in the CLIENT (e.g. MVC app or Console)
e) Create the RDBMS schema. I have a table in 'cowReports' :
CREATE TABLE [dbo].[cowSummaries](
[Id] nvarchar NULL,
[Status] nchar NULL)
My particular problem was not adding the index document to the store. I know. facepalm. Of course everything is a document. Works like a charm!
You need to define two things.
a) An index that transform the document to the row shape.
b) A document that tell RavenDB what is the connection string name, the table name, and the columns to map