full-text search examples

本文详细介绍了SQL中使用CONTAINS、FREETEXT、CONTAINSTABLE和FREETEXTTABLE进行全文搜索和近似搜索的方法。通过具体实例展示了如何在产品名称中查找特定价格的产品、使用FREETEXT搜索包含特定关键词的文档、使用CONTAINSTABLE进行复杂关键字的搜索以及使用FREETEXTTABLE进行高级查询并添加排名。
A. Using CONTAINS with <simple_term>

The following example finds all products with a price of $80.99 that contain the word "Mountain".

USE AdventureWorks2012 GO SELECT Name, ListPrice FROM Production.Product WHERE ListPrice = 80.99 AND CONTAINS(Name, 'Mountain') GO
B. Using FREETEXT to search for words containing specified character values

The following example searches for all documents containing the words related to vital, safety, components.

USE AdventureWorks2012 GO SELECT Title FROM Production.Document WHERE FREETEXT (Document, 'vital safety components') GO


C. Using CONTAINSTABLE

The following example returns the description ID and description of all products for which the Description column contain the word "aluminum" near either the word "light" or the word "lightweight." Only rows with a rank value of 2 or higher are returned.

USE AdventureWorks2012 GO SELECT FT_TBL.ProductDescriptionID, FT_TBL.Description, KEY_TBL.RANK FROM Production.ProductDescription AS FT_TBL INNER JOIN CONTAINSTABLE (Production.ProductDescription, Description, '(light NEAR aluminum) OR (lightweight NEAR aluminum)' ) AS KEY_TBL ON FT_TBL.ProductDescriptionID = KEY_TBL.[KEY] WHERE KEY_TBL.RANK > 2 ORDER BY KEY_TBL.RANK DESC; GO
D. Using FREETEXTTABLE

The following example extends a FREETEXTTABLE query to return the highest ranked rows first and to add the ranking of each row to the select list. To specify the query, you must know that ProductDescriptionID is the unique key column for the ProductDescription table.

USE AdventureWorks2012 GO SELECT KEY_TBL.RANK, FT_TBL.Description FROM Production.ProductDescription AS FT_TBL INNER JOIN FREETEXTTABLE(Production.ProductDescription, Description, 'perfect all-around bike') AS KEY_TBL ON FT_TBL.ProductDescriptionID = KEY_TBL.[KEY] ORDER BY KEY_TBL.RANK DESC GO


Here is an extension of the same query that only returns rows with a rank value of 10 or greater:Here is an extension of the same query that only returns rows with a rank value of 10 or greater:

USE AdventureWorks2012 GO SELECT KEY_TBL.RANK, FT_TBL.Description FROM Production.ProductDescription AS FT_TBL INNER JOIN FREETEXTTABLE(Production.ProductDescription, Description, 'perfect all-around bike') AS KEY_TBL ON FT_TBL.ProductDescriptionID = KEY_TBL.[KEY] WHERE KEY_TBL.RANK >= 10 ORDER BY KEY_TBL.RANK DESC GO



B. Using FREETEXTTABLE

The following example extends a FREETEXTTABLE query to return the highest ranked rows first and to add the ranking of each row to the select list. To specify the query, you must know that ProductDescriptionID is the unique key column for the ProductDescription table.

The following example uses the ProductDescription table of the AdventureWorks2012 database. The query uses the CONTAINS predicate to search for descriptions in which the description ID is not equal to 5 and the description contains both the word "Aluminum" and the word "spindle." The search condition uses the AND Boolean operator.

USE AdventureWorks2012 GO SELECT Description FROM Production.ProductDescription WHERE ProductDescriptionID <> 5 AND CONTAINS(Description, 'aluminum AND spindle') GO



Sure, I can help you with Elasticsearch SQL access. Elasticsearch provides a SQL API that allows you to query data using SQL syntax. To access Elasticsearch using SQL, you need to follow these steps: 1. Install the necessary software: You need to install Elasticsearch and the appropriate version of the Elasticsearch JDBC driver for your programming language or client. 2. Configure Elasticsearch: Enable the SQL feature in your Elasticsearch configuration file. You can do this by setting `xpack.sql.enabled: true` in the `elasticsearch.yml` file. 3. Connect to Elasticsearch: Establish a connection to your Elasticsearch cluster using the JDBC driver. The connection string will depend on your specific setup and programming language. 4. Execute SQL queries: Once connected, you can execute SQL queries against Elasticsearch. The queries can include standard SQL statements like SELECT, INSERT, UPDATE, and DELETE, as well as Elasticsearch-specific extensions for full-text search and complex aggregations. Here's an example of how to query Elasticsearch using SQL in Java: ```java import java.sql.*; public class ElasticsearchSQLAccess { public static void main(String[] args) { Connection conn = null; Statement stmt = null; ResultSet rs = null; try { // Register the JDBC driver Class.forName("org.elasticsearch.xpack.sql.jdbc.EsDriver"); // Open a connection conn = DriverManager.getConnection("jdbc:es://localhost:9200"); // Create a statement stmt = conn.createStatement(); // Execute a query String sql = "SELECT * FROM index_name WHERE field_name = 'value'"; rs = stmt.executeQuery(sql); // Process the result set while (rs.next()) { // Access the retrieved data String field1 = rs.getString("field1"); int field2 = rs.getInt("field2"); // Do something with the data } } catch (SQLException | ClassNotFoundException e) { e.printStackTrace(); } finally { // Close the connections try { rs.close(); } catch (Exception ignored) {} try { stmt.close(); } catch (Exception ignored) {} try { conn.close(); } catch (Exception ignored) {} } } } ``` Remember to replace `index_name`, `field_name`, and the connection details with your actual values. That's a basic overview of how to access Elasticsearch using SQL. You can refer to the Elasticsearch documentation for more information on supported SQL syntax and examples for other programming languages.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值