⑴ elasticsearch 怎么同时检索单 field 多个值的条件
1、 多词条查询:
多词条查询 允许匹配那些在内容中含有某些词条的文档。词条查询允许匹配单个未经分析的词条,多词条查询可以用来匹配多个这样的词条。假设想得到所有在tags字段中含有novel或book的文档。运行以下查询来达到目的:
{
"query":{
"terms":{
"tags":["novel","book"],
"minimum_match":1
}
}
}
把minimum_match属性设置为1;这意味着至少有1个词条应该匹配。如果想要查询匹配所有词条的文档,可以把minimum_match属性设置为2
2、使用bool查询来合并多个term插叙。
可以通过布尔查询来封装无限数量的查询,并通过下面描述的节点之一使用一个逻辑值来连接它们。
should:被它封装的布尔查询可能被匹配,也可能不被匹配。
被匹配的should节点数由minimum_should_match参数控,此参数的值描述了文档被视为匹配时,应该匹配的should子句的最少数量。举例来说,它可以是个整数值,比如2,也可以是个百分比,比如75%。
3、must:被它封装的布尔查询必须被匹配,文档才会返回。
4、must_not:被它封装的布尔查询必须不被匹配,文档才会返回。
{
"query":{
"bool":{
"should":{
"term":{
"title":"aa"
}
},
"should":{
"term":{
"title":"bb"
}
},
"should":{
"term":{
"title":"cc"
}
},
minimum-should-match=1
}
}
}
⑵ elasticsearch query 和 filter 的区别
如下例子,查找性别是女,所在的州是PA,过滤条件是年龄是39岁,回balance大于等于10000的文答档: { "query": { "bool": { "must": [ { "match": { "gender": "F" } }, { "match": { "state": "PA" } } ], "filter": [ { "term": { "age": "39" } }...
⑶ elasticsearch 怎么实现模糊匹配
QueryDSL如下:
{
"query": {
"bool": {
"must": [
{
"term": {
"category_id": "7"
}
},
{
"term": {
"enable": 1
}
},
{
"term": {
"status": 2
}
},
{
"range": {
"stock": {
"gt": 0
}
}
},
{
"match": {
"search_field": {
⑷ Elasticsearch 怎么让模糊搜索和其他条件同时满足
{
"query": {
"bool": {
"must": [
{
"term": {
"allocation": "0"
}
},
{
"range": {
"order_id": {
"from": "0",
"to": "9999999999"
}
}
},
{
"match": {
"content" : {
"query" : "乘客离开",
"analyzer" : "ik"
}
}
}
],
"must_not": [],
"should": []
}
},
"from": 0,
"size": 10,
"sort": [],
"facets": {}
}
⑸ ElasticSearch中Filter和Query的异同
如下例子,查找性别是女,所在的州是PA,过滤条件是年龄是39岁,balance大于等于10000的文档:
{
"query":
{
"bool":
{
"must":
[
{
"match":
{
"gender":
"F"
}
},
{
"match":
{
"state":
"PA"
}
}
],
"filter":
[
{
"term":
{
"age":
"39"
}
},
{
"range":
{
"balance":
{
"gte":
"10000"
}
}
}
]
}
}
}
返回结果:
查询虽然包含这两种,但是查询在不同的执行环境下,操作还是不一样的。
Query与Filter
查询在Query查询上下文和Filter过滤器上下文中,执行的操作是不一样的:
Query查询上下文:
在查询上下文中,查询会回答这个问题——“这个文档匹不匹配这个查询,它的相关度高么?”
如何验证匹配很好理解,如何计算相关度呢?之前说过,ES中索引的数据都会存储一个_score分值,分值越高就代表越匹配。另外关于某个搜索的分值计算还是很复杂的,因此也需要一定的时间。
查询上下文
是在
使用query进行查询时的执行环境,比如使用search的时候。
Filter过滤器上下文:
在过滤器上下文中,查询会回答这个问题——“这个文档匹不匹配?”
答案很简单,是或者不是。它不会去计算任何分值,也不会关心返回的排序问题,因此效率会高一点。
过滤上下文
是在使用filter参数时候的执行环境,比如在bool查询中使用Must_not或者filter。
另外,经常使用过滤器,ES会自动的缓存过滤器的内容,这对于查询来说,会提高很多性能。
总结
1
查询上下文中,查询操作不仅仅会进行查询,还会计算分值,用于确定相关度;在过滤器上下文中,查询操作仅判断是否满足查询条件
2
过滤器上下文中,查询的结果可以被缓存。
⑹ elasticsearch怎么实现分组排序
thisObj.className = "active"; document.getElementById(tabObj+"_Content"+i).style.display = "block"; }else{ tabList[i].className = "normal"; document.getElementById(tabObj+"_Content"+i).style.display = "none"; }
⑺ elasticsearch 怎么过滤分词一个字
分词一个字
⑻ 如何让elasticsearch字段不分池
该属性的取值可以为yes和no,用于指定字段的原始属性是否存入索引。默认值是no.意味着不能版在结果中返回字段的原权始值(即使没有存储原始值,也可以使用Soure字段返回原始值)。如果已经建立索引可以搜索该字段的内容。
⑼ ElasticSearch DSL 分组查询后再次分组问题
SearchRequestBuilder对象设置index、type、分页、高亮等等后,打印这个对象即可
⑽ 请教elasticsearch自定义结果集过滤如何支持
用groovy脚本自定义ElasticSearch查询,来实现以上功能。 例,数据中包含字段birdtyday,记录游客生日: "birthday": "1992-02-05 00:00:00", 新建文件getAgeByBirthday.groovy,编辑其内容为: def b = doc[birthday_field].value def birthday = new Date(b) def now = new Date() long age = (now -birthday)/365 age 并把此文件放在es的config/scripts目录下(如果没有此目录就新建一个)。 然后在config/elasticsearch.yml文件中加一行: script.groovy.sandbox.enabled: true 最后重启es即可。 接下来,我们就可以用以下DSL进行年龄统计了 GET /lovingtrip-report/hotelcustomer/_search?search_type=count { "aggs": { "counts_by_age": { "terms": { "script_file": "getAgeByBirthday", "params": { "birthday_field": "birthday" }, "size": 100 } } } } 或者: GET /lovingtrip-report/hotelcustomer/_search?search_type=count { "aggs": { "histogram_by_age": { "histogram": { "script_file": "getAgeByBirdthday", "params": { "birdthday_field": "birdthday" }, "interval": 5 } } } } 不过脚本查询性能不佳,且不能利用es的缓存,所以在大数据量或高性能要求的场景下不适用。。 ------------------------------------- 补充一个自定义的年龄range过滤: range_AgeByBirthday.groovy: def b = doc[birdthday_field].value def birdthday = new Date(b) def now = new Date() long age = (now -birdthday)/365 gte<=age && age<=lte DSL: GET /lovingtrip-report/hotelcustomer/_search?search_type=count { "query": { "filtered": { "filter": { "script": { "script_file": "range_AgeByBirdthday", "params": { "birdthday_field": "birdthday", "gte": 50, "lte": 60 } } } } }, "aggs": { "histogram_by_age": { "histogram": { "script_file": "getAgeByBirdthday", "params": { "birdthday_field": "birdthday" }, "interval": 5 } } } }