簡體   English   中英

如何在 Elasticsearch 中編寫復雜的 DSL 查詢?

[英]How to write complex DSL query in Elasticsearch?

這就是我用來在多字段映射中創建索引的方法

curl -X PUT http://localhost:9200/mapping_log
{ "mappings":{ "properties":{"data1:{"type": "text","fields":{"keyword":{"type":"keyword"}}}, {"data2":{"type": "text","fields":{"keyword":{"type":"keyword"}}}, {"data3":{"type": "text","fields":{"keyword":{"type":"keyword"}}}, } } }  

這是我在彈性搜索中的數據,我想做搜索,例如,當我搜索支持時,我應該獲取值數據和服務器,當我搜索開發人員時,我應該獲取地理和圖形,並且我想要 data3 的建議者,比如假設如果我輸入 g 那么我應該可以選擇圖形和地理,有人可以幫我解決這個問題嗎?

{
        "took": 14,
        "timed_out": false,
        "_shards": {
            "total": 1,
            "successful": 1,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": {
                "value": 4,
                "relation": "eq"
            },
            "max_score": 1,
            "hits": [
                {
                    "_index": "mapping_log",
                    "_type": "properties",
                    "_id": "1",
                    "_score": 1,
                    "_source": {
                        "data1": "support",
                        "data2": "data",
                        "data3": "datapos"
                    }
                },
                {
                    "_index": "mapping_log",
                    "_type": "properties",
                    "_id": "2",
                    "_score": 1,
                    "_source": {
                        "data1": "support",
                        "data2": "server",
                        "data3": "serverpos"
                    }
                },
                {
                    "_index": "mapping_log",
                    "_type": "properties",
                    "_id": "3",
                    "_score": 1,
                    "_source": {
                        "data1": "developer",
                        "data2": "graph",
                        "data3": "graphpos"
                    }
                },
                {
                    "_index": "mapping_log",
                    "_type": "properties",
                    "_id": "4",
                    "_score": 1,
                    "_source": {
                        "data1": "developer",
                        "data2": "geo",
                        "data3": "geopos"
                    }
                }
            ]
        }
    }

您需要將edge_ngram 標記器與源過濾一起使用

添加帶有索引數據(與問題相同)、索引映射、搜索查詢和搜索結果的工作示例

索引映射:

{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "my_tokenizer"
        }
      },
      "tokenizer": {
        "my_tokenizer": {
          "type": "edge_ngram",
          "min_gram": 1,
          "max_gram": 10,
          "token_chars": [
            "letter",
            "digit"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "data1": {
        "type": "text"
      },
      "data2": {
        "type": "text"
      },
      "data3": {
        "type": "text",
        "analyzer":"my_analyzer"
      }
    }
  }
}

搜索查詢:

當我尋求支持時,我應該得到值數據和服務器

{
  "_source": [
    "data2"
  ],
  "query": {
    "multi_match": {
      "query": "support",
      "fields": [
        "data1",
        "data2"
      ]
    }
  }
}

搜索結果:

"hits": [
      {
        "_index": "67260491",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.4094054,
        "_source": {
          "data2": "server"
        }
      },
      {
        "_index": "67260491",
        "_type": "_doc",
        "_id": "4",
        "_score": 0.44183272,
        "_source": {
          "data2": "data"
        }
      }
    ]

搜索查詢:

{
  "_source": [
    "data2"
  ],
  "query": {
    "multi_match": {
      "query": "developer",
      "fields": [
        "data1",
        "data2"
      ]
    }
  }
}

搜索結果:

 "hits": [
      {
        "_index": "67260491",
        "_type": "_doc",
        "_id": "2",
        "_score": 1.0296195,
        "_source": {
          "data2": "graph"
        }
      },
      {
        "_index": "67260491",
        "_type": "_doc",
        "_id": "3",
        "_score": 1.0296195,
        "_source": {
          "data2": "geo"
        }
      }
    ]

搜索查詢:

我想要 data3 的建議者,比如假設如果我輸入 g 那么我應該選擇圖形和地理

{
  "_source": [
    "data3"
  ],
  "query": {
    "match": {
      "data3": "g"
    }
  }
}

搜索結果:

"hits": [
      {
        "_index": "67260491",
        "_type": "_doc",
        "_id": "3",
        "_score": 1.1123568,
        "_source": {
          "data3": "geopos"
        }
      },
      {
        "_index": "67260491",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.99270093,
        "_source": {
          "data3": "graphpos"
        }
      }
    ]

更新1:

您可以將索引映射更新為

PUT /<index-name>/_mapping

{
  "properties": {
    "data2": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword"
        }
      }
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM