【问题标题】:Elastic Search - example.com vs example弹性搜索 - example.com 与示例
【发布时间】:2019-01-07 19:41:48
【问题描述】:

我们正在使用弹性搜索 5.6.9 为我们的 Django 1.11 服务器上的搜索提供支持。

如果我正在索引数据 example.com 并搜索 example.com 我会得到搜索结果,但如果我正在搜索 example 我不会得到任何搜索结果。

理想情况下,我希望 exampleexample.com 都能工作并返回相同的搜索结果。

我怎样才能得到这种行为?

我认为我将不得不更改相同的分析器和标记器。

使用simple 分析器似乎是正确的做法。 前任: POST _analyze { "analyzer": "simple", "text": "example.com" } 返回 examplecom 作为单独的标记 { "tokens": [ { "token": "example", "start_offset": 0, "end_offset": 7, "type": "word", "position": 0 }, { "token": "com", "start_offset": 8, "end_offset": 11, "type": "word", "position": 1 } ] }

我认为我必须在索引数据和搜索时设置相同的分析器/标记器。

我尝试将analyzer 设置为simple,如下所述:https://www.elastic.co/guide/en/elasticsearch/reference/5.6/analyzer.html 但是,现在我仍然需要搜索 example.com 而不是 example,而且现在我在搜索结果的 highlight 中看不到任何内容。

我很困惑这是如何导致搜索结果而不是highlight

我完全不在这儿吗?

【问题讨论】:

  • 您好,有elastic.co/guide/en/elasticsearch/reference/current/… 的映射吗?可能您不使用“简单”分析器进行索引/搜索。尝试具有特定索引且没有“分析器”参数的分析器 API。 elastic.co/guide/en/elasticsearch/reference/6.5/…
  • @KarstenR。这是我的映射。 ``` { "my_index": { "mappings": { "record": { "date_detection": false, "properties": { "fields": { "properties": { "a0g8dfdrpm": { "type": "文本”,“分析器”:“简单”},“a0of86vs7k”:{“类型”:“文本”,“分析器”:“简单”},“apemix41h6”:{“类型”:“文本”,“分析器” : "simple" }, "b0j9j69qou": { "type": "text", "analyzer": "simple" } ....... }}}}}}} ```我不确定你是什么意味着通过尝试具有特定索引且没有“分析器”参数的分析器 api。

标签: elasticsearch elasticsearch-5 elasticsearch-dsl-py


【解决方案1】:

也许这个例子对你有帮助:

映射

PUT /so54071449
{
  "mappings": {
    "doc": {
      "properties": {
        "url": {
          "type": "text",
          "term_vector": "with_positions_offsets",
          "fields": {
            "simple": {
              "type": "text",
              "analyzer": "simple",
              "search_analyzer": "simple",
              "term_vector": "with_positions_offsets"
            }
          }
        }
      }
    }
  }
}

添加文档

POST /so54071449/doc
{
  "url": "example.com"
}

example搜索

GET /so54071449/_search
{
  "query": {
    "multi_match": {
      "query": "example",
      "fields": ["url", "url.simple"]
    }
  },
  "highlight": {
    "fields": {
      "url": {
        "matched_fields": [
          "url",
          "url.simple"
        ]
      }
    }
  }
}

example 的结果

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.25811607,
    "hits": [
      {
        "_index": "so54071449",
        "_type": "doc",
        "_id": "AWgoEwDT2HOwokHu0yvd",
        "_score": 0.25811607,
        "_source": {
          "url": "example.com"
        },
        "highlight": {
          "url": [
            "<em>example</em>.com"
          ]
        }
      }
    ]
  }
}

example.com搜索

GET /so54071449/_search
{
  "query": {
    "multi_match": {
      "query": "example.com",
      "fields": ["url", "url.simple"]
    }
  },
  "highlight": {
    "fields": {
      "url": {
        "matched_fields": [
          "url",
          "url.simple"
        ]
      }
    }
  }
}

example.com 的结果

{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.51623213,
    "hits": [
      {
        "_index": "so54071449",
        "_type": "doc",
        "_id": "AWgoEwDT2HOwokHu0yvd",
        "_score": 0.51623213,
        "_source": {
          "url": "example.com"
        },
        "highlight": {
          "url": [
            "<em>example.com</em>"
          ]
        }
      }
    ]
  }
}

我使用multi fields 应用了两个分析器(standard,这是默认值,在url 字段和simpleurl.simple 子字段上)和matched_fields to combine highlighting 结果来自urlurl.simple 合并到一个字段中。

【讨论】:

  • 我可以使用_allmatch 而不是multi_match 并列出所有字段。我问是因为字段列表可以超过 1000 个并且它是动态的。在有效负载中发送此列表会导致有效负载过大的错误。我可以使用_all 同时指定使用所有子字段吗?
  • 是的,你可以。要在 _all 字段上突出显示,请阅读:elastic.co/guide/en/elasticsearch/reference/current/…。请记住,_all 在 6.0.0 中已弃用。但也许这对你来说不是问题。
  • _allmatch 一起使用不会给出任何搜索结果。我想这是因为它只在url 中搜索,而不是在url.simple 中搜索。我试过这样:``` GET /so54071449/_search { "query": { "match": { "_all": "example" } }, "highlight": { "fields": { "_all": {} } } } ```
  • 是的。我错过了。可以使用通配符 "query": {"multi_match": {"query": "example","fields": ["url*"]}} 进行查询,但在您的情况下它可能不是一个选项。
猜你喜欢
  • 2021-11-23
  • 2020-03-29
  • 2022-12-08
  • 2015-10-21
  • 1970-01-01
  • 2018-02-03
  • 1970-01-01
  • 2018-10-26
  • 2023-01-19
相关资源
最近更新 更多