Elasticsearch:Span_near和子串匹配

时间:2014-04-09 18:09:14

标签: elasticsearch substring sequence wildcard string-matching

我是弹性搜索的新手。 我想实现span附近的功能,在精确的短语匹配和精确的单词序列匹配之后也会处理子串匹配。

例如:

我在索引上的文件:

  1. 男士奶油
  2. 男士抗皱霜
  3. 男士高级抗皱霜
  4. 女士奶油
  5. 女性抗皱霜
  6. 女性高级抗皱霜
  7. 如果我搜索“男士奶油”,我希望结果与上面显示的顺序相同。 预期搜索结果:

    1. 男士奶油 - >确切的短语匹配
    2. 男士抗皱霜 - >使用slop 1
    3. 搜索字词序列
    4. 男士高级皱纹霜 - >使用slop 2
    5. 搜索字词序列
    6. 女士奶油 - >子串接近精确短语匹配
    7. 女士抗皱霜 - >带有slop 1
    8. 的子字符串搜索词序列
    9. 女性高级皱纹霜 - >带有slop 2
    10. 的子字符串搜索词序列

      我可以通过span_near嵌套span_terms slop = 2in_order = true来获得前3个结果。
      我无法实现剩余的4到6,因为span_near有嵌套的span_terms不支持wildcard,在这个例子中“男人的奶油”或“男人的< / em> cream “。 我有什么方法可以使用ELASTICSEARCH来实现它吗?

      更新
      我的索引:

      {
        "bluray": {
          "settings": {
            "index": {
              "uuid": "4jofvNfuQdqbhfaF2ibyhQ",
              "number_of_replicas": "1",
              "number_of_shards": "5",
              "version": {
                "created": "1000199"
              }
            }
          }
        }
      }
      

      映射:

      {
        "bluray": {
          "mappings": {
            "movies": {
              "properties": {
                "genre": {
                  "type": "string"
                }
              }
            }
          }
        }
      }
      

      我正在运行以下查询:

      POST /bluray/movies/_search
      {
        "query": {
          "bool": {
            "should": [
              {
                "span_near": {
                  "clauses": [
                    {
                      "span_term": {
                        "genre": "women"
                      }
                    },
                    {
                      "span_term": {
                        "genre": "cream"
                      }
                    }
                  ],
                  "collect_payloads": false,
                  "slop": 12,
                  "in_order": true
                }
              },
              {
                "custom_boost_factor": {
                  "query": {
                    "match_phrase": {
                      "genre": "women cream"
                    }
                  },
                  "boost_factor": 4.1
                }
              },
              {
                "match": {
                  "genre": {
                    "query": "women cream",
                    "analyzer": "standard",
                    "minimum_should_match": "99%"
                  }
                }
              }
            ]
          }
        }
      }
      

      它给了我以下结果:

      "took": 3,
         "timed_out": false,
         "_shards": {
            "total": 5,
            "successful": 5,
            "failed": 0
         },
         "hits": {
            "total": 6,
            "max_score": 0.011612939,
            "hits": [
               {
                  "_index": "bluray",
                  "_type": "movies",
                  "_id": "u9aNkZAoR86uAiW9SX8szQ",
                  "_score": 0.011612939,
                  "_source": {
                     "genre": "men's cream"
                  }
               },
               {
                  "_index": "bluray",
                  "_type": "movies",
                  "_id": "cpTyKrL6TWuJkXvliibVBQ",
                  "_score": 0.009290351,
                  "_source": {
                     "genre": "men's wrinkle cream"
                  }
               },
               {
                  "_index": "bluray",
                  "_type": "movies",
                  "_id": "rn_SFvD4QBO6TJQJNuOh5A",
                  "_score": 0.009290351,
                  "_source": {
                     "genre": "men's advanced wrinkle cream"
                  }
               },
               {
                  "_index": "bluray",
                  "_type": "movies",
                  "_id": "9a31_bRpR2WfWh_4fgsi_g",
                  "_score": 0.004618556,
                  "_source": {
                     "genre": "women's cream"
                  }
               },
               {
                  "_index": "bluray",
                  "_type": "movies",
                  "_id": "q-DoBBl2RsON_qwLRSoh9Q",
                  "_score": 0.0036948444,
                  "_source": {
                     "genre": "women's advanced wrinkle cream"
                  }
               },
               {
                  "_index": "bluray",
                  "_type": "movies",
                  "_id": "TxzCP8B_Q8epXtIcfgEw3Q",
                  "_score": 0.0036948444,
                  "_source": {
                     "genre": "women's wrinkle cream"
                  }
               }
            ]
         }
      }
      

      这根本不正确。当我搜索女性时,为什么要首先搜索男性。

      注意:搜索“男士奶油”仍然会返回更好的效果,但不会遵循搜索词序列。

1 个答案:

答案 0 :(得分:1)

POST /bluray/movies/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "span_near": {
            "clauses": [
              {
                "span_term": {
                  "genre": "women's"
                }
              },
              {
                "span_term": {
                  "genre": "cream"
                }
              }
            ],
            "collect_payloads": false,
            "slop": 12,
            "in_order": true
          }
        },{
          "match": {
            "genre": {
              "query": "women's cream",
              "analyzer": "standard",
              "minimum_should_match": "99%"
            }
          }
        }
      ]
    }
  }
}

根据您的预期提供以下输出:

    {
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 6,
    "max_score": 0.7841132,
    "hits": [
      {
        "_index": "bluray",
        "_type": "movies",
        "_id": "4",
        "_score": 0.7841132,
        "_source": {
          "genre": "women's cream"
        }
      },
      {
        "_index": "bluray",
        "_type": "movies",
        "_id": "5",
        "_score": 0.56961054,
        "_source": {
          "genre": "women's wrinkle cream"
        }
      },
      {
        "_index": "bluray",
        "_type": "movies",
        "_id": "6",
        "_score": 0.35892165,
        "_source": {
          "genre": "women's advanced wrinkle cream"
        }
      },
      {
        "_index": "bluray",
        "_type": "movies",
        "_id": "3",
        "_score": 0.2876821,
        "_source": {
          "genre": "men's advanced wrinkle cream"
        }
      },
      {
        "_index": "bluray",
        "_type": "movies",
        "_id": "1",
        "_score": 0.25811607,
        "_source": {
          "genre": "men's cream"
        }
      },
      {
        "_index": "bluray",
        "_type": "movies",
        "_id": "2",
        "_score": 0.11750762,
        "_source": {
          "genre": "men's wrinkle cream"
        }
      }
    ]
  }
}