Foros de discusión

com.liferay.portal.kernel.search.Document and elasticsearch nested fields

thumbnail
Miguel Ángel Júlvez, modificado hace 7 años.

com.liferay.portal.kernel.search.Document and elasticsearch nested fields

Junior Member Mensajes: 63 Fecha de incorporación: 29/03/11 Mensajes recientes
Hi all,

I'd like to index nested fields in elasticsearch (https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-mapping.html) but I don't know how to do it with "com.liferay.portal.kernel.search.Document" class

Something like this additional mapping:

{"LiferayDocumentType": {
    "properties": {
      "cdu": {
        "type": "string",
        "index": "not_analyzed",
        "store": true
      },
      "pages": {
        "type": "nested",
        "properties": {
          "pageNumber": {
            "type": "integer",
            "format": "not_analyzed"
          },
          "pageContent": {
            "type": "string",
            "index": "analyzed"
          }
        }
      }
    }
  }
}


I'm not sure, but if it's not possible directoy directly I'm thinking in a workaround with a new "ElasticsearchDocumentFactory" service in order to overwrite "getElasticsearchDocument" and use the actual "com.liferay.portal.kernel.search.Document" class but saving the nested fields using a special naming convention and getting them in "getElasticsearchDocument" method. (because I don't know how to overwrite "com.liferay.portal.kernel.search.Document" without ext environment).

Any idea?

Thanks
Best regards
thumbnail
Miguel Ángel Júlvez, modificado hace 7 años.

RE: com.liferay.portal.kernel.search.Document and elasticsearch nested fiel (Respuesta)

Junior Member Mensajes: 63 Fecha de incorporación: 29/03/11 Mensajes recientes
I answer myself.

Yes, it's possible use com.liferay.portal.kernel.search.Document and com.liferay.portal.kernel.search.Field classes in order to get nested objects. Firstable, you need to add an additional mapping with your nested type and after that, you can use the classes named before to develop something similar to this code

 
document.addKeyword("cdu", cduValue);

FieldArray pages = new FieldArray("pages");

Field page1 = new Field("null");

Field pageNumber1 = new Field(("pageNumber"));
pageNumber1.setNumeric(true);
pageNumber1.setValue("1");
pageNumber1.setSortable(false);
pageNumber1.setParentField(page1);
pageNumber1.setNumericClass(Integer.class);

Field pageContent1 = new Field(("pageContent"));
pageContent1.setValue("contenido1");
pageContent1.setSortable(false);
pageContent1.setParentField(page1);

page1.addField(pageNumber1);
page1.addField(pageContent1);


Field page2 = new Field("null");

Field pageNumber2 = new Field(("numeroDePagina"));
pageNumber2.setNumeric(true);
pageNumber2.setValue("2");
pageNumber2.setSortable(false);
pageNumber2.setParentField(page2);
pageNumber2.setNumericClass(Integer.class);

Field pageContent2 = new Field(("pageContent"));
pageContent2.setValue("contenido2");
pageContent2.setSortable(false);
pageContent2.setParentField(page2);

page2.addField(pageNumber2);
page2.addField(pageContent2);


pages.addField(page1);
pages.addField(page2);

document.add(pages);


Kind regards
thumbnail
Sunit Chatterjee, modificado hace 6 años.

RE: com.liferay.portal.kernel.search.Document and elasticsearch nested fiel

Junior Member Mensajes: 28 Fecha de incorporación: 18/05/17 Mensajes recientes
Thanks Miguel, for the solution.
I was stuck with this issue and wasn't able to find anything in liferay documentation.

Your post helped me resolve the issue... So thanks a ton. :-)
thumbnail
Sunit Chatterjee, modificado hace 6 años.

RE: com.liferay.portal.kernel.search.Document and elasticsearch nested fiel

Junior Member Mensajes: 28 Fecha de incorporación: 18/05/17 Mensajes recientes
I was able to create the nested mapping via your solution.

Do you have any idea on how we can create the nested search query, using liferay search API, especially providing the inner hits and highlights configurations (since the Nested Query object of liferay does not takes any configurations around inner_hits and highlights)