Search code examples
phpsphinx

How add attribute to Sphinx index from scheme?


I use PHP XMLWriter:

$xmlwriter->startElement('sphinx:schema');

   $xmlwriter->startElement('spinx:attribute');
     $xmlwriter->writeAttribute("name", "path");
     $xmlwriter->writeAttribute("type", "string");
   $xmlwriter->endElement(); // attr

   $xmlwriter->startElement('sphinx:field');
      $xmlwriter->writeAttribute("name", "content");
   $xmlwriter->endElement(); // field

$xmlwriter->endElement(); // schema

$xmlwriter->startElement('sphinx:document');
   $xmlwriter->writeAttribute("id", 1);
   $xmlwriter->writeAttribute("path", "TEST_ATTRIBUTE");
   $xmlwriter->startElement("content");
      $xmlwriter->text("TEST_CONTENT");
   $xmlwriter->endElement(); // field
$xmlwriter->endElement(); // doc
$xmlwriter->endElement(); // docset

So I get:

<?xml version="1.0" encoding="UTF-8"?>
<sphinx:docset>
  <sphinx:schema>
    <spinx:attribute name="path" type="string"/>  - why there is '/>' not '>'?
    <sphinx:field name="content"/>
  </sphinx:schema>

  <sphinx:document id="1" path="TEST_ATTRIBUTE">
    <content>TEST_CONTENT</content>
  </sphinx:document>
</sphinx:docset>

So I try: indexer --rotate --all --config /etc/sphinxsearch/sphinx.conf Sphinx 2.2.11-id64-release (95ae9a6) using config file 'sphinx.conf'... indexing index 'test1'...

WARNING: Attribute count is 0: switching to none docinfo

So I don't get atribute in index.


Solution

  • Looks like a typo...

    $xmlwriter->startElement('spinx:attribute');
                          -----^
    

    why there is '/>' not '>'?

    https://www.google.com/search?q=XML+closing+tags

    Each tag opened has to be closed. XML is pedantic about this. However, if a tag is empty (no content), a single tag can serve as both the opening and closing tag if it ends with /> rather than with > . XML - Libxml2 http://www.xmlsoft.org/XMLinfo.html