【问题标题】:elasticsearch phrase searchingelasticsearch 词组搜索
【发布时间】:2012-09-16 01:43:10
【问题描述】:

我在弹性搜索中有一个看起来像...的文档

{
    "items":
    [
        "ONE BLAH BLAH BLAH TWO BLAH BLAH BLAH THREE",
        "FOUR BLAH BLAH BLAH FIVE BLAH BLAH BLAH SIX"
    ]
}

我希望能够使用类似...的短语查询来搜索此文档

{
    "match_phrase" : {
        "items" : "ONE TWO THREE"
    }
}

这样无论中间的单词如何,它都会匹配。单词也需要按照这个顺序。我意识到这可以通过slop 属性来实现,但是当我尝试它时,如果斜率大于我正在搜索的单词之间的单词,并且因为这是一个不确定的单词数量,它似乎会换行我认为slop不合适。另外我只需要搜索数组中的每个项目,所以...

{
    "match_phrase" : {
        "items" : "ONE TWO SIX"
    }
}

不会匹配此文档,因为 SIXONETWO 位于数组中的不同项目中。

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

【问题讨论】:

    标签: json search nosql full-text-search elasticsearch


    【解决方案1】:

    可以使用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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-01
      • 1970-01-01
      • 2016-03-19
      • 2016-01-07
      • 1970-01-01
      • 2018-02-03
      • 2021-04-21
      相关资源
      最近更新 更多