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

标签 php elasticsearch

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/

相关文章:

php - Mongo Map Reduce 第一次

php - 计算两个日期之间的日期差异

php - 使用 PDO 的更新语句是否根据 setFetchMode 准备返回数据?

php - SplPriorityQueue::next() 正在删除项目

java - 从ElasticSearch获取指定字段

elasticsearch - 问题设置集群elasticsearch,总是返回1个节点,配置有什么问题?

php - 是否可以在 phpExcel 的页眉或页脚中将文本放在图像下方?

elasticsearch - Elasticsearch非常长字符串的完全匹配性能

elasticsearch - Kibana 对移动设备不友好

elasticsearch - Elasticsearch使用数组字段索引multi_field