Solr 9.2.1 server SolrJ index and retrieve issues

73 Views Asked by At
  1. Env: Solr 9.2.1 docker image
  2. I am using Parent POJO, Child POJO and indexing with SolrJ library
  3. I am looking for SolrJ solution who could help me
  4. managed-schema.xml updated with <field name="_nest_parent_" type="string" indexed="true" stored="true"/>

There are issues which i noticed

  1. Parent and Child POJO name, c1 attribute indexes as multivalued, i need only single value

  2. Parent id is not nesting with child

  3. When i retrieve based on query , i get child null

             import java.util.List;
             import org.apache.solr.client.solrj.beans.Field;
    
         public class Top {
             @Field
             private String id;
             @Field
             private String name;
             @Field(child = true)
             private List<Child> childs;
    
             public Top() {
             }
    
             public String getId() {
                 return id;
             }
    
             public void setId(String id) {
                 this.id = id;
             }
    
             public String getName() {
                 return name;
             }
    
             public void setName(String name) {
                 this.name = name;
             }
    
             public List<Child> getChilds() {
                 return childs;
             }
    
             public void setChilds(List<Child> childs) {
                 this.childs = childs;
             }
         }
    
    
         import org.apache.solr.client.solrj.beans.Field;
    
         public class Child {
             @Field
             private String id;
             @Field
             private String c1;
    
             public Child() {
             }
             public String getId() {
                 return id;
             }
             public void setId(String id) {
                 this.id = id;
             }
             public String getC1() {
                 return c1;
             }
             public void setC1(String c1) {
                 this.c1 = c1;
             }
         }
    

Main

HttpSolrClient solrClient = new HttpSolrClient.Builder(solrBaseURL + "/" + core_users)
            .withConnectionTimeout(1000, TimeUnit.MILLISECONDS)
            .withSocketTimeout(2000, TimeUnit.MILLISECONDS)
            .build();
try{
                    // Index
                    Child c1 = new Child();
                    c1.setC1("C1");
                    c1.setId("P0Child0");
                    Top top = new Top();
                    top.setId("P0");
                    top.setName("P0_Name");
                    top.setChilds(Arrays.asList(c1));

                    solrClient.addBean(top);
                    solrClient.commit();
                    
                    
                    // Retrieve
                    Map<String, String> map = new HashMap<>();
                    map.put(CommonParams.Q, "id:P0");
                    SolrParams solrMap = new MapSolrParams(map);
                    QueryResponse queryResponse = solrClient.query(solrMap);
                    System.out.println(queryResponse.getResults());
              List<Top> beans = queryResponse.getBeans(Top.class);
                    System.out.println(beans.get(0).getChilds());

        } catch(Exception e) {
            e.printStackTrace();
        } 

Sample result http://localhost:8983/solr/users/select?fl=*%2C%5Bchild%5D&indent=true&q.op=OR&q=*%3A*&useParams=

    {
  "responseHeader":{
    "status":0,
    "QTime":2,
    "params":{
      "q":"*:*",
      "indent":"true",
      "fl":"*,[child]",
      "q.op":"OR",
      "useParams":"",
      "_":"1689603922013"}},
  "response":{"numFound":2,"start":0,"numFoundExact":true,"docs":[
      {
        "id":"P0Child0",
        "c1":["C1"],
        "_version_":1771697482056597504},
      {
        "id":"P0",
        "name":["P0_Name"],
        "_version_":1771697482056597504}]
  }}
0

There are 0 best solutions below