【问题标题】:Match query return records only if query contains all words of object's field仅当查询包含对象字段的所有单词时才匹配查询返回记录
【发布时间】:2018-07-18 04:17:11
【问题描述】:

我阅读了有关匹配和多字查询的信息,但似乎我需要做一些不同的事情。

假设我有以下查询:“这是一个测试”,我想在一个名为“文本”的字段中找到该查询。我想获取与某些查询匹配的对象(不管多少字),但只有那些查询值包含文本字段的每个字的对象。

查询示例:“这是一个测试”。我想得到那些对象:

obj1: {"text":"this is a test"}
obj2: {"text":"this is a"}
obj3 : { "text" : "is a" }
obj4 : { "text" : "test" }

但如果 obj 在文本字段中有更多内容,则不会返回例如:

obj5: {"text":"this is a test and something more"}

是否可以使用 Elasticsearch 实现这一点?

【问题讨论】:

    标签: elasticsearch spring-data-elasticsearch


    【解决方案1】:

    这是一种 hack,但我能够让它与 script filter 一起工作:

    POST /test_index/_search
    {
       "query": {
          "match": {
             "text": "this is a test"
          }
       },
       "filter": {
          "script": {
             "script": "for(val in doc[\"text\"].values){ if(!(val in terms)){ return false; }}; return true;",
             "params": {
                "terms": ["this", "is", "a", "test"]
             }
          }
       }
    } 
    

    我认为会有更好的方法来做到这一点,但无法立即想出一个。在生产中使用脚本可能会出现问题,除非您的 ES 集群位于某种身份验证墙后面。

    不管怎样,这是我用来测试它的代码:

    http://sense.qbox.io/gist/3929abc89d71ebf724e6121b1b5ba6da54501088

    【讨论】:

      猜你喜欢
      • 2019-07-22
      • 2021-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-28
      • 1970-01-01
      相关资源
      最近更新 更多