简体   繁体   English

Elasticsearch JSON 上的精确短语匹配

[英]Elasticsearch exact phrase match on JSON

I am working on exact phrase match from a json field using the elasticsearch.我正在使用 elasticsearch 从 json 字段进行精确的短语匹配。 I have tried mutiple syntax like multi_match , query_string & simple_query_string but they does not return results exactly as per the given phrase.我已经尝试过multi_matchquery_stringsimple_query_string等多种语法,但它们并没有完全按照给定的短语返回结果。

query_string syntax that I am using;我正在使用的query_string语法;

    "query":{
        "query_string":{
            "fields":[
                "json.*"
            ],
            "query":"\"legal advisor\"",
            "default_operator":"OR"
        }
    }
}

I also tried filter instead of query but filter is not given any result on json.我也尝试了过滤器而不是查询,但过滤器在 json 上没有给出任何结果。 The syntax I used for filter is;我用于过滤器的语法是;

  "query": {
    "bool": {
      "filter": {
        "match": {
          "json": "legal advisor"
        }
      }
    }
  }
}

Now the question is;现在的问题是;

Is it possible to perform exact match operation on json using elasticsearch?是否可以使用 elasticsearch 对 json 执行精确匹配操作?

You can try using multi-match query with type phrase您可以尝试使用类型为短语的多重匹配查询

{
  "query": {
    "multi_match": {
      "query": "legal advisor",
      "fields": [
        "json.*"
      ],
      "type": "phrase"
    }
  }
}

Since you have not provided your sample docs and expected docs, I am assuming you are looking for a phrase match , Adding a working sample.由于您没有提供示例文档和预期文档,我假设您正在寻找短语匹配,添加工作示例。

Index sample docs which will also generate the index mapping也将生成索引映射的索引示例文档

{
    "title" : "legal advisor"
}

{
    "title" : "legal expert advisor"
}

Now if you are looking for exact phrase search of legal advisor use below query现在,如果您正在寻找legal advisor的精确词组搜索,请使用以下查询

{
    "query": {
        "match_phrase": {
            "title": "legal advisor"
        }
    }
}

Which returns only first doc仅返回第一个文档

"hits": [
            {
                "_index": "64989158",
                "_type": "_doc",
                "_id": "1",
                "_score": 0.5753642,
                "_source": {
                    "title": "legal advisor"
                }
            }
        ]

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

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