【问题标题】:How do I match entries in the elasticsearch containing dashes (-)如何匹配包含破折号 (-) 的弹性搜索中的条目
【发布时间】:2019-07-22 23:41:10
【问题描述】:

ElasticSearch 对数据进行标记。 所以“这个-那个”被分成 2 个标记。 如果有区别,我使用的是 6.6 版本的 ES。 我有文档,它们具有不同的字段,例如标题、描述、名称等。 为了让我们有一个唯一的标识符,标题“这个那个”中的文本被翻译成“这个-那个”。 我正在尝试查询将返回该文档的“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”的所有实例。 我只想取回一份文件!

【问题讨论】:

  • 你能在shows索引上发布你的配置吗?
  • 如果您使用默认映射,请将name 替换为name.keyword,否则更新映射以添加keyword 字段或更改字段的分析器。
  • 这个answer 可能会对你有所帮助。
  • $params = [ 'index' => 'shows', 'body' => [ 'settings' => [ 'number_of_shards' => 3, 'number_of_replicas' => 0, 'index. mapping.ignore_malformed' => true ],
  • 'mappings' => [ '_doc' => [ 'date_detection' => false, 'properties' => [ 'pubdate' => [ 'type' => 'date', 'ignore_malformed ' => true ] , 'seasons' => [ 'properties' => [ 'episodes' => [ 'properties' => [ 'pubdate' => [ 'type' => 'date', 'ignore_malformed' =>真 ] ] ]

标签: php elasticsearch


【解决方案1】:

您可以使用 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"
      }
    }
  }
}

这与将字段定义为开头的关键字类型完全相同:

PUT test-index2
{
  "mappings": {
    "properties": {
      "name": {
        "type": "keyword"
      }
    }
  }
}

【讨论】:

    猜你喜欢
    • 2016-05-08
    • 1970-01-01
    • 1970-01-01
    • 2011-09-13
    • 1970-01-01
    • 1970-01-01
    • 2019-06-08
    • 1970-01-01
    • 2020-04-26
    相关资源
    最近更新 更多