Elasticsearch查询字符串查询不能与同义词分析器一起使用(Elasticsearch Query String Query not working with synonym analyzer)
我正在尝试使用同义词配置弹性搜索。
这些是我的设置:
"analysis": { "analyzer": { "category_synonym": { "tokenizer": "whitespace", "filter": [ "synonym_filter" ] } }, "filter": { "synonym_filter": { "type": "synonym", "synonyms_path": "synonyms.txt" } } }
映射配置:
"category": { "properties": { "name": { "type":"string", "search_analyzer" : "category_synonym", "index_analyzer" : "standard", "fields": { "raw": { "type": "string", "index": "not_analyzed" } } } } }
以及我的同义词列表
film => video, ooh => panels , poster, commercial => advertisement, print => magazine
我必须说我正在使用Elasticsearch Java API。 我正在使用
QueryBuilders.queryStringQuery
因为这是我将分析器设置为我的请求的唯一方法。 所以,当我在做的时候:QueryBuilders.queryStringQuery("name:film").analyzer(analyzer)
它归还给我
[ { "id": 71, "name": "Pitch video", "description": "... ", "parent": null }, { "id": 25, "name": "Video", "description": "... ", "parent": null } ]
这对我来说是完美的,但是当我打电话的时候
QueryBuilders.queryStringQuery("name:vid").analyzer(analyzer)
我希望它应该返回相同的对象,但没有任何东西:
[]
所以,我在
queryStringQuery
添加了星号:QueryBuilders.queryStringQuery("name:vid*").analyzer(analyzer)
效果很好,但现在
QueryBuilders.queryStringQuery("name:film*").analyzer(analyzer)
给我回复
[]
那么,当我搜索
video
,vid
,film
和film
时,如何配置我的弹性搜索它将返回相同的对象?提前致谢!
I am trying to configure elastic search with synonyms.
These are my settings:
"analysis": { "analyzer": { "category_synonym": { "tokenizer": "whitespace", "filter": [ "synonym_filter" ] } }, "filter": { "synonym_filter": { "type": "synonym", "synonyms_path": "synonyms.txt" } } }
Mappings config:
"category": { "properties": { "name": { "type":"string", "search_analyzer" : "category_synonym", "index_analyzer" : "standard", "fields": { "raw": { "type": "string", "index": "not_analyzed" } } } } }
And the list of my synonyms
film => video, ooh => panels , poster, commercial => advertisement, print => magazine
I must say that I am using Elasticsearch Java API. I am using
QueryBuilders.queryStringQuery
because this is the only way how I set analyzers to my request. So, when I am making:QueryBuilders.queryStringQuery("name:film").analyzer(analyzer)
It returns me
[ { "id": 71, "name": "Pitch video", "description": "... ", "parent": null }, { "id": 25, "name": "Video", "description": "... ", "parent": null } ]
That is perfect for me, but when I am calling something like this
QueryBuilders.queryStringQuery("name:vid").analyzer(analyzer)
I expect that it should return same objects, but there is nothing:
[]
So, I added asterisk to
queryStringQuery
:QueryBuilders.queryStringQuery("name:vid*").analyzer(analyzer)
Works well, but now
QueryBuilders.queryStringQuery("name:film*").analyzer(analyzer)
returns me
[]
So, how can I configure my elastic search that it will return same objects when I am searching
video
,vid
,film
andfil
?Thanks in advance!
原文:https://stackoverflow.com/questions/43502362
最满意答案
连续显示两种方法:
1)使用额外的
explode
功能结合list
功能:$customstring = "Eye Width=3/4 in|Finish=Nickel|Hook Opening=7/16 in|Locking Type=Spring Loaded Plunger|Material=Zinc Die Cast|Mounting=Swivel Eye|Overall Length [Nom]=3 1/2 in|Type=Swiveled Securing Hook|Wt.=0.09 lb"; $pairs = explode("|", $customstring); $result = []; foreach ($pairs as $p) { list($k, $v) = explode('=', $p); $result[$k] = $v; } print_r($result);
2)另一种替代解决方案是使用
preg_match_all
和array_combine
函数:$customstring = "Eye Width=3/4 in|Finish=Nickel|Hook Opening=7/16 in|Locking Type=Spring Loaded Plunger|Material=Zinc Die Cast|Mounting=Swivel Eye|Overall Length [Nom]=3 1/2 in|Type=Swiveled Securing Hook|Wt.=0.09 lb"; preg_match_all("/([^=|]+)=([^+|]+)/", $customstring, $m); $result = array_combine($m[1], $m[2]); print_r($result);
输出(两种方法都相同):
Array ( [Eye Width] => 3/4 in [Finish] => Nickel [Hook Opening] => 7/16 in [Locking Type] => Spring Loaded Plunger [Material] => Zinc Die Cast [Mounting] => Swivel Eye [Overall Length [Nom]] => 3 1/2 in [Type] => Swiveled Securing Hook [Wt.] => 0.09 lb )
Two approaches shown consecutively:
1) Use additional
explode
function combined withlist
function:$customstring = "Eye Width=3/4 in|Finish=Nickel|Hook Opening=7/16 in|Locking Type=Spring Loaded Plunger|Material=Zinc Die Cast|Mounting=Swivel Eye|Overall Length [Nom]=3 1/2 in|Type=Swiveled Securing Hook|Wt.=0.09 lb"; $pairs = explode("|", $customstring); $result = []; foreach ($pairs as $p) { list($k, $v) = explode('=', $p); $result[$k] = $v; } print_r($result);
2) Another alternative solution would be using
preg_match_all
andarray_combine
functions:$customstring = "Eye Width=3/4 in|Finish=Nickel|Hook Opening=7/16 in|Locking Type=Spring Loaded Plunger|Material=Zinc Die Cast|Mounting=Swivel Eye|Overall Length [Nom]=3 1/2 in|Type=Swiveled Securing Hook|Wt.=0.09 lb"; preg_match_all("/([^=|]+)=([^+|]+)/", $customstring, $m); $result = array_combine($m[1], $m[2]); print_r($result);
The output(same for both approaches):
Array ( [Eye Width] => 3/4 in [Finish] => Nickel [Hook Opening] => 7/16 in [Locking Type] => Spring Loaded Plunger [Material] => Zinc Die Cast [Mounting] => Swivel Eye [Overall Length [Nom]] => 3 1/2 in [Type] => Swiveled Securing Hook [Wt.] => 0.09 lb )
相关问答
更多-
将数组转换为字符串?(Convert an array to string)[2023-12-20]
您可以使用以下方式加入您的阵列: string.Join(",", Client); 然后你可以输出任何你想要的。 您可以将逗号更改为任何您想要的内容,空格,管道等等。 You can join your array using the following: string.Join(",", Client); Then you can output anyway you want. You can change the comma to what ever you want, a space, a pi ... -
编码功能可以帮助你在这里,编码返回字符串的编码版本 In [44]: str = "ABCD" In [45]: [elem.encode("hex") for elem in str] Out[45]: ['41', '42', '43', '44'] 或者你可以使用数组模块 In [49]: import array In [50]: print array.array('B', "ABCD") array('B', [65, 66, 67, 68]) encode function can h ...
-
简短的回答是,如果您的编码算法确实是UTF-16字符串的按位否定,那么您无法打印输出。 该算法产生不可打印的输出。 您可以在显示时向base64添加一个额外的编码,以便在代码中转换为可显示的否定字节。 如果您需要更精确的帮助,我认为您需要解释背景以及您的动机是什么。 The short answer is that if your encode algorithm really is bitwise negation of a UTF-16 string, then you cannot print the ...
-
如果你的数组有尾随逗号,你可以使用下面的代码: var data = "[[1, 2, 3,], [3, 2, 1]]"; var array = JSON.parse(data.replace(/,\s*]/g, ']')); console.log(array); 如果你有这样的字符串: var data = "[1, 2, 3,], [3, 2, 1]"; 使用 var array = JSON.parse('[' + data.replace(/,\s*]/g, ']') + ']'); 例 ...
-
如何在rails应用程序的ruby中将数组输出转换为普通字符串(how to convert array output to normal string in ruby on rails application)[2022-05-15]
你看看pluck吗? 如果您只需要db中的一条记录(在您的情况下为'name'),这非常有用。 您可以使用它来执行此操作: a = article.tags.pluck(:name) 然后输出以空格分隔的文章名称,执行以下操作: a.join(" ") 为了完整起见,您可以链接这些方法(就像您在下面的评论中所说的那样),如下所示: article.tags.pluck(:name).join(" ") Did you look at pluck? This if very useful for if ... -
没有必要将它写入控制台并将其读回..简单地说: var units = Convert.ToDouble(product[1]); 您还可以考虑使用Double.TryParse()来检查该值是否可以转换为double并且不是字母串。 There's no need to write it to console and read it back.. simply: var units = Convert.ToDouble(product[1]); You might also consider usin ...
-
无法转换为Json字符串(Unable to convert to Json String)[2022-07-29]
当你使用JSON.parse(t)将t转换为x时,这会将boys.json变成javascript对象。 然而,当你检查b_json.x时,你需要从b_json中获取“x”属性 - 它很可能没有一个名为x的属性。 尝试console.log(b_json)来查看对象中的内容。 When you converted t into x using JSON.parse(t) this then turns what was is boys.json into a javascript object. Howev ... -
如果你想做的是转这个: String = "hello"; 进入这个: String[] h; String[] e; String[] l; String[] l; String[] o; 它不能做到。 Java在编译期间需要知道变量的名称,您不能根据一些随机输入来命名新变量。 但是,您可以定义所有可能的字母,然后仅返回所需的字母; 像这样的东西: final String[] LETTER_A = {" * ", " * * ", "*****", "* *", "* *"}; fina ...
-
试图将混合字符串转换为数组,但无法获得准确的输出(Trying to convert mix string into the array but unable to get exact output)[2022-10-13]
连续显示两种方法: 1)使用额外的explode功能结合list功能: $customstring = "Eye Width=3/4 in|Finish=Nickel|Hook Opening=7/16 in|Locking Type=Spring Loaded Plunger|Material=Zinc Die Cast|Mounting=Swivel Eye|Overall Length [Nom]=3 1/2 in|Type=Swiveled Securing Hook|Wt.=0.09 lb"; ... -
首先,这里不需要用$name做任何事情: $name = filter_input(INPUT_POST, 'name'); 在这里也不需要用$myarray做任何事情: $myarray = filter_input(INPUT_POST, 'myarray'); 或在这里: if($myarray === NULL){ $myarray = array(); } 由于您在此覆盖可能存在的任何内容: $myarray = $names; 在这里你覆盖$name : $name = expl ...