以编程方式使用搜索 API Solr Drupal 8 上的"AND"、"OR"、"NOT"进行直接查询



我是Drupal的新手。我正在尝试在我的Drupal8网站上实现高级搜索。它与逻辑运算符ORAND完美配合,但不能与NOT+-一起使用。

这是我的要求:

$index = Drupalsearch_apiEntityIndex::load('videos_full');
    $query = $index->query();
    $parse_mode = Drupal::service('plugin.manager.search_api.parse_mode')
                  ->createInstance('direct');
    $query->setParseMode($parse_mode);
    $query->addCondition('search_api_language', Drupal::languageManager()->getCurrentLanguage()->getId());
    $query->addCondition("status",5);
    $query->setFulltextFields(['field_title', 'title', 'resourcetype', 'tag', 'choreographer', 'director', 'producer', 'structure', 'contributor', 'secondaryauthor']);
    $query->keys($search);
    $query->sort('created','DESC');
    $results = $query->execute();

例如,如果我输入"iggy NOT pop",我有很多"iggy pop"的结果

感谢您的帮助!

我试过你的代码,但我无法确认

它与逻辑运算符 OR 和 AND 完美配合

进入时

伊吉或流行

它执行"AND"搜索,结果不包括只有"iggy"或"pop"的内容。您确定可以这样设置运算符吗?

我读了一些关于

$parse_mode = Drupal::service('plugin.manager.search_api.parse_mode')
  ->createInstance('direct');
$parse_mode->setConjunction('OR');
$query->setParseMode($parse_mode);

在文档中,但这似乎也不起作用。"iggy pop"和"iggy OR pop"找不到只有"iggy"的内容,就像"AND"是被迫的。

我使用运算符的唯一方法是为查询提供一个数组而不是字符串:

$search_string_exploded = str_word_count($search_string, 1);
$keys = array_merge($search_string_exploded, ['#conjunction' => 'OR']);
$query->keys($keys);

也许这会有所帮助。

drupal 中的搜索查询字符串被清理,这可能就是为什么您将无法使用 solr 的 +/-

我要做的是在Drupal中自定义查询清理,只允许+和-。solr 的 - 运算符是实现 NOT 功能的唯一方法。

希望这有帮助。

最新更新