全文搜索
> 文档中心 > 文档中心 > INFINI Easysearch > 功能手册 > SQL 查询 > 全文搜索

全文搜索 #

全文搜索是对存储的单个文档进行搜索,这与基于数据库中的原始文本的常规搜索有所区别。全文搜索尝试通过检查每个文档中的所有单词来匹配搜索条件。 在 Easysearch 中,提供的全文查询使你能够搜索在索引过程中分析的文本字段。

Match Query #

在 Easysearch 中,Match 查询是执行全文搜索的标准查询。MATCHQUERYMATCH_QUERY 都是用于执行匹配查询的函数。

示例 1 #

这两个函数都可以接受字段名称作为第一个参数,文本作为第二个参数。

SQL query:

POST /_sql
{
  "query" : """
    SELECT account_number, address
    FROM accounts
    WHERE MATCH_QUERY(address, 'Holmes')
    """
}

解释:

{
  "from" : 0,
  "size" : 200,
  "query" : {
    "bool" : {
      "filter" : [
        {
          "bool" : {
            "must" : [
              {
                "match" : {
                  "address" : {
                    "query" : "Holmes",
                    "operator" : "OR",
                    "prefix_length" : 0,
                    "max_expansions" : 50,
                    "fuzzy_transpositions" : true,
                    "lenient" : false,
                    "zero_terms_query" : "NONE",
                    "auto_generate_synonyms_phrase_query" : true,
                    "boost" : 1.0
                  }
                }
              }
            ],
            "adjust_pure_negative" : true,
            "boost" : 1.0
          }
        }
      ],
      "adjust_pure_negative" : true,
      "boost" : 1.0
    }
  },
  "_source" : {
    "includes" : [
      "account_number",
      "address"
    ],
    "excludes" : [ ]
  }
}

结果集:

account_numberaddress
1880 Holmes Lane

示例 2 #

这两个函数也可以接受单个参数,并按以下方式使用。

SQL query:

POST /_sql
{
  "query" : """
    SELECT account_number, address
    FROM accounts
    WHERE address = MATCH_QUERY('Holmes')
    """
}

解释:

{
  "from" : 0,
  "size" : 200,
  "query" : {
    "bool" : {
      "filter" : [
        {
          "bool" : {
            "must" : [
              {
                "match" : {
                  "address" : {
                    "query" : "Holmes",
                    "operator" : "OR",
                    "prefix_length" : 0,
                    "max_expansions" : 50,
                    "fuzzy_transpositions" : true,
                    "lenient" : false,
                    "zero_terms_query" : "NONE",
                    "auto_generate_synonyms_phrase_query" : true,
                    "boost" : 1.0
                  }
                }
              }
            ],
            "adjust_pure_negative" : true,
            "boost" : 1.0
          }
        }
      ],
      "adjust_pure_negative" : true,
      "boost" : 1.0
    }
  },
  "_source" : {
    "includes" : [
      "account_number",
      "address"
    ],
    "excludes" : [ ]
  }
}

结果集:

account_numberaddress
1880 Holmes Lane

Multi-match Query #

除了针对单个字段进行匹配查询外,您还可以使用多个字段搜索文本。为此,提供了函数 MULTI_MATCHMULTIMATCHMULTIMATCHQUERY

示例 #

每个前面提到的函数都接受一个 query 参数用于指定要搜索的文本,以及一个 fields 参数用于指定要搜索的字段名称或模式。例如,以下查询在名为 accounts 的索引中搜索名字或姓氏为 ‘Dale’ 的文档:

SQL query:

POST /_sql
{
  "query" : """
    SELECT firstname, lastname
    FROM accounts
    WHERE MULTI_MATCH('query'='Dale', 'fields'='*name')
    """
}

解释:

{
  "from" : 0,
  "size" : 200,
  "query" : {
    "bool" : {
      "filter" : [
        {
          "bool" : {
            "must" : [
              {
                "multi_match" : {
                  "query" : "Dale",
                  "fields" : [
                    "*name^1.0"
                  ],
                  "type" : "best_fields",
                  "operator" : "OR",
                  "slop" : 0,
                  "prefix_length" : 0,
                  "max_expansions" : 50,
                  "zero_terms_query" : "NONE",
                  "auto_generate_synonyms_phrase_query" : true,
                  "fuzzy_transpositions" : true,
                  "boost" : 1.0
                }
              }
            ],
            "adjust_pure_negative" : true,
            "boost" : 1.0
          }
        }
      ],
      "adjust_pure_negative" : true,
      "boost" : 1.0
    }
  },
  "_source" : {
    "includes" : [
      "firstname",
      "lastname"
    ],
    "excludes" : [ ]
  }
}

结果集:

firstnamelastname
DaleAdams

Query String 查询 #

查询字符串查询是基于 Lucene 查询字符串语法解析和拆分提供的查询字符串。这种小语言支持逻辑连接词、通配符、正则表达式和邻近搜索。更多详细信息请参阅官方文档。请注意,如果在查询字符串中存在任何无效语法,则会抛出错误。

示例 #

