如何使用php在弹性搜索中添加more_like_this查询

时间:2015-05-05 06:13:48

标签: php elasticsearch

我正在尝试将弹性搜索包含到我的应用程序中。在场景中,用户可以选择字段类型并输入可搜索的值。

假设,使用名字和值搜索是'ABC'。 我使用bool查询尝试了这个,但我如何使用'more_like_this'查询方法? 这就是我到目前为止所做的。

$searchParams['index'] = 'articles';
$searchParams['type']  = 'article';
$searchParams['body']['query']['match']['body'] = 'ABC';
$queryResponse = $es->search($searchParams);

但如果我将more_like_this放入查询字段,则无法选择数据。 有什么帮助吗? 提前致谢

这是已修改的代码

$searchParams['index'] = 'articles';
$searchParams['type']  = 'article';

$searchParams['body']['query']['more_like_this']['fields'] = array('body','title');
$searchParams['body']['query']['more_like_this']['like_text'] =  $q;
$searchParams['body']['query']['more_like_this']['min_term_freq'] = 1;
$searchParams['body']['query']['more_like_this']['percent_terms_to_match'] = 1;
$searchParams['body']['query']['more_like_this']['min_doc_freq'] = 1;

你能告诉我这行代码是什么吗?

$searchParams['body']['query']['more_like_this']['min_term_freq'] = 1;
$searchParams['body']['query']['more_like_this']['percent_terms_to_match'] = 1;
$searchParams['body']['query']['more_like_this']['min_doc_freq'] = 1;

1 个答案:

答案 0 :(得分:0)

你需要做一个看起来像是json到ES的数组:

{
   "query": {
      "more_like_this": {
         "fields": [
            "text"
         ],
         "like_text": "apple",
         "min_term_freq": 1,
         "percent_terms_to_match": 1,
         "min_doc_freq": 1
      }
   }
}

所以在PHP中它将是:

$searchParams['body']['query']['more_like_this']['fields'] = array('body');
$searchParams['body']['query']['more_like_this']['like_text'] = 'apple';
$searchParams['body']['query']['more_like_this']['min_term_freq'] = 1;
$searchParams['body']['query']['more_like_this']['percent_terms_to_match'] = 1;
$searchParams['body']['query']['more_like_this']['min_doc_freq'] = 1;