簡體   English   中英

使用elastic4s搜索整個URL

[英]Searching for whole URL using elastic4s

我正在使用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個結果。 我究竟做錯了什么?

謝謝!

問題解決了。

在映射中,而不是這樣做:

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
    }
  }
}

我認為您的查詢可以簡化為:

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

奇怪的是,由於布爾查詢的嵌套,我希望您的查詢返回所有文檔。 should意味着與數組中任何查詢匹配的文檔的得分均高於不匹配的文檔。 因此,一個should只包含一個must要返回所有的文件,用匹配的文檔得分比不匹配更高。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM