搜索引擎(Elasticsearch搜索详解)

学完本课题,你应达成以下目标:

掌握ES搜索API的规则、用法。
掌握各类查询用法html

 

搜索API

搜索API 端点地址java

GET /twitter/_search?q=user:kimchy
GET /twitter/tweet,user/_search?q=user:kimchy
GET /kimchy,elasticsearch/_search?q=tag:wow
GET /_all/_search?q=tag:wow
GET /_search?q=tag:wow

搜索的端点地址能够是多索引多mapping type的。搜索的参数可做为URI请求参数给出,也可用 request body 给出。正则表达式

URI Searchspring

URI 搜索方式经过URI参数来指定查询相关参数。让咱们能够快速作一个查询。apache

GET /twitter/_search?q=user:kimchy

可用的参数请参考: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-uri-request.htmlapi

URI中容许的参数是:
q 
查询字符串(映射到query_string查询,请参阅 查询字符串查询以获取更多详细信息)。
df 
在查询中未定义字段前缀时使用的默认字段。
analyzer 
分析查询字符串时要使用的分析器名称。
analyze_wildcard 
是否应该分析通配符和前缀查询。默认为false。
batched_reduce_size 
一次在协调节点上应该减小的分片结果的数量。若是请求中的潜在分片数量可能很大,则应将此值用做保护机制以减小每一个搜索请求的内存开销。
default_operator 
要使用的默认运算符能够是AND或 OR。默认为OR。
lenient 
若是设置为true,则会致使基于格式的失败(如向数字字段提供文本)被忽略。默认为false。
explain 
对于每一个命中,包含如何计算命中得分的解释。
_source 
设置为false禁用检索_source字段。您也可使用_source_include&获取部分文档_source_exclude(请参阅请求主体 文档以获取更多详细信息)
stored_fields 
选择性存储的文件字段为每一个命中返回,逗号分隔。没有指定任何值将致使没有字段返回。
sort 
排序以执行。能够是fieldName,或者是 fieldName:asc的形式fieldName:desc。fieldName能够是文档中的实际字段,也能够是_score根据分数表示排序的特殊名称。能够有几个sort参数(顺序很重要)。
track_scores 
排序时,设置为true仍然能够跟踪分数并将它们做为每次击中的一部分返回。
timeout 
搜索超时,限制在指定时间值内执行的搜索请求,并在到期时积累至该点的保留时间。默认没有超时。
terminate_after 
为每一个分片收集的文档的最大数量,一旦达到该数量,查询执行将提早终止。若是设置,则响应将有一个布尔型字段terminated_early来指示查询执行是否实际已经terminate_early。缺省为no terminate_after。
from 
从命中的索引开始返回。默认为0。
size 
要返回的点击次数。默认为10。
search_type 
要执行的搜索操做的类型。能够是 dfs_query_then_fetch或query_then_fetch。默认为query_then_fetch。有关能够执行的不一样搜索类型的更多详细信息,请参阅 搜索类型。

查询结果说明数组

{
    "took": 1,               耗时(毫秒)
    "timed_out": false,      是否超时
    "_shards":{              查询了多少个分片
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
    },
    "hits":{                 命中结果
        "total" : 1,         总命中数
        "max_score": 1.3862944,  最高得分
        "hits" : [                本页结果文档数组
            {
                "_index" : "twitter",  文档
                "_type" : "_doc",
                "_id" : "0",
                "_score": 1.3862944,
                "_source" : {
                    "user" : "kimchy",
                    "message": "trying out Elasticsearch",
                    "date" : "2009-11-15T14:12:12",
                    "likes" : 0
                }            }        ]    }}

特殊的查询参数用法缓存

若是咱们只想知道有多少文档匹配某个查询,能够这样用参数:并发

GET /bank/_search?q=city:b*&size=0

若是咱们只想知道有没有文档匹配某个查询,能够这样用参数:app

GET /bank/_search?q=city:b*&size=0&terminate_after=1

比较两个查询的结果,有什么区别。

Request  body Search

Request body 搜索方式以JSON格式在请求体中定义查询 query。请求方式能够是 GET 、POST 。

