弹性搜索短语搜索

ele*_*eep 3 search json full-text-search nosql elasticsearch

我有一个弹性搜索文档,看起来像......

{
    "items":
    [
        "ONE BLAH BLAH BLAH TWO BLAH BLAH BLAH THREE",
        "FOUR BLAH BLAH BLAH FIVE BLAH BLAH BLAH SIX"
    ]
}
Run Code Online (Sandbox Code Playgroud)

我希望能够使用短语查询来搜索此文档,例如...

{
    "match_phrase" : {
        "items" : "ONE TWO THREE"
    }
}
Run Code Online (Sandbox Code Playgroud)

因此,无论中间的单词如何,它都将匹配.这些词也需要按顺序排列.我意识到这可以通过slop属性来实现,但是当我进行实验的时候,它似乎包裹起来,如果斜率不仅仅是我在搜索之间的单词,而且这是一个不确定的单词我不喜欢认为slop是合适的.另外我只需搜索数组中的每个项目,所以...

{
    "match_phrase" : {
        "items" : "ONE TWO SIX"
    }
}
Run Code Online (Sandbox Code Playgroud)

如在本文件不会匹配SIX是在该阵列中不同的项目ONETWO.

所以我的问题是,这可能是通过elasticsearch还是我必须创建一个对象数组并使用嵌套查询来搜索它们?

imo*_*tov 12

可以使用Span Near Query完成.我不确定你的实验出了什么问题,你的意思是"包装".我只能猜测,或许你指定了"in_order":"false",你的查询只是忽略了术语的顺序.你能提供一个例子吗?

为避免跨多个项目的查询,您需要使用"position_offset_gap"属性增加映射中项目之间的"间隙".这是一个例子:

curl -XDELETE "localhost:9200/slop-test"
echo
curl -XPUT "localhost:9200/slop-test" -d '{
  "settings" : {
    "index" : {
        "number_of_shards" : 1,
        "number_of_replicas" : 0
    }    
  },
  "mappings" : {
    "doc" : {
      "properties" : {
        "items" : {
          "type" : "string",
          "position_offset_gap": 100
        }
      }
    }
  }
}'
echo
curl -XPUT "localhost:9200/slop-test/doc/1" -d '{
  "items":
  [
      "ONE BLAH BLAH BLAH TWO BLAH BLAH BLAH THREE",
      "FOUR BLAH BLAH BLAH FIVE BLAH BLAH BLAH SIX"
  ]
}'
curl -XPOST "localhost:9200/slop-test/_refresh"
echo
curl "localhost:9200/slop-test/_search?pretty=true" -d '{
  "query" : {
    "span_near" : {
      "clauses" : [
        { "span_term" : { "items" : "one" } },
        { "span_term" : { "items" : "two" } },
        { "span_term" : { "items" : "three" } }
      ],
      "slop" : 40,
      "in_order" : true
    }
  }
}'
echo
curl "localhost:9200/slop-test/_search?pretty=true" -d '{
  "query" : {
    "span_near" : {
      "clauses" : [
        { "span_term" : { "items" : "one" } },
        { "span_term" : { "items" : "two" } },
        { "span_term" : { "items" : "six" } }
      ],
      "slop" : 40,
      "in_order" : true
    }
  }
}'
echo
Run Code Online (Sandbox Code Playgroud)