结构化搜素
注意
term 和 terms 是 contain 不是equals 的关系
精确查找 term
constant_score 不对查询进行评分计算
term 精确查找
SELECT product
FROM products
WHERE productID = “XHDK-A-1293-#fJ3”
GET /my_store/products/_search
{
“query” : {
“constant_score” : {
“filter” : {
“term” : {
“productID” : “XHDK-A-1293-#fJ3”
}
}
}
}
}
组合过滤器 should 、must 、must_not
SELECT product
FROM products
WHERE (price = 20 OR productID = “XHDK-A-1293-#fJ3”)
AND (price != 30)
bool过滤器
{
“bool” : {
“must” : [],
“should” : [],
“must_not” : [],
}
}
must
所有的语句都 必须(must) 匹配,与 AND 等价。
must_not
所有的语句都 不能(must not) 匹配,与 NOT 等价。
should
至少有一个语句要匹配,与 OR 等价。
GET /my_store/products/_search
{
“query” : {
“filtered” : {
“filter” : {
“bool” : {
“should” : [
{ “term” : {“price” : 20}},
{ “term” : {“productID” : “XHDK-A-1293-#fJ3”}}
],
“must_not” : {
“term” : {“price” : 30}
}
}
}
}
}
}
嵌套bool
SELECT document
FROM products
WHERE productID = “KDKE-B-9947-#kL5”
OR ( productID = “JODL-X-1937-#pV7”
AND price = 30 )
GET /my_store/products/_search
{
“query” : {
“filtered” : {
“filter” : {
“bool” : {
“should” : [
{ “term” : {“productID” : “KDKE-B-9947-#kL5”}},
{ “bool” : {
“must” : [
{ “term” : {“productID” : “JODL-X-1937-#pV7”}},
{ “term” : {“price” : 30}}
]
}}
]
}
}
}
}
}
查询多个精确值 terms
GET /my_store/products/_search
{
“query” : {
“constant_score” : {
“filter” : {
“terms” : {
“price” : [20, 30]
}
}
}
}
}
范围查询 range
SELECT document
FROM products
WHERE price BETWEEN 20 AND 40
“range” : {
“price” : {
“gte” : 20,
“lte” : 40
}
}
gt: > 大于(greater than)
lt: < 小于(less than)
gte: >= 大于或等于(greater than or equal to)
lte: <= 小于或等于(less than or equal to)
日期范围
“range” : {
“timestamp” : {
“gt” : “2014-01-01 00:00:00”,
“lt” : “2014-01-07 00:00:00”
}
}
null 值处理
SELECT tags
FROM posts
WHERE tags IS NOT NULL
GET /my_index/posts/_search
{
“query” : {
“constant_score” : {
“filter” : {
“exists” : { “field” : “tags” }
}
}
}
}
缺失查询
SELECT tags
FROM posts
WHERE tags IS NULL
GET /my_index/posts/_search
{
“query” : {
“constant_score” : {
“filter”: {
“missing” : { “field” : “tags” }
}
}
}
}
对象缺失
{
“bool”: {
“should”: [
{ “exists”: { “field”: “name.first” }},
{ “exists”: { “field”: “name.last” }}
]
}
}
full-text-search 全文搜索
全文查询,match ,query_string
数据
POST /my_index/my_type/_bulk
{ “index”: { “_id”: 1 }}
{ “title”: “The quick brown fox” }
{ “index”: { “_id”: 2 }}
{ “title”: “The quick brown fox jumps over the lazy dog” }
{ “index”: { “_id”: 3 }}
{ “title”: “The quick brown fox jumps over the quick dog” }
{ “index”: { “_id”: 4 }}
{ “title”: “Brown fox brown dog” }
单个查询
GET /my_index/my_type/_search
{
“query”: {
“match”: {
“title”: “QUICK!”
}
}
}
多词查询
GET /my_index/my_type/_search
{
“query”: {
“match”: {
“title”: “BROWN DOG!”
}
}
}
提高精度
GET /my_index/my_type/_search
match 查询还可以接受 operator 操作符作为输入参数,默认情况下该操作符是 or 。我们可以将它修改成 and 让所有指定词项都必须匹配:
{
“query”: {
“match”: {
“title”: {
“query”: “BROWN DOG!”,
“operator”: “and”
}
}
}
}
组合查询
GET /my_index/my_type/_search
{
“query”: {
“bool”: {
“must”: { “match”: { “title”: “quick” }},
“must_not”: { “match”: { “title”: “lazy” }},
“should”: [
{ “match”: { “title”: “brown” }},
{ “match”: { “title”: “dog” }}
]
}
}
}
操作符
bool 匹配
{
“match”: { “title”: “brown fox”}
}
{
“bool”: {
“should”: [
{ “term”: { “title”: “brown” }},
{ “term”: { “title”: “fox” }}
]
}
}
如果使用 and 操作符,所有的 term 查询都被当作 must 语句,所以 所有(all) 语句都必须匹配。以下两个查询是等价的:
{
“match”: {
“title”: {
“query”: “brown fox”,
“operator”: “and”
}
}
}
查询结果优化
https://www.elastic.co/guide/cn/elasticsearch/guide/current/_best_fields.html
https://www.elastic.co/guide/cn/elasticsearch/guide/current/_tuning_best_fields_queries.html
多字段查询multi_match
查询字段模糊
{
“multi_match”: {
“query”: “Quick brown fox”,
“fields”: “*_title”
}
}
跨字段搜素
{
“query”: {
“bool”: {
“should”: [
{ “match”: { “street”: “Poland Street W1V” }},
{ “match”: { “city”: “Poland Street W1V” }},
{ “match”: { “country”: “Poland Street W1V” }},
{ “match”: { “postcode”: “Poland Street W1V” }}
]
}
}
}
{
“query”: {
“multi_match”: {
“query”: “Poland Street W1V”,
“type”: “most_fields”,
“fields”: [ “street”, “city”, “country”, “postcode” ]
}
}
}
近似匹配
短语匹配,match_phrase
一个被认定为和短语 quick brown fox 匹配的文档,必须满足以下这些要求:
quick 、 brown 和 fox 需要全部出现在域中。
brown 的位置应该比 quick 的位置大 1 。
fox 的位置应该比 quick 的位置大 2 。
GET /my_index/my_type/_search
{
“query”: {
“match_phrase”: {
“title”: “quick brown fox”
}
}
}
精确短语匹配 或许是过于严格了。也许我们想要包含 “quick brown fox” 的文档也能够匹配 “quick fox,” , 尽管情形不完全相同。
加入slop参数
slop参数详细使用 https://www.elastic.co/guide/cn/elasticsearch/guide/current/slop.html
GET /my_index/my_type/_search
{
“query”: {
“match_phrase”: {
“title”: {
“query”: “quick fox”,
“slop”: 1
}
}
}
}
本文探讨了结构化搜索的概念,包括精确查找、组合过滤器、范围查询、全文搜索及匹配查询等多种搜索技术。深入解析了Elasticsearch的查询语法,如term、terms、bool过滤器、range、match及multi_match等,旨在提升搜索效率和结果相关性。
315

被折叠的 条评论
为什么被折叠?