QUERY 函数接受查询字符串,并分别对符合查询字符串的文档返回true或false。

SQL query:

POST /_sql
{
  "query" : """
    SELECT account_number, address
    FROM accounts
    WHERE QUERY('address:Lane OR address:Street')
    """
}

解释:

{
  "from" : 0,
  "size" : 200,
  "query" : {
    "bool" : {
      "filter" : [
        {
          "bool" : {
            "must" : [
              {
                "query_string" : {
                  "query" : "address:Lane OR address:Street",
                  "fields" : [ ],
                  "type" : "best_fields",
                  "default_operator" : "or",
                  "max_determinized_states" : 10000,
                  "enable_position_increments" : true,
                  "fuzziness" : "AUTO",
                  "fuzzy_prefix_length" : 0,
                  "fuzzy_max_expansions" : 50,
                  "phrase_slop" : 0,
                  "escape" : false,
                  "auto_generate_synonyms_phrase_query" : true,
                  "fuzzy_transpositions" : true,
                  "boost" : 1.0
                }
              }
            ],
            "adjust_pure_negative" : true,
            "boost" : 1.0
          }
        }
      ],
      "adjust_pure_negative" : true,
      "boost" : 1.0
    }
  },
  "_source" : {
    "includes" : [
      "account_number",
      "address"
    ],
    "excludes" : [ ]
  }
}

结果集:

account_numberaddress
1880 Holmes Lane
6671 Bristol Street
13789 Madison Street

Match Phrase 查询 #

匹配短语查询与匹配查询类似,但用于匹配确切的短语。为此,提供了函数 MATCHPHRASEMATCH_PHRASEMATCHPHRASEQUERY

示例 #

SQL query:

POST /_sql
{
  "query" : """
    SELECT account_number, address
    FROM accounts
    WHERE MATCH_PHRASE(address, '880 Holmes Lane')
    """
}

解释:

{
  "from" : 0,
  "size" : 200,
  "query" : {
    "bool" : {
      "filter" : [
        {
          "bool" : {
            "must" : [
              {
                "match_phrase" : {
                  "address" : {
                    "query" : "880 Holmes Lane",
                    "slop" : 0,
                    "zero_terms_query" : "NONE",
                    "boost" : 1.0
                  }
                }
              }
            ],
            "adjust_pure_negative" : true,
            "boost" : 1.0
          }
        }
      ],
      "adjust_pure_negative" : true,
      "boost" : 1.0
    }
  },
  "_source" : {
    "includes" : [
      "account_number",
      "address"
    ],
    "excludes" : [ ]
  }
}

结果集:

account_numberaddress
1880 Holmes Lane

Score Query #

Easysearch支持将过滤查询包装起来,以便在每个匹配的文档中返回相关性得分。可以使用 SCORESCOREQUERYSCORE_QUERY 来实现此功能。

示例 #

第一个参数是匹配查询表达式,第二个参数是一个可选的浮点数,用于提高得分。默认值为1.0。除此之外,隐式变量_score也可用于返回每个文档的得分或用于排序。

SQL query:

POST /_sql
{
  "query" : """
    SELECT account_number, address, _score
    FROM accounts
    WHERE SCORE(MATCH_QUERY(address, 'Lane'), 0.5) OR
      SCORE(MATCH_QUERY(address, 'Street'), 100)
    ORDER BY _score
    """
}

解释:

{
  "from" : 0,
  "size" : 200,
  "query" : {
    "bool" : {
      "must" : [
        {
          "bool" : {
            "should" : [
              {
                "constant_score" : {
                  "filter" : {
                    "match" : {
                      "address" : {
                        "query" : "Lane",
                        "operator" : "OR",
                        "prefix_length" : 0,
                        "max_expansions" : 50,
                        "fuzzy_transpositions" : true,
                        "lenient" : false,
                        "zero_terms_query" : "NONE",
                        "auto_generate_synonyms_phrase_query" : true,
                        "boost" : 1.0
                      }
                    }
                  },
                  "boost" : 0.5
                }
              },
              {
                "constant_score" : {
                  "filter" : {
                    "match" : {
                      "address" : {
                        "query" : "Street",
                        "operator" : "OR",
                        "prefix_length" : 0,
                        "max_expansions" : 50,
                        "fuzzy_transpositions" : true,
                        "lenient" : false,
                        "zero_terms_query" : "NONE",
                        "auto_generate_synonyms_phrase_query" : true,
                        "boost" : 1.0
                      }
                    }
                  },
                  "boost" : 100.0
                }
              }
            ],
            "adjust_pure_negative" : true,
            "boost" : 1.0
          }
        }
      ],
      "adjust_pure_negative" : true,
      "boost" : 1.0
    }
  },
  "_source" : {
    "includes" : [
      "account_number",
      "address",
      "_score"
    ],
    "excludes" : [ ]
  },
  "sort" : [
    {
      "_score" : {
        "order" : "asc"
      }
    }
  ]
}

结果集:

account_numberaddress_score
1880 Holmes Lane0.5
6671 Bristol Street100
13789 Madison Street100