0

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

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

理想情况下,我希望example两者example.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 } ] }

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

我尝试按照此处所述设置为:analyzerhttps ://www.elastic.co/guide/en/elasticsearch/reference/5.6/analyzer.html 但是,现在我仍然需要搜索而不是,而且现在我不需要在搜索结果中查看任何内容。simpleexample.comexamplehighlight

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

我完全不在这儿吗?

4

1 回答 1

0

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

映射

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>"
          ]
        }
      }
    ]
  }
}

我使用了多个字段来应用两个分析器(standard默认情况下,在url字段和子字段simple上),并将突出显示结果从一个字段合并到一个字段中。url.simplematched_fieldsurlurl.simple

于 2019-01-07T11:41:48.183 回答