gpt4 book ai didi

php - 如何匹配Elasticsearch中包含破折号(-)的条目

转载 作者:行者123 更新时间:2023-12-02 22:35:19 24 4
gpt4 key购买 nike

ElasticSearch标记数据。
因此,“this-that”被分为2个 token 。
如果有什么不同,我使用的是6.6版本的ES。
我有一些具有不同字段的文档,例如标题,描述,名称等。
为了使我们拥有唯一的标识符,标题“This that”中的文本被压缩为“this-that”。
我试图查询将返回该文档的“this-that”。
我尝试了各种各样的事情。我尝试了本论坛中其他问题的建议以及https://www.elastic.co/guide/en/elasticsearch/reference/current/analyzer.html页面中的说明。
不幸的是,似乎没有任何工作。
您的帮助将不胜感激。
先感谢您。

https://www.elastic.co/guide/en/elasticsearch/reference/current/analyzer.html

<?php
require 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;

$hosts = ['localhost:9200'];
$client = ClientBuilder::create()
->setHosts($hosts)
->build();

$params = array();

$params = [
'index' => 'shows',
'type' => '_doc',
'body' => [
'size'=> 10000,
'query' => [
'bool' => [
'must' => [ 'match' => [ 'name' => 'this-that'] ],
]
]
]
];

$response = $client->search($params);

print_r($response);
?>

没有错误,但是会找到名称字段中带有单词“this”和单词“that”的所有实例。
我只想拿回一份文件!

最佳答案

您可以使用Kibana控制台或http来尝试analyzerstokenizers:

curl -XPOST "http://localhost:9200/_analyze" -H 'Content-Type: application/json' -d'{  "analyzer": "standard",  "text": "this-that"}'
{
"tokens" : [
{
"token" : "this",
"start_offset" : 0,
"end_offset" : 4,
"type" : "<ALPHANUM>",
"position" : 0
},
{
"token" : "that",
"start_offset" : 5,
"end_offset" : 9,
"type" : "<ALPHANUM>",
"position" : 1
}
]
}
curl -XPOST "http://localhost:9200/_analyze" -H 'Content-Type: application/json' -d'{ "analyzer": "keyword", "text": "this-that"}'
{
"tokens" : [
{
"token" : "this-that",
"start_offset" : 0,
"end_offset" : 9,
"type" : "word",
"position" : 0
}
]
}

要始终与字段完全匹配,必须使用关键字标记。您可以这样做:
PUT test-index
{
"mappings": {
"properties": {
"name": {
"type": "text",
"analyzer": "keyword"
}
}
}
}

与将field定义为关键字类型开头完全相同:
PUT test-index2
{
"mappings": {
"properties": {
"name": {
"type": "keyword"
}
}
}
}

关于php - 如何匹配Elasticsearch中包含破折号(-)的条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57154978/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com