【问题标题】:Elasticsearch reverse match_phraseElasticsearch 反向匹配短语
【发布时间】:2015-09-25 09:28:47
【问题描述】:

考虑以下文档:

{
  "Title": "Western Europe"
}

我想针对 Title 字段运行此类搜索查询

  • 苹果在西欧
  • 苹果在东欧

我可以运行一个简单的匹配查询:

POST /_search
{
  "query": {
    "match": {
      "Title": "Apple in Western Europe"
    }
  }
}

无论我使用哪个搜索词组,它显然都会匹配并带回它。但我想做一个查询,只有当 Title 字段 phrase 匹配 我的搜索查询时,我的文档才会返回。那可能吗?有没有额外的参数? phrase matching 似乎是相反的情况。

如果没有,我是否应该考虑使用带状疱疹重新索引我的数据?

因此,在这种情况下,运行此(使用附加参数)不会得分并带回我的文档。

POST /_search
{
  "query": {
    "match": {
      "Title": "Apple in Eastern Europe"
    }
  }
}

tl;dr

如果我的搜索查询中存在其所有字段(我正在搜索的那个)标记,我如何编写一个可以带回文档的查询? 例如,我在文档中的字段仅包含这两个标记:

  • abc
  • xyz

如果我的搜索短语是,例如 Lorem ipsum dolor sit amet, consectetur adipiscing elit abc xyz,则文档被带回

如果是 Lorem ipsum dolor sit amet, consectetur adipiscing elit xyz,它不会带回来

【问题讨论】:

  • "bring my document back only if Title field phrase matches my search query" 您的搜索查询是Apple in Western Europe。也许你想说“in 之后应该匹配 title”?
  • 在这个非常特殊的情况下是的。
  • @AndreiStefan 我用 tl;dr 更新了我的问题。我希望这能说清楚。
  • 条款的顺序很重要吗?
  • 这是个好问题。不,它没有。

标签: elasticsearch


【解决方案1】:

您可以使用 Percolator 查询 (ES Documentation):

  1. 索引您想要匹配的查询
  2. 与与查询关联的元数据兼容
  3. 在搜索请求中发送文档
  4. 获取匹配的查询

您的用例示例

创建映射

  • query: 接受 ES 查询,拥有强大的背后力量
  • Title:元数据(可选)
PUT /my_index
{
    "mappings": {
        "properties": {
            "Title": {
                "type": "text"
            },
            "query": {
                "type": "percolator"
            }
        }
    }
}

添加文档

PUT /my_index/_doc/1
{
  "query": {
    "match_phrase": {
      "Title": "Western Europe"
    }
  },
  "Title": "Western Europe"
}

获取匹配的查询

POST /my_index/_search
{
    "query": {
        "percolate": {
            "field": "query",
            "document": {
                "Title": "Apple in Western Europe"
            }
        }
    }
}

【讨论】:

    【解决方案2】:

    尝试使用“间隔查询”使用不同的参数来帮助您。

    https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-intervals-query.html

    【讨论】:

    • 欢迎来到 SO!您应该提供更多帮助,而不仅仅是链接,否则您的答案将被标记为仅链接答案并最终被删除。
    【解决方案3】:

    我知道 Stefan 在 cmets 中给出了一个简单有效的解决方案,但您可能还想将 Span Queries 视为仅供参考!!

    我创建了示例映射、文档、查询和响应:

    映射:

    PUT my_span_index
    {
      "mappings": {
        "properties": {
          "Title":{
            "type": "text"
          }
        }
      }
    }
    

    示例文件:

    POST my_span_index/_doc/1
    {
      "Title": "Western Europe"
    }
    
    POST my_span_index/_doc/2
    {
      "Title": "Eastern Europe"
    }
    
    //slop - distance between words Western and Europe here is 13
    POST my_span_index/_doc/3
    {
      "Title": "As far as Western culture is America, we see gradually more and more of the same in Europe"
    }
    

    跨度查询:

    POST my_span_index/_search
    {
        "query": {
            "span_near" : {
                "clauses" : [
                    { "span_term" : { "Title": "western" } },
                    { "span_term" : { "Title": "europe" } }
                ],
                "slop" : 12,                                <---- Distance Between Words
                "in_order" : true                           <---- If order is important
            }
        }
    }
    

    请注意,我使用了 Span NearSpan Term Query 并注意上面的 cmets。

    回应:

    {
      "took" : 1,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 2,
          "relation" : "eq"
        },
        "max_score" : 0.5420371,
        "hits" : [
          {
            "_index" : "my_span_index",
            "_type" : "_doc",
            "_id" : "1",
            "_score" : 0.5420371,
            "_source" : {
              "Title" : "Western Europe"
            }
          },
          {
            "_index" : "my_span_index",
            "_type" : "_doc",
            "_id" : "3",
            "_score" : 0.028773852,
            "_source" : {
              "Title" : "As far as Western culture is America, we see gradually more and more of the same in Europe"
            }
          }
        ]
      }
    }
    

    请注意,在响应中还会返回具有id:3 的文档,但是如果将斜率更改为较小的值,它将不会出现。

    如果您的请求将有更多令牌,那么您最终会在应用程序端编写/生成长查询。

    希望我能帮上忙!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-08
      • 2018-04-21
      • 1970-01-01
      • 1970-01-01
      • 2021-03-08
      • 1970-01-01
      相关资源
      最近更新 更多