【问题标题】:Elasticsearch query_string search complex keyword by its termsElasticsearch query_string 按其术语搜索复杂关键字
【发布时间】:2017-07-21 09:17:16
【问题描述】:

现在,我知道关键字不应该包含非结构化文本,但假设出于某种原因,恰好将此类文本写入关键字字段。 使用 match 或 term 查询搜索此类文档时,找不到文档,但使用 query_string 搜索时,通过部分匹配(关键字内部的“term”)找到文档。当 Elasticsearch 的文档明确指出关键字按原样进行反向索引时,我不明白这是怎么可能的,没有术语标记化。 例子: 我的索引映射:

PUT my_index
{
  "mappings": {
    "my_type": {
      "properties": {
        "full_text": {
          "type":  "text" 
        },
        "exact_value": {
          "type":  "keyword" 
        }
      }
    }
  }
}

然后我把一个文件放进去:

PUT my_index/my_type/2
{
  "full_text":   "full text search", 
  "exact_value": "i want to find this trololo!"  
}

想象一下当我得到一个按关键字词而不是完全匹配的文档时我的惊讶:

GET my_index/my_type/_search
{
  "query": {
    "match": {
      "exact_value": "trololo" 
    }
  }
}

- 没有结果;

GET my_index/my_type/_search
{
  "query": {
    "term": {
      "exact_value": "trololo" 
    }
  }
}

- 没有结果;

POST my_index/_search
{"query":{"query_string":{"query":"trololo"}}}

- 我的文档被退回(!):

       "hits": {
      "total": 1,
      "max_score": 0.27233246,
      "hits": [
         {
            "_index": "my_index",
            "_type": "my_type",
            "_id": "2",
            "_score": 0.27233246,
            "_source": {
               "full_text": "full text search",
               "exact_value": "i want to find this trololo!"
            }
         }
      ]
   }

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    当您像下面这样在弹性上执行 query_string 查询时

    POST index/_search
    {
        "query": {
            "query_string": {
                "query": "trololo"
            }
        }
    }
    

    这实际上是在 _all 字段上进行搜索,如果您不提及该字段,则由弹性标准分析器进行分析。

    如果您在查询中指定字段,如下所示,您将不会获得关键字字段的记录。

    POST my_index/_search
    {
      "query": {
        "query_string": {
          "default_field": "exact_value", 
          "query": "field"
        }
      }
    }
    

    【讨论】:

    • 这很有意义。当然,我知道 _all 领域,我只是没有足够的实践经验,因为我们最近才开始研究 Elastic stack,而且我没有设法将它们放在一起。谢谢!
    猜你喜欢
    • 2016-05-15
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    相关资源
    最近更新 更多