【问题标题】:Using wildcards in combination with exact match in the r library Elasticsearch在 r 库 Elasticsearch 中结合使用通配符和完全匹配
【发布时间】:2017-06-01 16:13:43
【问题描述】:

可能由于我对 Elasticsearch(和 R 库 Elastic)缺乏经验,我无法弄清楚如何以精确匹配与通配符结合的方式来制定搜索查询。

docs_create(index="test",type="x", body=list(txt="this is a test"))
count(index='test', type='x',q="'a test'",default_operator="AND")

...返回一个匹配项。

count(index='test', type='x',q="'a te*'",default_operator="AND")

...返回不匹配(虽然我也希望得到一个匹配)。

count(index='test', type='x',q="'a te/*'",default_operator="AND")

...产生错误 (Error: 400 - all shards failed)。

count(index='test', type='x',q="'a te'//*",default_operator="AND")  

...返回不匹配。

欢迎任何帮助!谢谢。

PS。如果使用 count 函数无法做到这一点,那么使用 Search 函数也可以解决我的问题(使用 body 参数)。

【问题讨论】:

  • 您使用的是哪个版本的elastic 和 Elasticsearch?
  • 我用的是5.4.0版
  • 好的,elastic(R pkg)的哪个版本?
  • 也是最新的,0.7.8版

标签: r elasticsearch


【解决方案1】:

@peterk 不带单引号的查询返回什么?

你可以这样试试吗:

count(index='test', type='x', q="a te*", default_operator="AND")

【讨论】:

  • @usogan,是的,它返回一个命中,但不能解决问题,因为 count(index='test', type='x', q="is te*", default_operator="AND") 也返回一个我不想要的命中。在 Elasticsearch 文档中我看到了选项prefix search,也许这就是解决方案的开始?
【解决方案2】:

有一点需要注意,尝试connect(errors = "complete") 来获取更详细的错误 - 与弹性服务器提供的信息基本相同

count(index='test', type='x',q="'a te/*'",default_operator="AND")
#> Error: 400 - all shards failed
#> ES stack trace:
#> 
#>   type: query_shard_exception
#>   reason: Failed to parse query ['a te/*']
#>   index_uuid: xbZ5ANNBRwqxEsqPqS05Dg
#>   index: test

这表明该查询无效

一般来说,使用 elastic::Search() 更灵活,因为它更容易使用 - 并将 size 参数设置为 0,这样您就可以获得匹配的记录数。

我仍然不清楚确切的部分是什么。是a te 还是a test 还是别的什么?

【讨论】:

  • 感谢您的建议;好的,我切换到elastic::Search()。很抱歉不清楚,我想要返回/在数据库中查找的是所有以(精确部分)a te 开头的短语组合,包括a tea testa testsa testies。希望我的问题现在很清楚。
  • 您是否尝试过通过 elastic::Search() 查询 DSL elastic.co/guide/en/elasticsearch/reference/5.4/… 的通配符 - 是否符合您的要求?
【解决方案3】:

在睡了一个星期之后,我想出了如何完成这项工作(使用 match_phrase_prefix 选项:

docs_create(index="test",type="x", body=list(txt="this is a test"))

body <-
'{
    "query": {
        "match_phrase_prefix" : {
            "txt" : {
                "query" : "a te",
                "max_expansions" : 10
            }
        }
    }
}'

Search(index='test',body=body)

【讨论】:

    猜你喜欢
    • 2012-03-26
    • 1970-01-01
    • 1970-01-01
    • 2016-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    相关资源
    最近更新 更多