1

我正在使用 elastic4s 来索引和搜索 ES 中的数据。我存储的部分文档包含一个我需要搜索的 url 字段(整个 url)。当我搜索包含 url 字段的文档并获得 0 个结果时,就会出现问题。

出于此搜索的目的,我在将数据插入索引之前定义了一个映射:

client.execute {
  create index <my_index> mappings {
    <my_type> as {
      "url" typed StringType analyzer NotAnalyzed
    }
  }
}

我正在插入数据:

client.execute { index into <my_index> -> <my_type> fields (
  "url" -> "http://sample.com"
  )
}.await

我搜索文件:

val filter =
"""
  |{
  |    "filtered" : {
  |        "query" : {
  |            "match_all" : {}
  |        },
  |        "filter" : {
  |            "bool" : {
  |              "should" : [
  |                { "bool" : {
  |                  "must" : [
  |                     { "term" : { "url" : "http://sample.com" } }
  |                  ]
  |                } }
  |              ]
  |            }
  |        }
  |    }
  |}
""".stripMargin

client.execute { search in <my_index> -> <my_type> rawQuery { filter } }.await

执行此查询后,我得到 0 个结果。我究竟做错了什么?

谢谢!

4

2 回答 2

1

问题解决了。

在映射中,而不是这样做:

client.execute {
  create index <my_index> mappings {
    <my_type> as {
      "url" typed StringType analyzer NotAnalyzed
    }
  }
}

我应该这样做:

client.execute {
  create index <my_index> mappings {
    <my_type> as {
      "url" typed StringType index "not_analyzed"
    }
  }
}

或者

client.execute {
  create index <my_index> mappings {
    <my_type> as {
      "url" typed StringType index NotAnalyzed
    }
  }
}
于 2015-09-24T11:35:48.567 回答
0

我认为您的查询可以简化一点,例如:

{
  "query": {
    "term": {
      "url": {
        "value": "http://example.com"
      }
    }
  }
}

奇怪的是,由于布尔查询的嵌套,我希望您的查询返回所有文档。should表示匹配数组中任何查询的文档得分高于不匹配的文档。因此,should只包含一个must应该返回所有文档,匹配的文档得分高于不匹配的文档。

于 2015-09-22T10:53:58.847 回答