第87篇ES之Elastica-php匹配多值字段及给Problem的elementName设中文分词ik
關鍵詞:Elastica-php匹配多值字段, 給Problem的elementName設中文分詞ik
一、Elastica-php匹配多值字段
1.1 實現了匹配選項
1)參考如下:
https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_search_operations.html
a.)標準的黑窗口查詢如下:
curl -XGET'localhost:9200/my_index/my_type/_search' -d '{
???"query" : {
???????"match" : {
???????????"testField" : "abc"
???????}
??? }
}'
b.)對應的php客戶端的偽代碼如下:
$params = [
???'index' => 'my_index',
???'type' => 'my_type',
???'body' => [
???????'query' => [
???????????'match' => [
??????????????? 'testField' => 'abc'
???????????]
???????]
??? ]
];
$results = $client->search($params);
分析:
$params = [
???'index' => 'my_index',
???'type' => 'my_type',
???'body' => [
???????'query' => [
???????????'match' => [
??????????????? 'testField' => 'abc'
???????????]
???????]
??? ]
];
print_r(json_encode($params['body']));
{"query":{"match":{"testField":"abc"}}}
?
c.)php客戶端使用json串如下:
$json = '{
???"query" : {
???????"match" : {
???????????"testField" : "abc"
???????}
??? }
}';
$params = [
???'index' => 'my_index',
???'type' => 'my_type',
???'body' => $json
];
$results = $client->search($params);
注:這種寫法在php客戶端能執行成功。
d.)應用到自己的項目如下:
if($user_input){
????????????????????????? ??? $json = '{
???????????????????????????????????????????????????? ??? "query":{
???????????????????????????????????????????????????? ??????? "multi_match":{
???????????????????????????????????????????????????? ??????????? "query":"通過改變橡皮筋的長度來改變拉力做功的數值",
???????????????????????????????????????????????????? ??????????? "fields":[
???????????????????????????????????????????????????? ??????????????? "text",
???????????????????????????????????????????????????? ??????????????? "content"
???????????????????????????????????????????????????? ??????????? ]
???????????????????????????????????????????????????? ??????? }
???????????????????????????????????????????????????? ??? }
???????????????????????????????????????????????????? }';
?????????????????????????????????? $params= array(
?????????????????????????????????? ??? 'index' => 'dzplib',
?????????????????????????????????? ??? 'type' => $index_type,
?????????????????????????????????? ??? 'body' => $json
????????????????????????? ??? );
?????????????????????????????????? $response= $client->search($params);
?????????????????????????????????? print_r($response);
???????? }
e.)改成數組方式如下:
if($user_input){
?????????????????????????????????? $params= array(
?????????????????????????????????? ??? 'index' => 'dzplib',
?????????????????????????????????? ??? 'type' => $index_type,
?????????????????????????????????? ??? 'body' => array(
????????????????????????? ??????????? 'query' => array('multi_match'=>
array('query'=>$user_input,'fields'=>array('text','content'))),
????????????????????????? ??????????? 'highlight' => array(
???????????????????????????????????????????????????? ??? 'fields' => array(
???????????????????????????????????????????????????? ??????? $index_field => new \stdClass()
???????????????????????????????????????????????????? ??? )
??????????????????????????????????????????? ??? ),
??????????????????????????????????????????? 'size'=>3,? //可以限制輸出的結果個數
????????????????????????? ??????? )??
????????????????????????? ??? );
?
?????????????????????????????????? $response= $client->search($params);
?????????????????????????????????? print_r($response);
????????????????? }
?1.2 給Problem的elementName設中文分詞ik
1)描述:
現在作用在選項上,是實現了,但效果并不好。就是說我把四個選項全寫上,搜題搜索不出來。我分析原因在于:答案和解析里面有elementContent;因此,我想到的優化策略是:在搜索中去掉答案和解析中的檢索。
2)在Problem的elementName中加入ik
成功了,但遇到二個問題。一是必須要先建個索引dzplib,之后再更新dzplib;二是類型Problem中加入了elementName的ik,那Paper中elementName也要加ik,否則,不能更新成功。另外,如果說沒刪除數據庫的話,就更新,會說elementName已存在,不能對其進行更新。因為我設置的狀態是不能動態更新("dynamic":"false")
2017年3月28日星期二
總結
以上是生活随笔為你收集整理的第87篇ES之Elastica-php匹配多值字段及给Problem的elementName设中文分词ik的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python计算连续复利_复利的Pyth
- 下一篇: 刷题回顾(持续更新)