Using Java to manipulate Sphinx

To use Java to operate Sphinx, you need to follow the following steps: Step 1: Configure Maven dependencies In order to use Sphinx, you need to add the following Maven dependencies to the pom.xml file of the project: <dependencies> <dependency> <groupId>org.sphinxsearch</groupId> <artifactId>sphinx4j</artifactId> <version>2.2.11</version> </dependency> </dependencies> Ensure the correct Sphinx version number is used. Step 2: Create a Sphinx connection Create a connection to the Sphinx server using the SphinxClient class. You need to specify the host name and port number of the Sphinx server. import org.sphinxsearch.SphinxClient; public class SphinxExample { public static void main(String[] args) { SphinxClient sphinxClient = new SphinxClient(); SphinxClient. setServer ("localhost", 9312)// Set the host name and port number of the Sphinx server //You can also set other connection parameters, such as setting timeout time, etc } } Step 3: Insert Data To insert data, you need to create a Document object, add fields and field values to it, and then call SphinxClient's updateDocuments () method. import org.sphinxsearch.SphinxClient; import org.sphinxsearch.SphinxDocument; import org.sphinxsearch.SphinxException; public class SphinxExample { public static void main(String[] args) { try { SphinxClient sphinxClient = new SphinxClient(); sphinxClient.setServer("localhost", 9312); SphinxDocument document = new SphinxDocument(); document.addStringField("title", "Example Title"); document.addStringField("content", "Example Content"); sphinxClient.updateDocuments(new SphinxDocument[]{document}, "index_name", "rt_name"); } catch (SphinxException e) { e.printStackTrace(); } } } Note: index_ Name is the name of your Sphinx index, rt_ Name is the name of your Sphinx real-time index. Step 4: Modify data To modify data, you need to specify the fields and field values to be modified, and then call SphinxClient's updateAttributes() method. import org.sphinxsearch.SphinxClient; import org.sphinxsearch.SphinxException; public class SphinxExample { public static void main(String[] args) { try { SphinxClient sphinxClient = new SphinxClient(); sphinxClient.setServer("localhost", 9312); sphinxClient.updateAttributes("index_name", new long[]{1234}, new String[]{"title"}, new Object[]{"New Title"}); } catch (SphinxException e) { e.printStackTrace(); } } } Step 5: Query Data To query data, you need to use SphinxClient's query() method and specify query keywords and index names. import org.sphinxsearch.SphinxClient; import org.sphinxsearch.SphinxException; import org.sphinxsearch.SphinxResult; public class SphinxExample { public static void main(String[] args) { try { SphinxClient sphinxClient = new SphinxClient(); sphinxClient.setServer("localhost", 9312); SphinxResult result = sphinxClient.query("example query", "index_name"); //Process query results } catch (SphinxException e) { e.printStackTrace(); } } } The query() method returns a SphinxResult object that you can use to access query results, such as obtaining the number of matching documents, obtaining the value of each matching field, and so on. Step 6: Delete data To delete data, you need to specify the deleted document ID and call the deleteDocuments () method of SphinxClient. import org.sphinxsearch.SphinxClient; import org.sphinxsearch.SphinxException; public class SphinxExample { public static void main(String[] args) { try { SphinxClient sphinxClient = new SphinxClient(); sphinxClient.setServer("localhost", 9312); sphinxClient.deleteDocuments("index_name", new long[]{1234}); } catch (SphinxException e) { e.printStackTrace(); } } } This is the basic steps for using Java to operate Sphinx. You can perform more complex operations based on specific needs.