弹性搜索找到精确的字符串PHP

时间:2015-07-16 00:54:50

标签: php elasticsearch

我是elasticsearch的新用户,但我遇到了困难,因为我的查询在更改类型和过滤条件后没有改变我的结果!

我正在使用MongoDB PHP,这是我的代码:

  $test2 = [ 
        "filtered" => [
            "query" => [
                "match_phrase" => [
                    "product_name" => [
        "query" =>  "Brown Fox",
    ]
                ]
            ]
        ]
    ];
$testResult = $client_es->search(
                array(
                    'index' =>'myindex',
                    'type'  => 'mytype',
                    'body'  => array(
                        'query' => $test2,
                        'size' => 5000,
                    ),
                )
            );


        $testR = $testResult["hits"]["hits"];
        $total = $testResult["hits"]["total"];
        echo "TOTAL:  ". $total ."<br>";

        foreach ($testR as $key ) {
            echo $key["_source"]["product_name"];
           echo "<br>";


        }

我只想搜索确切的匹配,但我得到了所有带有字符串的东西。 指导我如何解决这个问题。

更新1:

curl -XPUT "www.domain.com/my_index/my_mapping/my_type" -d '{
        "my_type" : {
          "properties": {
              "product_name": {
                  "type": "string",
                  "index": "not_analyzed"
              }
          }
        }
}'; echo

2 个答案:

答案 0 :(得分:0)

我无法从您的标题中真正关注您的代码。如果您的问题是您没有获得完全匹配的结果,那么相应的string字段的映射可能不会使用not_analyzed,例如

{
    "product": {
        "properties": {
            "id": {
                "type": "integer"
            },
            "name": {
                "type": "string"
            }
        }
    }
}

而不是

{
    "product": {
        "properties": {
            "id": {
                "type": "integer"
            },
            "name": {
                "type": "string",
                "index": "not_analyzed"
            }
        }
    }
}

这将导致name字段使用默认分析器编制索引(导致每个单词都被编入索引)。

答案 1 :(得分:0)

你可以用这个

    <?php
$search_host = '127.0.0.1';
$search_port = '9200';
$index = 'myindex';
$doc_type = 'tweecoms';
$query = '_search';
ini_set('memory_limit', '5500000M');
set_time_limit(10000);
echo date('j m y h:i:s') . '<br>';
    $json_doc = 
                 '{
                    "query": {
                    "query_string": {
                        "query": "*life*",
                        "lenient": true
                    }
                    },
                    "size": 1000,
                    "from": 0,
                    "fields": [
                    "association_key"
                    ]
                }';
    //$json_doc = json_encode($json_doc);
    $baseUri = 'http://' . $search_host . ':' . $search_port . '/' . $index . '/' . $doc_type . '/' . $query;
    $ci = curl_init();
    curl_setopt($ci, CURLOPT_URL, $baseUri);
    curl_setopt($ci, CURLOPT_PORT, $search_port);
    curl_setopt($ci, CURLOPT_TIMEOUT, 200);
    curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ci, CURLOPT_FORBID_REUSE, 0);
    curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'GET');
    curl_setopt($ci, CURLOPT_POSTFIELDS, $json_doc);
    $response = curl_exec($ci);
    curl_close($ci);
    print_r($response);
    echo '<br>'.date('j m y h:i:s') . '<br>';