簡體   English   中英

Elasticsearch組合查詢和過濾器未提供正確的結果

[英]Elasticsearch combined query and filter not giving correct resutls

我正在嘗試使用額外的過濾器項來創建搜索頁面,但是我無法使查詢正常工作。

這是查詢示例:

{
    "size": 25,
    "from": 0,
    "sort": {
        "_score": {
            "order": "asc"
        }
    },
    "query": {
        "filtered": {
            "filter": {
                "bool": {
                    "must": [
                        {
                            "term": {
                                "year": "2015"
                            }
                        }
                    ]
                }
            },
            "query": {
                "match": {
                    "title": "Sense"
                }
            }
        }
    }
}

我只需要2015年的結果。即使標題為“ Sense8”的行,搜索標題“ Sense”也沒有結果。 如果我搜索Sense8,它將返回正確的數據,但不會返回“ Sense”。

我究竟做錯了什么?

謝謝

您可能需要在映射中使用ngramedge ngram分析器。 我在Qbox博客上寫了一篇關於使用ngrams自動完成的博客文章 ,其中詳細介紹了一些代碼,但是下面的代碼可能會為您提供所需的信息:

PUT /test_index
{
   "settings": {
      "analysis": {
         "filter": {
            "ngram_filter": {
               "type": "edgeNGram",
               "min_gram": 2,
               "max_gram": 20,
               "token_chars": [
                  "letter",
                  "digit",
                  "punctuation",
                  "symbol"
               ]
            }
         },
         "analyzer": {
            "ngram_analyzer": {
               "type": "custom",
               "tokenizer": "whitespace",
               "filter": [
                  "lowercase",
                  "asciifolding",
                  "ngram_filter"
               ]
            },
            "whitespace_analyzer": {
               "type": "custom",
               "tokenizer": "whitespace",
               "filter": [
                  "lowercase",
                  "asciifolding"
               ]
            }
         }
      }
   },
   "mappings": {
      "doc": {
          "properties": {
               "year":{
                   "type": "string"
               },
               "title":{
                   "type": "string",
                   "index_analyzer": "ngram_analyzer", 
                   "search_analyzer": "whitespace_analyzer"
               }
           }
      }
   }
}

POST /test_index/_bulk
{"index":{"_index":"test_index","_type":"doc","_id":1}}
{"year": "2015","title":"Sense8"}
{"index":{"_index":"test_index","_type":"doc","_id":2}}
{"year": "2014","title":"Something else"}

POST /test_index/_search
{
    "size": 25,
    "from": 0,
    "sort": {
        "_score": {
            "order": "asc"
        }
    },
    "query": {
        "filtered": {
            "filter": {
                "bool": {
                    "must": [
                        {
                            "term": {
                                "year": "2015"
                            }
                        }
                    ]
                }
            },
            "query": {
                "match": {
                    "title": "Sense"
                }
            }
        }
    }
}
...
{
   "took": 3,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": null,
      "hits": [
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "1",
            "_score": 0.30685282,
            "_source": {
               "year": "2015",
               "title": "Sense8"
            },
            "sort": [
               0.30685282
            ]
         }
      ]
   }
}

您可以在瀏覽器中運行以下代碼:

http://sense.qbox.io/gist/4f72c182db2017ac7d32077af16cbc3528cb79f0

暫無
暫無

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

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