GET /twitter/_search
{
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

可用的参数:

timeout:请求超时时长,限定在指定时长内响应(即便没查完);
from: 分页的起始行,默认0;
size:分页大小;
request_cache:是否缓存请求结果,默认true。
terminate_after:限定每一个分片取几个文档。若是设置,则响应将有一个布尔型字段terminated_early来指示查询执行是否实际已经terminate_early。缺省为no terminate_after;
search_type:查询的执行方式,可选值dfs_query_then_fetch or query_then_fetch ,默认: query_then_fetch ;
batched_reduce_size:一次在协调节点上应该减小的分片结果的数量。若是请求中的潜在分片数量可能很大,则应将此值用做保护机制以减小每一个搜索请求的内存开销。

query 元素定义查询

query 元素用Query DSL 来定义查询。

GET /_search
{
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

指定返回哪些内容

source filter  对_source字段进行选择

GET /_search
{
    "_source": false,
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}
GET /_search
{
    "_source": [ "obj1.*", "obj2.*" ],
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}
GET /_search
{
    "_source": "obj.*",
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}
GET /_search
{
    "_source": {
        "includes": [ "obj1.*", "obj2.*" ],
        "excludes": [ "*.description" ]
    },
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

stored_fields    来指定返回哪些stored字段

GET /_search
{
    "stored_fields" : ["user", "postDate"],
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

docValue Field    返回存储了docValue的字段值

GET /_search
{
    "query" : {
        "match_all": {}
    },
    "docvalue_fields" : ["test1", "test2"]
}

version    来指定返回文档的版本字段

GET /_search
{
    "version": true,
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

explain 返回文档的评分解释

GET /_search
{
    "explain": true,
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

Script Field 用脚原本对命中的每一个文档的字段进行运算后返回

GET /bank/_search
{
  "query": {
    "match_all": {}
  },
  "script_fields": {
    "test1": {
      "script": {
        "lang": "painless",
        "source": "doc['balance'].value * 2"   doc指文档
      }
    },
    "test2": {
      "script": {
        "lang": "painless",
        "source": "doc['age'].value * params.factor",
        "params": {
          "factor": 2
        }
      }
    } }}
GET /bank/_search
{
  "query": {
    "match_all": {}
  },
  "script_fields": {
    "ffx": {
      "script": {
        "lang": "painless",
        "source": "doc['age'].value * doc['balance'].value"
      }
    },
    "balance*2": {
      "script": {
        "lang": "painless",
        "source": "params['_source'].balance*2"   params  _source 取 _source字段值
      }                                           官方推荐使用doc,理由是用doc效率比取_source 高。
    }
  }
}

过滤 

min_score  限制最低评分得分。

GET /_search
{
    "min_score": 0.5,
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

post_filter  后置过滤:在查询命中文档、完成聚合后,再对命中的文档进行过滤。

如:要在一次查询中查询品牌为gucci且颜色为红色的shirts,同时还要获得gucci品牌各颜色的shirts的分面统计。

PUT /shirts
{
    "mappings": {
        "_doc": {
            "properties": {
                "brand": { "type": "keyword"},
                "color": { "type": "keyword"},
                "model": { "type": "keyword"}
            }
        }
    }
}
PUT /shirts/_doc/1?refresh
{
    "brand": "gucci",
    "color": "red",
    "model": "slim"
}
PUT /shirts/_doc/2?refresh
{
    "brand": "gucci",
    "color": "green",
    "model": "seec"
}
GET /shirts/_search
{
  "query": {
    "bool": {
      "filter": {
        "term": { "brand": "gucci" } 
      }
    }
  },
  "aggs": {
    "colors": {
      "terms": { "field": "color" } 
    }
  },
  "post_filter": { 
    "term": { "color": "red" }
  }
}

sort  排序

能够指定按一个或多个字段排序。也可经过_score指定按评分值排序,_doc 按索引顺序排序。默认是按相关性评分从高到低排序。

GET /bank/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [           order 值:asc、desc。若是不给定,默认是asc,_score默认是desc
    {
      "age": {
        "order": "desc"
      }    },
    {
      "balance": {
        "order": "asc"
      }    },
    "_score"
  ]
}
"hits": {
    "total": 1000,
    "max_score": null,
    "hits": [
      {
        "_index": "bank",
        "_type": "_doc",
        "_id": "549",
        "_score": 1,
        "_source": {
          "account_number": 549,
          "balance": 1932, "age": 40, "state": "OR"
        },
        "sort": [              结果中每一个文档会有排序字段值给出
          40,
          1932,
          1
        ]    }

多值字段排序

对于值是数组或多值的字段,也可进行排序,经过mode参数指定按多值的:

PUT /my_index/_doc/1?refresh
{
   "product": "chocolate",
   "price": [20, 4]
}

POST /_search
{
   "query" : {
      "term" : { "product" : "chocolate" }
   },
   "sort" : [
      {"price" : {"order" : "asc", "mode" : "avg"}}
   ]
}

Missing values  缺失该字段的文档

GET /_search
{
    "sort" : [
        { "price" : {"missing" : "_last"} }
    ],
    "query" : {
        "term" : { "product" : "chocolate" }
    }
}

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html#geo-sorting

地理空间距离排序

GET /_search
{
    "sort" : [
        {
            "_geo_distance" : {
                "pin.location" : [-70, 40],
                "order" : "asc",
                "unit" : "km",
                "mode" : "min",
                "distance_type" : "arc"
            }
        }
    ],
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

_geo_distance   距离排序关键字
pin.location是 geo_point 类型的字段
distance_type:距离计算方式 arc球面 、plane 平面。
unit: 距离单位 km 、m   默认m


Script Based Sorting   基于脚本计算的排序

GET /_search
{
    "query" : {
        "term" : { "user" : "kimchy" }
    },
    "sort" : {
        "_script" : {
            "type" : "number",
            "script" : {
                "lang": "painless",
                "source": "doc['field_name'].value * params.factor",
                "params" : {
                    "factor" : 1.1
                }
            },
            "order" : "asc"
        }
    }
}

折叠 

用 collapse指定根据某个字段对命中结果进行折叠

GET /bank/_search
{
    "query": {
        "match_all": {}
    },
    "collapse" : {
        "field" : "age" 
    },
    "sort": ["balance"] 
}
GET /bank/_search
{
    "query": {
        "match_all": {}
    },
    "collapse" : {
        "field" : "age" ,
        "inner_hits": {                 指定inner_hits来解释折叠
            "name": "details",          自命名
            "size": 5,                  指定每组取几个文档
            "sort": [{ "balance": "asc" }]   组内排序
        },
        "max_concurrent_group_searches": 4   指定组查询的并发数
    },
    "sort": ["balance"] 
}

在inner_hits 中返回多个角度的组内topN

GET /twitter/_search
{
    "query": {
        "match": {
            "message": "elasticsearch"
        }
    },
    "collapse" : {
        "field" : "user", 
        "inner_hits": [
            {
                "name": "most_liked",  
                "size": 3,
                "sort": ["likes"]
            },
            {
                "name": "most_recent", 
                "size": 3,
                "sort": [{ "date": "asc" }]
            }
        ]
    },
    "sort": ["likes"]
}

分页

from and size

GET /_search
{
    "from" : 0, "size" : 10,
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}

注意:搜索请求耗用的堆内存和时间与 from + size 大小成正比。分页越深耗用越大,为了避免因分页致使OOM或严重影响性能,ES中规定from + size 不能大于索引setting参数 index.max_result_window 的值,默认值为 10,000。

须要深度分页, 不受index.max_result_window 限制,怎么办?

 

Search after  在指定文档后取文档, 可用于深度分页

GET twitter/_search
{
    "size": 10,               首次查询第一页
    "query": {
        "match" : {
            "title" : "elasticsearch"
        }
    },
    "sort": [
        {"date": "asc"},
        {"_id": "desc"}
    ]
}
GET twitter/_search
{
    "size": 10,                     后续页的查询
    "query": {
        "match" : {
            "title" : "elasticsearch"
        }
    },
    "search_after": [1463538857, "654323"],
    "sort": [
        {"date": "asc"},
        {"_id": "desc"}
    ]
}

注意:使用search_after,要求查询必须指定排序,而且这个排序组合值每一个文档惟一(最好排序中包含_id字段)。 search_after的值用的就是这个排序值。 用search_after时 from 只能为0、-1。

高亮

PUT /hl_test/_doc/1
{
  "title": "lucene solr and elasticsearch",
  "content": "lucene solr and elasticsearch for search"
}
GET /hl_test/_search
{
  "query": {
    "match": {
      "title": "lucene"
    }
  },
  "highlight": {
    "fields": {
      "title": {},
      "content": {}
    }
  }
}
GET /hl_test/_search
{
  "query": {
    "match": {
      "title": "lucene"
    }
  },
  "highlight": {
    "require_field_match": false,
    "fields": {
      "title": {},
      "content": {}
    }
  }
}

高亮结果在返回的每一个文档中以hightlight节点给出

"highlight": {
  "title": [
	"<em>lucene</em> solr and elaticsearch"
  ]}

 

GET /hl_test/_search
{
  "query": {
    "match": {
      "title": "lucene"
    }
  },
  "highlight": {
    "require_field_match": false,
    "fields": {
      "title": {                   指定高亮标签
        "pre_tags":["<strong>"],
        "post_tags": ["</strong>"]
      },
      "content": {}
    }
  }
}

Profile  为了调试、优化

对于执行缓慢的查询,咱们很想知道它为何慢,时间都耗在哪了,能够在查询上加入上 profile 来得到详细的执行步骤、耗时信息。

GET /twitter/_search
{
  "profile": true,
  "query" : {
    "match" : { "message" : "some number" }
  }
}

信息的说明请参考:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-profile.html

count  api   查询数量

PUT /twitter/_doc/1?refresh
{
    "user": "kimchy"
}

GET /twitter/_doc/_count?q=user:kimchy

GET /twitter/_doc/_count
{
    "query" : {
        "term" : { "user" : "kimchy" }
    }
}
{
    "count" : 1,
    "_shards" : {
        "total" : 5,
        "successful" : 5,
        "skipped" : 0,
        "failed" : 0
    }
}

validate api

用来检查咱们的查询是否正确,以及查看底层生成查询是怎样的。

GET twitter/_validate/query?q=user:foo
GET twitter/_doc/_validate/query
{
  "query": {                 校验查询
    "query_string": {
      "query": "post_date:foo",
      "lenient": false
    }
  }
}
GET twitter/_doc/_validate/query?explain=true
{
  "query": {                 得到查询解释
    "query_string": {
      "query": "post_date:foo",
      "lenient": false
    }
  }
}
GET twitter/_doc/_validate/query?rewrite=true
{
  "query": {
    "more_like_this": {
      "like": {                   用rewrite得到比explain 更详细的解释
        "_id": "2"
      },
      "boost_terms": 1
    }
  }
}
GET twitter/_doc/_validate/query?rewrite=true&all_shards=true
{
  "query": {                     得到全部分片上的查询解释
    "match": {
      "user": {
        "query": "kimchy",
        "fuzziness": "auto"
      }
    }
  }
}

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-validate.html

Explain api

得到某个查询的评分解释,及某个文档是否被这个查询命中

GET /twitter/_doc/0/_explain
{
      "query" : {
        "match" : { "message" : "elasticsearch" }
      }
}

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-explain.html

Search Shards API

让咱们能够了解可执行查询的索引分片节点状况

GET /twitter/_search_shards

想知道指定routing值的查询将在哪些分片节点上执行

GET /twitter/_search_shards?routing=foo,baz

Search Template

POST _scripts/<templatename>
{
    "script": {
        "lang": "mustache",
        "source": {
            "query": {
                "match": {
                    "title": "{{query_string}}"
                }
            }
        }
    }
}
注册一个模板
GET _search/template
{
    "id": "<templateName>", 
    "params": {
        "query_string": "search for these words"
    }
}
使用模板进行查询

详细了解请参考官网:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-template.html

 

Query DSL

官网介绍连接:

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html

DSL是什么?

Domain Specific Language:领域特定语言

Elasticsearch基于JSON提供完整的查询DSL来定义查询。

一个查询可由两部分字句构成:

1.Leaf query clauses 叶子查询字句
   Leaf query clauses 在指定的字段上查询指定的值, 如:match, term or range queries. 叶子字句能够单独使用. 
2.Compound query clauses  复合查询字句
   以逻辑方式组合多个叶子、复合查询为一个查询

Query and filter context

一个查询字句的行为取决于它是用在query context  仍是 filter context 中 。

Query context  查询上下文

用在查询上下文中的字句回答“这个文档有多匹配这个查询?”。除了决定文档是否匹配,字节匹配的文档还会计算一个字节评分,来评定文档有多匹配。查询上下文由  query 元素表示。

Filter context  过滤上下文

过滤上下文由 filter 元素或 bool 中的 must not 表示。用在过滤上下文中的字节回答“这个文档是否匹配这个查询?”,不参与相关性评分。

被频繁使用的过滤器将被ES自动缓存,来提升查询性能。

GET /_search
{
  "query": {          查询
    "bool": { 
      "must": [
        { "match": { "title":   "Search"        }}, 
        { "match": { "content": "Elasticsearch" }}  
      ],
      "filter": [     过滤
        { "term":  { "status": "published" }}, 
        { "range": { "publish_date": { "gte": "2015-01-01" }}} 
      ]
    }
  }
}
提示:在查询上下文中使用查询子句来表示影响匹配文档得分的条件,并在过滤上下文中使用全部其余查询子句。

官网介绍连接:

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html

Match all query

查询全部

GET /_search
{
    "query": {
        "match_all": {}
    }
}
GET /_search
{
    "query": {
        "match_none": {}
    }
}

Full text querys

全文查询,用于对分词的字段进行搜索。会用查询字段的分词器对查询的文本进行分词生成查询。可用于短语查询、模糊查询、前缀查询、临近查询等查询场景

https://www.elastic.co/guide/en/elasticsearch/reference/current/full-text-queries.html

match query

全文查询的标准查询,它能够对一个字段进行模糊、短语查询。 match queries 接收 text/numerics/dates, 对它们进行分词分析, 再组织成一个boolean查询。可经过operator 指定bool组合操做(or、and 默认是 or ), 以及minimum_should_match 指定至少需多少个should(or)字句需知足。还可用ananlyzer指定查询用的特殊分析器。

GET /_search
{
    "query": {
        "match" : {
            "message" : "this is a test"
        }
    }
}

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html

match query  示例

PUT /ftq/_doc/1
{
  "title": "lucene solr and elasticsearch",
  "content": "lucene solr and elasticsearch for search"
}

PUT /ftq/_doc/2
{
  "title": "java spring boot",
  "content": "lucene is writerd by java"
}
GET ftq/_search
{
  "query": {
    "match": {
      "title": "lucene java"
    }
  }
}
GET ftq/_doc/_validate/query?rewrite=true
{
  "query": {             看看执行的查询
    "match": {
      "title": "lucene java"
    }
  }
}
GET ftq/_search
{
  "query": {
    "match": {
      "title": {
        "query": "lucene java",
        "operator": "and"
      }
    }
  }
}
GET ftq/_search
{
  "query": {
    "match": {
      "title": {
        "query": "ucen elatic",
        "fuzziness": 2                模糊查询,最大编辑数为2
      }
    }
  }
}
GET ftq/_search
{
  "query": {
    "match": {
      "content": {
        "query": "ucen elatic java",
        "fuzziness": 2,
        "minimum_should_match": 2     指定最少需知足两个词匹配
      }
    }
  }
}

可用max_expansions 指定模糊匹配的最大词项数,默认是50。好比:反向索引中有 100 个词项与 ucen 模糊匹配,只选用前50 个。

match  phrase  query

match_phrase 查询用来对一个字段进行短语查询,能够指定 analyzer、slop移动因子。

GET ftq/_search
{
  "query": {
    "match_phrase": {
      "title": "lucene solr"
    }
  }
}
GET ftq/_search
{
  "query": {
    "match_phrase": {
      "title": "lucene elasticsearch"
    }
  }
}
GET ftq/_search
{
  "query": {
    "match_phrase": {
      "title": {
        "query": "lucene elasticsearch",
        "slop": 2
      }
    }
  }
}

match  phrase  prefix query

match_phrase_prefix  在 match_phrase 的基础上支持对短语的最后一个词进行前缀匹配

GET /_search
{
    "query": {
        "match_phrase_prefix" : {
            "message" : "quick brown f"
        }
    }
}
GET /_search
{
    "query": {
        "match_phrase_prefix" : {
            "message" : {
                "query" : "quick brown f",
                "max_expansions" : 10
            }
        }
    }
}
指定前缀匹配选用的最大词项数量

Multi match  query

若是你须要在多个字段上进行文本搜索,可用multi_match 。 multi_match在 match的基础上支持对多个字段进行文本查询。

GET ftq/_search
{
  "query": {
    "multi_match" : {
      "query":    "lucene java", 
      "fields": [ "title", "content" ] 
    }
  }
}
GET ftq/_search?explain=true
{
  "query": {
    "multi_match" : {
      "query":    "lucene elastic", 
      "fields": [ "title^5", "content" ]   //给字段的相关性评分加权重
    }
  }
}
GET ftq/_search
{
  "query": {
    "multi_match" : {
      "query":    "lucene java", 
      "fields": [ "title", "cont*" ] 
    }
  }
}

Common terms query

common 经常使用词查询

问一、什么是停用词?索引时作停用词处理的目的是什么?

问二、若是在索引时应用停用词处理,下面的两个查询会查询什么词项?
    the brown fox
    not happy

问三、索引时应用停用词处理对搜索精度是否有影响?若是不作停用词处理又会有什么影响?如何协调这两个问题?如何保证搜索的精确度又兼顾搜索性能?

tf-idf 相关性计算模型简介

tf:term frequency   词频 :指一个词在一篇文档中出现的频率。

如“世界杯”在文档A中出现3次,那么能够定义“世界杯”在文档A中的词频为3。请问在一篇3000字的文章中出现“世界杯”3次和一篇150字的文章中出现3词,哪篇文章更是与“世界杯”有关的。也就是说,简单用出现次数做为频率不够准确。那就用占比来表示:

问:tf值越大是否就必定说明这个词更相关?

说明:tf的计算不必定非是这样的,能够定义不一样的计算方式。

df:document frequency   词的文档频率 :指包含某个词的文档数(有多少文档中包含这个词)。 df越大的词越常见,哪些词会是高频词?

问1:词的df值越大说明这个词在这个文档集中是越重要仍是越不重要?

问2:词t的tf高,在文档集中的重要性也高,是否说明文档与该词越相关?举例:整个文档集中只有3篇文档中有“世界杯”,文档A中就出现了“世界级”好几回

问3:如何用数值体现词t在文档集中的重要性?df能够吗?

用 文档总数 / df 能够吗?

idf:inverse document frequency   词的逆文档频率 :用来表示词在文档集中的重要性。文档总数/ df ,df越小,词越重要,这个值会很大,那就对它取个天然对数,将值映射到一个较小的取值范围。

说明: +1 是为了不除0(即词t在文档集中未出现的状况)

tf-idf 相关性性计算模型:

Common terms query

common 区分经常使用(高频)词查询让咱们能够经过cutoff_frequency来指定一个分界文档频率值,将搜索文本中的词分为高频词和低频词,低频词的重要性高于高频词,先对低频词进行搜索并计算全部匹配文档相关性得分;而后再搜索和高频词匹配的文档,这会搜到不少文档,但只对和低频词重叠的文档进行相关性得分计算(这可保证搜索精确度,同时大大提升搜索性能),和低频词累加做为文档得分。实际执行的搜索是 必须包含低频词 + 或包含高频词。

思考:这样处理下,若是用户输入的都是高频词如 “to be or not to be”结果会是怎样的?你但愿是怎样的?

优化:若是都是高频词,那就对这些词进行and 查询。

进一步优化:让用户能够本身定对高频词作and/or 操做,本身定对低频词进行and/or  操做;或指定最少得多少个同时匹配。

Common terms query

GET /_search
{
    "query": {
        "common": {
            "message": {
                "query": "this is bonsai cool",
                "cutoff_frequency": 0.001
            }
        }
    }
}
cutoff_frequency : 值大于1表示文档数,0-1.0表示占比。
此处界定 文档频率大于 0.1%的词为高频词。
GET /_search
{
    "query": {
        "common": {
            "body": {
                "query": "nelly the elephant as a cartoon",
                "cutoff_frequency": 0.001,
                "low_freq_operator": "and"
            }
        }
    }
}

可用参数:minimum_should_match (high_freq, low_freq), low_freq_operator (default “or”) and high_freq_operator (default “or”)、 boost and analyzer

GET /_search
{
    "query": {
        "common": {
            "body": {
                "query": "nelly the elephant as a cartoon",
                "cutoff_frequency": 0.001,
                "minimum_should_match": 2
            }
        }
    }
}
GET /_search
{
    "query": {
        "common": {
            "body": {
                "query": "nelly the elephant not as a cartoon",
                "cutoff_frequency": 0.001,
                "minimum_should_match": {
                    "low_freq" : 2,
                    "high_freq" : 3
                }
            }
        }
    }
}
GET /_search
{
    "query": {
        "common": {
            "body": {
                "query": "how not to be",
                "cutoff_frequency": 0.001,
                "minimum_should_match": {
                    "low_freq" : 2,
                    "high_freq" : 3
                }
            }
        }
    }
}
粗略等于右边的查询
GET /_search
{
    "query": {
        "bool": {
            "should": [
            { "term": { "body": "how"}},
            { "term": { "body": "not"}},
            { "term": { "body": "to"}},
            { "term": { "body": "be"}}
            ],
            "minimum_should_match": "3<50%"
        }
    }
}

Query string query

query_string 查询,让咱们能够直接用lucene查询语法写一个查询串进行查询,ES中接到请求后,经过查询解析器解析查询串生成对应的查询。使用它要求掌握lucene的查询语法。

GET /_search
{
    "query": {
        "query_string" : {
            "default_field" : "content",
            "query" : "this AND that OR thus"
        }
    }
}
GET /_search
{
    "query": {
        "query_string" : {
            "fields" : ["content", "name.*^5"],
            "query" : "this AND that OR thus"
        }
    }
}

可与query同用的参数,如 default_field、fields,及query 串的语法请参考:

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html

查询描述规则语法(查询解析语法):

Term 词项:

单个词项的表示:     电脑
短语的表示:     "联想笔记本电脑"

Field 字段:

字段名:
示例: name:“联想笔记本电脑” AND type:电脑
若是name是默认字段,则可写成: “联想笔记本电脑” AND type:电脑
若是查询串是:type:电脑 计算机 手机
注意:只有第一个是type的值,后两个则是使用默认字段。

Term Modifiers 词项修饰符:

统配符:

?    单个字符
*    0个或多个字符
示例:te?t    test*    te*t
注意:通配符不可用在开头。

模糊查询 : 词后加 ~

示例:     roam~
模糊查询最大支持两个不一样字符。
示例:  roam~1

正则表达式:   /xxxx/

示例:     /[mb]oat/

临近查询 : 短语后加 ~移动值

示例: "jakarta apache"~10

范围查询:

mod_date:[20020101 TO 20030101]       包含边界值
title:{Aida TO Carmen}      不包含边界值

词项加权 : 使该词项的相关性更高,经过 ^数值来指定加权因子,默认加权因子值是1

示例:如要搜索包含 jakarta apache 的文章,jakarta更相关,则:
jakarta^4 apache
短语也能够: "jakarta apache"^4 "Apache Lucene"

查询描述规则语法(查询解析语法):

Boolean 操做符        Lucene支持的布尔操做: AND, “+”, OR, NOT ,"-"

Simple Query string query

simple_query_string 查同 query_string 查询同样用lucene查询语法写查询串,较query_string不一样的地方:更小的语法集;查询串有错误,它会忽略错误的部分,不抛出错误。更适合给用户使用。

GET /_search
{
  "query": {
    "simple_query_string" : {
        "query": "\"fried eggs\" +(eggplant | potato) -frittata",
        "fields": ["title^5", "body"],
        "default_operator": "and"
    }
  }
}

语法请参考:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html

Term level querys

https://www.elastic.co/guide/en/elasticsearch/reference/current/term-level-queries.html

Term query

term 查询用于查询指定字段包含某个词项的文档。

POST _search
{
  "query": {
    "term" : { "user" : "Kimchy" } 
  }
}
GET _search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "status": {
              "value": "urgent",
              "boost": 2.0                  加权重
            }
          }
        },
        {
          "term": {
            "status": "normal" 
          }
        }      ]    }  }}

terms 查询用于查询指定字段包含某些词项的文档。

GET /_search
{
    "query": {
        "terms" : { "user" : ["kimchy", "elasticsearch"]}
    }
}

Terms 查询支持嵌套查询的方式来得到查询词项,至关于 in (select term from other)

Terms query  嵌套查询示例

PUT /users/_doc/2
{
    "followers" : ["1", "3"]
}

PUT /tweets/_doc/1
{
    "user" : "1"
}

GET /tweets/_search
{
    "query" : {
        "terms" : {
            "user" : {
                "index" : "users",
                "type" : "_doc",
                "id" : "2",
                "path" : "followers"
            }
        }    }}

嵌套查询可用参数说明:

range query

GET _search
{
    "query": {
        "range" : {
            "age" : {
                "gte" : 10,
                "lte" : 20,
                "boost" : 2.0
            }
        }
    }
}
GET _search
{
    "query": {
        "range" : {
            "date" : {
                "gte" : "now-1d/d",
                "lt" :  "now/d"
            }
        }
    }
}
GET _search
{
    "query": {
        "range" : {
            "born" : {
                "gte": "01/01/2012",
                "lte": "2013",
                "format": "dd/MM/yyyy||yyyy"
            }
        }
    }
}

时间舍入 ||说明:

时间数学计算规则请参考:

https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#date-math

exists  query

查询指定字段值不为空的文档。至关 SQL 中的  column is not null

GET /_search
{
    "query": {
        "exists" : { "field" : "user" }
    }
}
GET /_search
{
    "query": {             查询指定字段值为空的文档
        "bool": {
            "must_not": {
                "exists": {
                    "field": "user"
                }
            }        }    }}

prefix  query   词项前缀查询

GET /_search
{ "query": {
    "prefix" : { "user" : "ki" }
  }
}
GET /_search
{ "query": {
    "prefix" : { "user" :  { "value" : "ki", "boost" : 2.0 } }
  }
}

wildcard  query   通配符查询:  ?  *

GET /_search
{
    "query": {
        "wildcard" : { "user" : "ki*y" }
    }
}
GET /_search
{
  "query": {
    "wildcard": {
      "user": {
        "value": "ki*y",
        "boost": 2
      }
    }
  }}

regexp query   正则查询

GET /_search
{
    "query": {
        "regexp":{
            "name.first": "s.*y"
        }
    }
}
GET /_search
{
    "query": {
        "regexp":{
            "name.first":{
                "value":"s.*y",
                "boost":1.2
            }
        }
    }
}

正则语法请参考:

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html#regexp-syntax

fuzzy query   模糊查询

GET /_search
{
    "query": {
       "fuzzy" : { "user" : "ki" }
    }
}
GET /_search
{
    "query": {
        "fuzzy" : {
            "user" : {
                "value": "ki",
                "boost": 1.0,
                "fuzziness": 2,
                "prefix_length": 0,
                "max_expansions": 100
            }
        }
    }
}

type query   mapping type 查询

GET /_search
{
    "query": {
        "type" : {
            "value" : "_doc"
        }
    }
}

ids query   根据文档id查询

GET /_search
{
    "query": {
        "ids" : {
            "type" : "_doc",
            "values" : ["1", "4", "100"]
        }
    }
}

Compound querys 复合查询

https://www.elastic.co/guide/en/elasticsearch/reference/current/compound-queries.html

Constant Score query

用来包装另外一个查询,将查询匹配的文档的评分设为一个常值。

GET /_search
{
    "query": {
        "constant_score" : {
            "filter" : {
                "term" : { "user" : "kimchy"}
            },
            "boost" : 1.2
        }
    }
}

Bool query

Bool 查询用bool操做来组合多个查询字句为一个查询。 可用的关键字:

POST _search
{
  "query": {
    "bool" : {
      "must" : {
        "term" : { "user" : "kimchy" }
      },
      "filter": {
        "term" : { "tag" : "tech" }
      },
      "must_not" : {
        "range" : {
          "age" : { "gte" : 10, "lte" : 20 }
        }
      },
      "should" : [
        { "term" : { "tag" : "wow" } },
        { "term" : { "tag" : "elasticsearch" } }
      ],
      "minimum_should_match" : 1,
      "boost" : 1.0
    }
  }
}