I'm moving from multiple cores to a single core with nested documents.(reason being matching/scoring from multiple cores is limited)
To achieve this I'm trying to index a nested structure using solrJ.
I've tested the following code but I get an error "BookDetail cannot have more than one Field with child=true"
How can I avoid this? Is this a solrj limitation?
Indexing is done as followed:
- Solr and solrj version 5.3.1
HttpSolrClient mytestcore=new HttpSolrClient("...");
mytestcore.add(dob.toSolrInputDocument(new Book());//should have some initialization
The structure is below(but most unused fields are removed).
public class Book implements Serializable {
#Field
private String id;
#Field
private String type;
#Field(child = true)
private List<BookDetail> details;
...
}
public class BookDetail implements Serializable {
#Field
private String id;
#Field
private String type;
#Field(child = true)
private List<BookMetaData> bookMetaData;
#Field(child = true)
private List<BookContent> pages;
...
}
public class BookMetaData implements Serializable {
#Field
private String id;
#Field
private String type;
...
}
public class BookContent implements Serializable {
#Field
private String id;
#Field
private String type;
#Field
private String content;
...
}
Edit:
Currently solved it by making seperate SolrInputDocuments for each document type and add them with addChildDocument.(as seen on other answers on stackoverflow) But this solution doesn't use the annotation "child = true" anymore...
Related
I want have two entities
#Entity
public class User {
#Index private String email;
#Index private String name;
#Index private String age;
}
#Entity
public class poll {
#Index private String pollid;
#Index private String answer;
#Index private Ref<User> user;
}
Now if I want to query poll and filter by email I m getting empty. Is it possible?
ofy().load().type(Poll.class).filter("email", email).list();
You can not do "indirect" queries on GAE (e.g. A JOIN type queries). Basically, your "poll" entity does not have the "email" field.
I have a Java model similar to:
public class Country {
#Id private String id;
private CurrencyId currencyId;
private List<Province> provinceList;
...
}
public class Province {
#Id private String id;
private Gobernor gobernorId;
private List<City> cityList;
...
}
public class City {
#Id private String id;
private String name;
...
}
I want to store that data using objectify. However, as Country data might change, I also want to store the date the Country data has been stored, so I think I should store an entity such as:
public class CountryListEntity {
#Id private String id;
private List<Country> countryList;
private Date storeDate;
}
Note I will only have one entity of kind CountryListEntity with the Id "root", if I can store it like that. I know very little about both how google apps stores data and how objectify works. I've tried many combinations of #Embedded, but I got many errors, i.e.
Cannot place array or collection properties inside #Embedded arrays or collections
Can anyone tell me how to define these classes? A snippet of the code needed to store and retrieve this "root" entity, would be highly appreciated!
#Embedded collections are transformed into a series of collection fields in the
low-level Entity. That's why one level embedding is all you can do.
If you are going to store/load all data at once and if your entities are as simple as the ones in your example you can put #Serialized annotation for your lists inside #Embedded lists.
You can find out more from this discussion.
The problem with this approach is that your low-level embeddings won't be able to be indexed.
public class CountryListEntity {
#Id private String id;
#Embedded
private List<Country> countryList;
private Date storeDate;
}
public class Country implements Serializable {
private String id;
private CurrencyId currencyId;
#Serialized
private List<Province> provinceList;
// ...
}
public class Province implements Serializable {
private String id;
private Gobernor gobernorId;
#Serialized
private List<City> cityList;
// ...
}
public class City implements Serializable {
private String id;
private String name;
// ...
}
I am not sure if I use the #Id in objectify the right way.
Right now I am using the eMail-Address as #Id field. The email field will be set on the server-side only (OAuthService.getCurrentUser.getEmail)
First question: Is this a good idea?
If I create for example an Item-class which has RegistrationTO as it's parent does it make sense to use the email-address as the #Id field in my Item-class or should Item-class have it's own, auto-generated, id and Key parent to specify the relation?
Objectify-Tutorial recommends to avoid #Parent - so, here I think it's not necessary either.
I am right?
Here my RegistrationTO:
public class RegistrationTO implements Serializable {
private static final long serialVersionUID = 1L;
#NotNull
#Size(min = 5, max = 20)
private String firstname;
#NotNull
#Size(min = 5, max = 20)
private String name;
#NotNull
#Size(min = 5, max = 20)
private String country;
#Id
#NotNull
#Size(min = 5, max = 20)
#Pattern(regexp = "\b[A-Z0-9._%-]+#[A-Z0-9.-]+\\.[A-Z]{2,4}\b")
private String email;
public RegistrationTO() {
}
public RegistrationTO(final String firstname, final String name, final String company) {
this.firstname = firstname;
this.name = name;
this.country = country;
email = "will be set on server (Oauth)";
}
public String getFirstname() {
return firstname;
}
public String getName() {
return name;
}
public String getCountry() {
return country;
}
public String getEmail() {
return email;
}
public void setEmail(final String email) {
this.email = email;
}
}
Sample for Item class:
public class Item implements Serializable {
private static final long serialVersionUID = 1L;
#Id
Long id
//or
//#Id
//String email
Key<RegistrationTO> parent;
String itemno;
}
Thank you in advance!
Regarding your question if the use of e-mail as #Id is correct or not, since the email will uniquely identify each object of the class, then you are fine!
Now, regarding the #Id of your Item class, if the email uniquely identifies each object, then there is no need to create a new auto-generated Long as #Id. In general, the criterion for the selection of the #Id is to uniquely identify all the objects of the class.
For the relationship between RegistrationTO and Item classes, use the #Parent annotation only if you need these entities to be the same entity group. The code for this:
#Parent
Key<RegistrationTO> parent;
Otherwise, use a "plain" relationship (as you have it in your example) that allows RegistrationTO and Item entities to be stored in different entity groups in the GAE datastore. For more information about entity groups, take a look at:
http://code.google.com/appengine/docs/java/datastore/entities.html#Entity_Groups_and_Ancestor_Paths
Hope that helps!
I have the following code:
#PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
public class A {
#Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
#PrimaryKey
private Key key;
#Persistent
private B b;
#Persistent
private int id;
// ...
}
#PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
public class B {
#Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
#PrimaryKey
private Key key;
#Persistent
private int id;
// ...
}
Now what I need to be able to do, is retrieve an instance of B, and refer to it from an instance of A like this:
B b = DAL.getBById(1);
A a = new A();
a.setB(b);
When I pass a to the makePersistent() method of the PersistenceManager, two things that I don't need happen:
1) a new instance of B is created
2) the reference A makes to b is null
Could someone tell me what I am doing wrong?
Thanks!
A field value can contain an instance of a Serializable class, storing the serialized value of the instance in a single property value of the type Blob. To tell JDO to serialize the value, the field uses the annotation #Persistent(serialized=true). Blob values are not indexed and cannot be used in query filters or sort orders.
Here is an example of a simple Serializable class that represents a file, including the file contents, a filename and a MIME type. This is not a JDO data class, so there are no persistence annotations.
import java.io.Serializable;
public class DownloadableFile implements Serializable {
private byte[] content;
private String filename;
private String mimeType;
// ... accessors ...
}To store an instance of a Serializable class as a Blob value in a property, declare a field whose type is the class, and use the #Persistent(serialized = "true") annotation:
import javax.jdo.annotations.Persistent;
import DownloadableFile;
// ...
#Persistent(serialized = "true")
private DownloadableFile file;
I your case you can use
import java.io.Serializable;
public class B implements Serializable {
private int xx;
....
..........
}
Then declare it in your data class
#Persistent(serialized = "true")
private B b;
is it possible to use Constraints in the google-app-engine? It seems not to work ...
http://www.datanucleus.org/products/accessplatform_1_1/jpa/orm/constr...
The properties codingSystem and code should be unique. Is there a
workaround?
#Entity
#Table(uniqueConstraints = {
#UniqueConstraint(columnNames = { "codingSystem", "code" }) })
public class ArticleCode {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Key id;
private String codingSystem;
private String code;
Thanks,
Ralph
In a nutshell, no, they're not. The underlying datastore implementation doesn't support global transactions, so it's not practical to enforce arbitrary uniqueness constraints.
The workaround is to make the unique components part of the key name.
Thanks a lot, it works fine.
Here is my new code.
#Entity
public class ArticleCode {
#Id
private Key id;
#Column(name="codingSystem")
private String codingSystem;
#Column(name="code")
private String code;
public ArticleCode(Key parent, String codingSystem, String code) {
this.id = KeyFactory.createKey(parent, ArticleCode.class.getSimpleName(), codingSystem + code);
this.codingSystem = codingSystem;
this.code = code;
}