php - 如何在 elasticsearch 中使用 Unicode 字符进行搜索?

标签 php mysql elasticsearch unicode

我已经将 MySQL 列索引到 elasticsearch 中,并且该列有一些 AR/EN/RO 语言值。 如何使用 unicode 字符串在这些索引中进行搜索?

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

$body = '{  "query": {
"filtered": {
  "query": {
    "match_all": {}
  },
  "filter": {
    "bool": {
      "must": [
        {"query": {"wildcard": {"text": {"value": "*'.$term.'*"}}}},
        {"query": {"wildcard": {"group": {"value": "hotels_cities"}}}}
      ]
    }
  }
}  }}';



$params['index'] = 'my_custom_index_name';
$params['type']  = 'translator_translations';
$params['body'] = $body;

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

输出命中为零。

-有一个叫做分析器的东西,但是没有关于如何在 PHP 中使用它的信息。

最佳答案

我想我找到了如何在 Elasticsearch 中索引 unicode 语言字符的答案,希望这对任何人都有用。

  • 首先您必须设置您的索引名称

  • 其次使用过滤器和语言分析器设置您的新语言设置,像这样:

    $client = ClientBuilder::create()       // Instantiate a new ClientBuilder
                ->setHosts(['localhost:9200'])      // Set the hosts
                ->build();
    
    $lang = 'el'; // Greek in my case
    
    $param['index'] = 'test_' . $lang; // index name
    
    // uncomment this line if you want to delete an existing index
    // $response = $client->indices()->delete($param);
    
    $body = '{
      "settings": {
        "analysis": {
          "filter": {
            "greek_stop": {
              "type":       "stop",
              "stopwords":  "_greek_" 
            },
            "greek_lowercase": {
              "type":       "lowercase",
              "language":   "greek"
            },
            "greek_keywords": {
              "type":       "keyword_marker",
              "keywords":   ["παράδειγμα"] 
            },
            "greek_stemmer": {
              "type":       "stemmer",
              "language":   "greek"
            }
          },
          "analyzer": {
            "greek": {
              "tokenizer":  "standard",
              "filter": [
                "greek_lowercase",
                "greek_stop",
                "greek_keywords",
                "greek_stemmer"
              ]
            }
          }
        }
      }
    }';
    
    $param['body'] = $body; // store the JSON body as a parameter in the main array
    
    $response = $client->indices()->create($param);
    

然后开始用希腊字符索引你的值

关于php - 如何在 elasticsearch 中使用 Unicode 字符进行搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45302197/

相关文章:

php - PDO 查询关联表并显示结果

mysql - 传播.REQUIRES_NEW 锁定

mysql - 当使用星号(通配符)运算符执行搜索时,MySQL 将如何使用 FT 索引?

Javascript 表单提交打开新窗口选项卡,然后重定向父页面

php - 在 MySQL 数据库中的纬度/经度矩形内查找项目的优雅方式?

elasticsearch - 跨多个索引的多个地理位置组合搜索

elasticsearch - Stormcrawler不会为Elasticsearch提取/索引页面

python - ElasticSearch批量更新: organizing JSON using python script

PHP:将返回值 'false' 放在哪里?

mysql - SQL GROUP BY 多列?