如何在ES中加入否定查询

时间:2016-02-05 08:10:23

标签: elasticsearch

我的Elasticsearch设置中有2个索引。

  • 索引_A
  • index_b

index_a有一个字段location,大约有1000个文档,其中index_b有一个字段city,并且有大约600个文档。

我想找出locationsindex_a以外的所有city index_b字段import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class SQLserverConnection { // Connection object static Connection con = null; // Statement object private static Statement stmt; // Constant for Database URL public static String DB_URL = "jdbc:sqlserver://localhost:1433"; // Constant for Database Username public static String DB_USER = "sa"; // Constant for Database Password public static String DB_PASSWORD = "VMADMIN#123"; @BeforeTest public void setUp() throws Exception { try{ // Make the database connection String dbClass = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; Class.forName(dbClass).newInstance(); // Get connection to DB Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;sa;VMADMIN#123;"); // Statement object to send the SQL statement to the Database stmt = con.createStatement(); } catch (Exception e) { e.printStackTrace(); } } @Test public void test() { try{ String query = "select * from userinfo"; // Get the contents of userinfo table from DB ResultSet res = stmt.executeQuery(query); // Print the result untill all the records are printed // res.next() returns true if there is any next record else returns false while (res.next()) { System.out.print(res.getString(1)); System.out.print("\t" + res.getString(2)); System.out.print("\t" + res.getString(3)); System.out.println("\t" + res.getString(4)); } } catch(Exception e) { e.printStackTrace(); } } @AfterTest public void tearDown() throws Exception { // Close DB connection if (con != null) { con.close(); } } } 。我应该用什么查询来实现上述目标?

1 个答案:

答案 0 :(得分:3)

您执行两个查询:选择"所有必要信息"从 index_b 开始,然后使用" info"在 index_a 上执行搜索来自 index_b

Elasticsearch无法加入"以任何方式在单个查询中使用两个索引,而更喜欢使用非规范化数据,或使用嵌套或父子关系。