Rac*_*naa 16 search search-engine elasticsearch
我是弹性搜索的新手,我想使用同义词,我在配置文件中添加了这些行:
index :
analysis :
analyzer :
synonym :
type : custom
tokenizer : whitespace
filter : [synonym]
filter :
synonym :
type : synonym
synonyms_path: synonyms.txt
Run Code Online (Sandbox Code Playgroud)
然后我创建了一个索引测试:
"mappings" : {
"test" : {
"properties" : {
"text_1" : {
"type" : "string",
"analyzer" : "synonym"
},
"text_2" : {
"search_analyzer" : "standard",
"index_analyzer" : "synonym",
"type" : "string"
},
"text_3" : {
"type" : "string",
"analyzer" : "synonym"
}
}
}
Run Code Online (Sandbox Code Playgroud)
}
并用这些数据进行了类型测试:
{
"text_3" : "foo dog cat",
"text_2" : "foo dog cat",
"text_1" : "foo dog cat"
}
Run Code Online (Sandbox Code Playgroud)
synonyms.txt包含"foo,bar,baz",当我搜索foo时,它会返回我的预期,但是当我搜索baz或bar时,它会返回零结果:
{
"query":{
"query_string":{
"query" : "bar",
"fields" : [ "text_1"],
"use_dis_max" : true,
"boost" : 1.0
}}}
Run Code Online (Sandbox Code Playgroud)
结果:
{
"took":1,
"timed_out":false,
"_shards":{
"total":5,
"successful":5,
"failed":0
},
"hits":{
"total":0,
"max_score":null,
"hits":[
]
}
}
Run Code Online (Sandbox Code Playgroud)
mol*_*are 18
我不知道,如果你的问题是因为你定义了"bar"的同义词.正如你所说的那样,你很新我会举一个与你相似的例子.我想展示elasticsearch如何在搜索时和索引时处理同义词.希望能帮助到你.
首先创建同义词文件:
foo => foo bar, baz
Run Code Online (Sandbox Code Playgroud)
现在我使用您要测试的特定设置创建索引:
curl -XPUT 'http://localhost:9200/test/' -d '{
"settings": {
"index": {
"analysis": {
"analyzer": {
"synonym": {
"tokenizer": "whitespace",
"filter": ["synonym"]
}
},
"filter" : {
"synonym" : {
"type" : "synonym",
"synonyms_path" : "synonyms.txt"
}
}
}
}
},
"mappings": {
"test" : {
"properties" : {
"text_1" : {
"type" : "string",
"analyzer" : "synonym"
},
"text_2" : {
"search_analyzer" : "standard",
"index_analyzer" : "standard",
"type" : "string"
},
"text_3" : {
"type" : "string",
"search_analyzer" : "synonym",
"index_analyzer" : "standard"
}
}
}
}
}'
Run Code Online (Sandbox Code Playgroud)
请注意,synonyms.txt必须与配置文件位于同一目录中,因为该路径相对于config dir.
现在索引一个doc:
curl -XPUT 'http://localhost:9200/test/test/1' -d '{
"text_3": "baz dog cat",
"text_2": "foo dog cat",
"text_1": "foo dog cat"
}'
Run Code Online (Sandbox Code Playgroud)
现在搜索
在字段text_1中搜索
curl -XGET 'http://localhost:9200/test/_search?q=text_1:baz'
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.15342641,
"hits": [
{
"_index": "test",
"_type": "test",
"_id": "1",
"_score": 0.15342641,
"_source": {
"text_3": "baz dog cat",
"text_2": "foo dog cat",
"text_1": "foo dog cat"
}
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
你得到文件是因为baz是foo的同义词,在索引时foo用它的同义词扩展
在字段text_2中搜索
curl -XGET 'http://localhost:9200/test/_search?q=text_2:baz'
Run Code Online (Sandbox Code Playgroud)
结果:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
Run Code Online (Sandbox Code Playgroud)
我没有得到点击,因为我没有在索引(标准分析器)时扩展同义词.而且,因为我正在搜索baz和baz不在文本中,所以我没有得到任何结果.
在字段text_3中搜索
curl -XGET 'http://localhost:9200/test/_search?q=text_3:foo'
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.15342641,
"hits": [
{
"_index": "test",
"_type": "test",
"_id": "1",
"_score": 0.15342641,
"_source": {
"text_3": "baz dog cat",
"text_2": "foo dog cat",
"text_1": "foo dog cat"
}
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
注意:text_3是"baz dog cat"
text_3是没有扩展同义词的索引.当我正在搜索foo时,其中有"baz"作为同义词之一,我得到了结果.
如果要调试,可以使用_analyze端点,例如:
curl -XGET 'http://localhost:9200/test/_analyze?text=foo&analyzer=synonym&pretty=true'
Run Code Online (Sandbox Code Playgroud)
结果:
{
"tokens": [
{
"token": "foo",
"start_offset": 0,
"end_offset": 3,
"type": "SYNONYM",
"position": 1
},
{
"token": "baz",
"start_offset": 0,
"end_offset": 3,
"type": "SYNONYM",
"position": 1
},
{
"token": "bar",
"start_offset": 0,
"end_offset": 3,
"type": "SYNONYM",
"position": 2
}
]
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10661 次 |
| 最近记录: |