"localhost", 'port' => 8983, )); $count = 0; $locations = array(); $sxml = simplexml_load_file($argv[1]); foreach ($sxml->node as $node) { $q = new SolrInputDocument(); $q->addField( 'id', "n{$node['id']}" );; $q->addField( 'type', 1 ); $q->addField( 'location', "{$node['lat']},{$node['lon']}" ); if (parseNode($q, $node)) { $client->addDocument( $q ); $count++; if ($count % 1000 === 0) { echo "."; } } // also keep a cache of all nodes and locations $locations[(string) $node['id']] = array( (string) $node['lat'], (string) $node['lon'] ); } foreach ($sxml->way as $way) { $q = new SolrInputDocument(); $q->addField( 'id', "w{$way['id']}" );; $q->addField( 'type', 2 ); parseNode($q, $way); fetchLocations($q, $locations, $way); $client->addDocument( $q ); $count++; if ($count % 1000 === 0) { echo "."; } } $client->commit(); function fetchLocations($q, $locations, $node) { $count = 0; $lat = $lon = 0; foreach ($node->nd as $nd) { $count++; $lat += $locations[(string) $nd['ref']][0]; $lon += $locations[(string) $nd['ref']][1]; } $locLat = $lat / $count; $locLon = $lon / $count; $q->addField('location', "{$locLat},{$locLon}"); } function parseNode($q, $sxml) { $standAloneNode = false; $amenity = NULL; $street = $postcode = $nr = $cuisine = false; // loop over tags foreach( $sxml->tag as $tag ) { if ($tag['k'] == 'name') { $q->addField( 'name', trim( (string) $tag['v'] ) ); $standAloneNode = true; } if ($tag['k'] == 'cuisine') { $cuisine = (string) $tag['v']; } if ($tag['k'] == 'amenity') { $amenity = $tag['v']; } if ($tag['k'] == 'amenity' && $tag['v'] == 'pub') { $cuisine = 'pub'; } if ($tag['k'] == 'amenity' && $tag['v'] == 'bar') { $cuisine = 'bar'; } if ($tag['k'] == 'contact:phone') { $q->addField( 'phone', trim( (string) $tag['v'] ) ); } if ($tag['k'] == 'addr:street') { $street = $tag['v']; } if ($tag['k'] == 'addr:postcode') { $postcode = strtoupper( (string) $tag['v'] ); } if ($tag['k'] == 'addr:housenumber') { $nr = $tag['v']; } } if ($amenity === null || in_array($amenity, array('post_box', 'parking', 'grave_yard'))) { return false; } if ( $street || $nr ) { $q->addField('address', trim( "{$nr} {$street}") ); } if ( $postcode ) { $q->addField( 'postcode', $postcode ); } if ( $cuisine ) { $q->addField( 'cuisine', $cuisine ); } if ( $amenity ) { $q->addField( 'amenity', $amenity ); } return $standAloneNode; }