42.query string分词

主要知识点:

1queery string 分词

238节中搜索结果解析

3,测试分词器

   

query string分词

query string必须以和index建立时相同的analyzer进行分词

query stringexact valuefull text的区别对待

比如我们有一个document,其中有一个field,包含的value是:hello you and me,建立倒排索引。

我们要搜索这个document对应的index,搜索文本是hell me,这个搜索文本就是query string,默认情况下,es会使用它对应的field建立倒排索引时相同的分词器去进行分词和normalization,只有这样,才能实现正确的搜索。

比如,我们建立倒排索引的时候,将dogs --> dog,结果你搜索的时候,还是一个dogs,那不就搜索不到了吗?所以搜索的时候,那个dogs也必须变成dog才行。才能搜索到,所以必须是相同的分词器。不同类型的field,可能有的就是full text,有的就是exact value

   

二、38小节中对搜索结果的讲解

   

1GET /_search?q=2017

搜索的是_all fielddocument所有的field都会拼接成一个大串,进行分词,其中2017-01-02会进行分词成20170102,所以用_all 搜索2017时能把三个docuemnt都搜索出来。

   

2GET /_search?q=2017-01-01

同样道理,query string会用跟建立倒排索引一样的分词器去进行分词(会把2017-01-01 分成201701),再进行搜索时,3个文档都有2017,所以会是三个结果。

   

3GET /_search?q=post_date:2017-01-01

   

搜索特定的field,post_date,会作为exact value去建立索引,2017-01-01没有被分词,只有doc1中才有2017-01-01,所以只有一个结果

   

4GET /_search?q=post_date:2017

这是es 5.2以后所做的一个优化,具体原理以后会学到。

   

三、测试分词器

   

GET /_analyze

{

"analyzer": "standard",

"text": "Text to analyze"

}

执行结果是:

{

"tokens": [

{

"token": "text",

"start_offset": 0,

"end_offset": 4,

"type": "<ALPHANUM>",

"position": 0

},

{

"token": "to",

"start_offset": 5,

"end_offset": 7,

"type": "<ALPHANUM>",

"position": 1

},

{

"token": "analyze",

"start_offset": 8,

"end_offset": 15,

"type": "<ALPHANUM>",

"position": 2

}

]

}

   

   

原文地址:https://www.cnblogs.com/liuqianli/p/8469802.html