Elasticsearch

Elasticsearch

查询

基于词项的查询

精确查询

term查询

​ term 查询 相当于 =

{
  "from":0,
  "size":10,
  "query":{
      "term":{
          "products.id":"id1"
      }
  }
  "_source":{
    "includes": "products.*",
    "excludes": "products.tax_amount"
  },
  "sort":
    {"products.taxless_price":"asc"}    
}
terms 查询

​ terms 多值匹配,相当于in。也可以进行跨索引查询

​ 用户索引(用户id)、文章索引(文章id)

​ id为1的用户发表的文章,根据userid查询用户索引id为1发表出的文章id,然后根据该文章id查询出对应的文章内容

{
    "query":{
        "terms":{
            "_id":{
                "index":"user"
                "id":1
                "path":"articles"
            }
        }
    }
}
range 查询
  • gt 大于
  • lt 小于
  • gte 大于等于
  • lte 小于等于
  • booste 相关性评分
{
	"query":{
        "range":{
            "fieldname":{
                "gte":100,
                "glt":200
            }
        }
    }
}
exists查询

不为空 is not null

{
	"query":{
	    "exists": {"field":"products.taxless_price"}
	}
}
prefix 查询

like ‘xxxx%’

{
	"query":{
		"prefix":{
			"products._id":"idprefix"
		}
	}
}
wildcard 查询和regexp查询
  • wildcard 通配符查询 *匹配多个 ? 一个
  • regexp 正则查询
{
	"query":{
		"wildcard":{
			"products.manufacturer":"Gno*ho?se"
		}
	}
}
{
	"query":{
		"regexp":{
			"products.manufacturer":"Gno*ho?se"
		}
	}
}

基于全文的检索

与词项的区别在于,先进行分析

存储过程中的分析器
  1. 字段上设置分析器
  2. 索引上设置分析器
  3. 默认分析器

设置全局索引分析器

put test
{
    "settings":{
        "analysis":{
            "analyzer":{
                "default":{
                    "type":"simple"
                },
                "my_analyzer":{
                    "type":"standard",
                    "stopword":["the","a"]
                }
            }
        }
    }
}

设置字段级别分析器,可以分别设置存储和查询的分析器

put test
{
    "mapping":{
        "properties":{
            "field":{
            	"type":"text",
                "analyzer":"standard",
                "search_analyzer":"simple"
       		 }
        }
    }
}
查询时的分析器
  1. 搜索请求 带上
  2. 创建索引时候字段上的search_analyzer
  3. 创建索引时候字段上的analyzer
  4. 创建索引时候,索引上配置的default_search
  5. standard

match

  • query 匹配相应的值
  • operator and/or 上述值时与还是或
  • minimum_should_match 上述值至少匹配到几个 默认一个
get /kibana_sample_data_ecommerce/_search
{
  "query":{
    "match": {
      "customer_full_name":{
        "query":"Eddie Underwood",
        "operator":"and"
      }
    }
  },
  "_source":{
    "includes":"customer_full_name"
  }
}

multi_match

多个字段上进行查询

get /kibana_sample_data_ecommerce/_search
{
  "query":{
    "multi_match": {
        "query":"FEMALE",
        "fields": ["customer_full_name","customer_gender"],
        "operator": "and",
        "minimum_should_match":2
		}
  },
  "_source":{
    "includes":["customer_full_name","customer_gender"]
  }
  
}

match_phrase

精确短语匹配

  • slop 最大间隔几个词算匹配

java spark

customer_full_name hellp world, java is very goog, spark is also very good.

get /kibana_sample_data_ecommerce/_search
{
  "query":{
    "multi_phrase": {
        "customer_full_name":"Eddie Underwood",
        "slop":3
		}
  },
  "_source":{
    "includes":["customer_full_name","customer_gender"]
  }
  
}

match_phrase_prefix

基于前缀的短语匹配,最后一个词作为前缀匹配

  • max_expansions 默认50 多少次以后不再寻找
get /kibana_sample_data_ecommerce/_search
{
  "query":{
    "match_phrase_prefix": {
        "customer_full_name":"Eddie Underwood",
        "max_expansions":50
		}
  },
  "_source":{
    "includes":["customer_full_name","customer_gender"]
  }
  
}

模糊查询与纠错提示

编辑距离算法

Levenshtein

​ 替换、插入、删除(换位)

NGram size

​ Unigram Biggram Thrigram

fuzziness: 0.5/1/2/auto

get /kibana_sample_data_ecommerce/_search
{
  "query":{
    "fuzzy": {
        "customer_full_name": {
          "value": "Dawsun",
          "fuzziness": 2
        }

      
    }
  },
  "_source":{
    "includes":["customer_full_name","customer_gender"]
  }
}

提示器 搜索建议

  • term 会进行分词
  • phrase 短语搜索建议
  • completion 自动补全提示器
get /kibana_sample_data_ecommerce/_search
{
  "suggest":{
  	"msg-suggest":{
  		"text":"dawsun",
  		"term":{
  			"field":"customer_full_name"
  		}
  	}
  }
}

组合查询

bool(布尔)查询

  • must 必须包含的内容,影响相关度
  • filter 必须包含的内容,不影响相关度
  • should 不是必须包含的,影响相关度。 如果没有must,should 应该至少含有其中一个
  • must_not 不能包含的内容 ,不影响相关度
get /kibana_sample_data_ecommerce/_search
{
	"query":{
		"bool":{
			"must":[
				{"match":{"customer_full_name":"Underwood"}}
			],
			"should":[
				{"term":{"currency":"EUR"}},
				{"term":{"customer_phone":"1111"}}
			],
			"filter":{
				"term":{
					"day_of_week":"Sunday"
				}
			}
		}
	}
}

dis_max组合查询

相关度最大得分的作为分值返回结果,其他相关度低的进行忽略。

tie_breaker : 其他相关度低的可占据比重

{
	"query":{
		"dis_max":{
			"queries":[
				{"match":{"messgae":"firefox"}},
				{"term":{"geo.src":"CN"}},
				{"term":{"geo.dest":"CN"}}
			],
			"tie_breaker":0.7
		}
	}
}

constant_score 查询

返回出来的分值为一个固定值. 有什么用? 再组合,子查询出来的所有的分值都一样。

get /kibana_sample_data_ecommerce/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "bool": {
          "must": [
            {
              "match": {
                "customer_full_name": "Underwood"
              }
            },
            {
              "match": {
                "customer_gender": "MALE"
              }
            }
          ]
        }
      },
      "boost": 1.2
    }
  }
}

boosting 查询

  • positive 正相关的 类似于 bool查询里边的 must。必须满足。
  • negative 负相关的 类似于bool查询里边的must_not。不过这个如果匹配只会降低分值。
  • negative_boost 负相关 权重
GET /kibana_sample_data_ecommerce/_search
{
  "query": {
    "boosting": {
      "positive": [
        {"match":{"customer_full_name":"Underwood"}}  
      ],
      "negative": [
        {"match":{"customer_gender":"MALE"}}  
      ],
      "negative_boost": 0.2
    }
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值