【问题标题】:Elasticsearch combined query and filter not giving correct resutlsElasticsearch 结合查询和过滤器没有给出正确的结果
【发布时间】:2015-03-07 11:19:57
【问题描述】:

我正在尝试使用额外的过滤器项创建一个搜索页面,但我无法让我的查询按我想要的方式工作。

这是查询示例:

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

我只想要 2015 年的结果。搜索标题“Sense”一无所获,即使有一行标题为“Sense8”。如果我搜索 Sense8,它会返回正确的数据,但不是“Sense”。

我做错了什么?

谢谢

【问题讨论】:

  • 请提及索引的映射,如果可能的话,请提及您希望出现在结果中的 JSON 文档。
  • 我正在使用一个名为 BungieSearch 的包装器来生成查询,但我发现 "term" 过滤器和 "terms" 过滤器都需要数组。查看这个 SO 答案:[link(]stackoverflow.com/questions/30363647/…)

标签: json elasticsearch dsl


【解决方案1】:

您可能需要在映射中使用ngramedge ngram 分析器。我在 Qbox blog 上写了一篇关于使用 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

【讨论】:

    猜你喜欢
    • 2019-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多