【问题标题】:Elasticsearch check if title is already used in typeElasticsearch 检查标题是否已在类型中使用
【发布时间】:2017-10-11 04:17:33
【问题描述】:

我在 Elasticsearch 中收集了一个数据库,我没有通过 ID 来识别它们,而是通过标题来识别它们。因此,每种类型的标题都不相同。

我尝试拥有must => match_phrase,但它让我得到了不止一个回报。有些东西可能被称为"Document 1",而其他东西可能被称为"Document 1,2,3"。因此,它会通过执行match_phrase 返回多个结果。

假设我有 5 个名为:

  1. 文档示例 1
  2. 示例 1
  3. 1 文档示例
  4. 文档示例 1 和 2
  5. 文档示例

我应该发送什么请求只返回例如:"Document example"

我尝试了这种搜索的不同变体127.0.0.1:9200/index/type/_search

{
    "query":{
        "match_phrase": {
            "title":"Document example"
        }
    }
}

所以我想知道如何检查或搜索确切的解析并只得到一个或零个结果作为回报?

编辑

127.0.0.1:9200/myindex/mytype/_mapping 返回:

{
  "myindex": {
    "mappings": {
      "mytype": {
        "properties": {
          "category": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "date": {
            "type": "date"
          },
          "link": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "size": {
            "type": "long"
          },
          "source": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "title": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}

【问题讨论】:

  • 你能分享你的索引映射吗? GET /index/type/_mapping.
  • 另外,您需要更好地定义您的需求。要求查询仅返回"Document example" 很容易。问题还有:如果您搜索document example 会怎样?您是否还有其他与您的 title 字段匹配的查询?您是否也对部分匹配感兴趣?
  • @Richa 我在底部添加了它。我改变了它的一些值(索引和类型)
  • @AndreiStefan 数据库包含小写和大写。在这种情况下,即使它是大写或小写,也没有重复。我也许应该在示例中解释这一点。

标签: elasticsearch elasticsearch-5 elasticsearch-query


【解决方案1】:

只需在title.keyword 上使用term 过滤器(进行精确匹配):

{
  "query": {
    "term": {
      "title.keyword": {
        "value": "Document example"
      }
    }
  }
}

【讨论】:

  • 哇,漂亮。成功了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-04
  • 1970-01-01
相关资源
最近更新 更多