按相同顺序分析和匹配所有条款

时间:2015-12-24 12:25:54

标签: elasticsearch

我想要实现的目标:

文件:“一二三四”

搜索字符串:

  • “一四”(必须匹配)
  • “四一”(绝不匹配)

到目前为止我学到了什么:

对于要考虑的顺序,应使用span_near查询,但这假定客户已经分析了这些条款(所有条款必须单独提供)。

要分析搜索字符串,应使用phrase_match查询,但不会考虑订单。

可能应该使用脚本(感谢@ ChintanShah25),但似乎无法分析脚本中的输入字符串。

如何实现分析和订单要求?

1 个答案:

答案 0 :(得分:3)

没有直接的方法可以实现这一点,您可以使用_analyze端点span queryscriptmatch_phrase

来实现此目的

1)您将搜索字符串传递给_analyze

curl -XGET 'localhost:9200/_analyze' -d '
{
  "analyzer" : "my_custom_analyzer",
  "text" : "one four"
}'

你会得到类似的东西

{
   "tokens": [
      {
         "token": "one",
         "start_offset": 0,
         "end_offset": 3,
         "type": "<ALPHANUM>",
         "position": 1
      },
      {
         "token": "four",
         "start_offset": 4,
         "end_offset": 8,
         "type": "<ALPHANUM>",
         "position": 2
      }
   ]
}
然后,您将令牌传递给span query

{
    "span_near" : {
        "clauses" : [
            { "span_term" : { "field" : "token1" } },
            { "span_term" : { "field" : "token2" } }
        ],
        "slop" : 2,
        "in_order" : true,
        "collect_payloads" : false
    }
}

2)另一种方法是使用advanced scripting,查看@Andrei Stefan对this question的回答,他使用_POSITIONSmatch_phrase来取回结果按顺序排列。

希望这有帮助!