簡體   English   中英

如何讓elasticsearch執行完全匹配查詢?

[英]How to get elasticsearch to perform an exact match query?

這是一個由兩部分組成的問題。

我的文件看起來像這樣:

{"url": "https://someurl.com", 
 "content": "searchable content here", 
 "hash": "c54cc9cdd4a79ca10a891b8d1b7783c295455040", 
 "headings": "more searchable content", 
 "title": "Page Title"}

我的第一個問題是如何檢索“標題” 正好是 “無標題”的所有文檔。 我不希望出現標題為“此文檔沒有標題”的文檔。

我的第二個問題是如何檢索所有文件,其中'url' 恰好出現在一長串網址中。

我正在使用pyelasticsearch,但curl中的通用答案也可以。

您必須為字段定義映射。

如果要查找精確值(區分大小寫),可以將index屬性設置為not_analyzed

就像是 :

"url" : {"type" : "string", "index" : "not_analyzed"}

如果您已存儲源(這是默認值),則可以使用腳本過濾器

它應該是這樣的:

$ curl -XPUT localhost:9200/index/type/1 -d '{"foo": "bar"}'
$ curl -XPUT localhost:9200/index/type/2 -d '{"foo": "bar baz"}'
$ curl -XPOST localhost:9200/index/type/_search?pretty=true -d '{
"filter": {
    "script": {
        "script": "_source.foo == \"bar\""
    }
}
}'
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "index",
      "_type" : "type",
      "_id" : "1",
      "_score" : 1.0, "_source" : {"foo": "bar"}
    } ]
  }
}

編輯 :我認為值得一提的是,“not_analyzed”映射應該是更快的方法。 但是如果你想要這個字段的精確匹配和部分匹配,我會看到兩個選項:使用腳本或將數據索引兩次(一旦分析,一旦未分析)。

試試這個方法。 是工作。

import json
from elasticsearch import Elasticsearch
connection = Elasticsearch([{'host': host, 'port': port}])

elastic_query = json.dumps({
     "query": {
         "match_phrase": {
            "UserName": "name"
          }
      }
 })
result = connection.search(index="test_index", body=elastic_query)

暫無
暫無

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

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