简体   繁体   English

ElasticSearch的搜索结果

[英]Search results from ElasticSearch

Is it possible to tell ElasticSearch to include in results the keywords for which the "document" was found?是否可以告诉 ElasticSearch 在结果中包含找到“文档”的关键字?

For example, I'm adding these documents to ES:例如,我将这些文档添加到 ES:

{
    "text": "C# is the best language"
}
{
    "text": "Javascript is the worst language"
}

Then I query them:然后我查询他们:

{
    "query": { "match": { "text": "C# javascript" } }
}

Can ES tell me that the 1st document was found by "C#" keyword and the second - by "javascript" keyword? ES 能告诉我第一个文档是由“C#”关键字找到的,第二个是由“javascript”关键字找到的吗?

You can use highlighting to achieve this您可以使用突出显示来实现这一点

Ingest documents摄取文件

POST test_pavel/_doc
{
    "text": "C# is the best language"
}

POST test_pavel/_doc
{
    "text": "Javascript is the worst language"
}

Query询问

POST test_pavel/_search
{
  "query": {
    "match": {
      "text": "c# javascript"
    }
  },
  "highlight": {
    "fields": {
      "text": {}
    }
  }
}

Response回复

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.6931471,
    "hits" : [
      {
        "_index" : "test_pavel",
        "_type" : "_doc",
        "_id" : "RNCc_XcB46EpgstaSy19",
        "_score" : 0.6931471,
        "_source" : {
          "text" : "C# is the best language"
        },
        "highlight" : {
          "text" : [
            "<em>C</em># is the best language"
          ]
        }
      },
      {
        "_index" : "test_pavel",
        "_type" : "_doc",
        "_id" : "RdCc_XcB46EpgstaUC34",
        "_score" : 0.6931471,
        "_source" : {
          "text" : "Javascript is the worst language"
        },
        "highlight" : {
          "text" : [
            "<em>Javascript</em> is the worst language"
          ]
        }
      }
    ]
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM