【问题标题】:Elastic Search: Different results for query string when using fields弹性搜索:使用字段时查询字符串的不同结果
【发布时间】:2019-03-16 08:13:36
【问题描述】:

我们有一个弹性搜索 5.5 设置。我们使用 nest 通过 C# 执行查询。

执行以下查询时:

{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "00917751"
          }
        }
      ]
    }
  }
}

我们得到了想要的结果:一个以数字为标识符的结果。

使用以下查询时:

{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "00917751",
            "fields": [
              "searchReference",
              "searchIdentifier",
              "searchObjectNo",
              "searchBrand",
              "searchExtSerNo"
            ]
          }
        }
      ]
    }
  }
}

我们没有得到任何结果。

我们正在搜索的值在字段 searchIndentifier 中,值为“1-00917751”。

我们有一个名为“final”的自定义分析器

.Custom("final", cu => cu .Tokenizer("keyword").Filters(new List() { "lowercase" }))

字段 searchIndentifier 上没有设置自定义分析器。我们尝试在其中添加空格标记器,但没有任何区别。

当我们尝试使用查询“S328”搜索值“S328-25”时,另一个名为“searchObjectNo”的字段确实有效。这些字段完全相同。

这里有什么想法吗?

另一个问题。在第一个查询中,当我们搜索 1-00917751(不带引号)时,我们会得到很多结果。但我们认为这是因为关键字标记器?

谢谢

Schoof

索引设置和映射:

{
  "inventoryitems": {
    "aliases": {},
    "mappings": {
      "inventoryobject": {
        "properties": {
          "articleGroups": {
            "type": "nested",
            "properties": {
              "id": {
                "type": "long"
              }
            }
          },
          "articleId": {
            "type": "long"
          },
          "articleNumber": {
            "type": "text",
            "boost": 1.5,
            "analyzer": "final"
          },
          "brand": {
            "type": "text",
            "analyzer": "final"
          },
          "catalogues": {
            "type": "nested",
            "properties": {
              "articleGroupId": {
                "type": "long"
              },
              "articleGroupName": {
                "type": "text",
                "analyzer": "final",
                "fielddata": true
              },
              "id": {
                "type": "long"
              },
              "name": {
                "type": "text",
                "analyzer": "final",
                "fielddata": true
              }
            }
          },
          "details": {
            "type": "nested",
            "properties": {
              "actualState": {
                "type": "double"
              },
              "allocation": {
                "type": "text",
                "analyzer": "final",
                "fielddata": true
              },
              "available": {
                "type": "double"
              },
              "batch": {
                "type": "text",
                "analyzer": "final"
              },
              "calibrationDate": {
                "type": "date"
              },
              "expected": {
                "type": "double"
              },
              "externalSerialNumber": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "id": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "inReturn": {
                "type": "double"
              },
              "inventory": {
                "type": "double"
              },
              "isInMobileCarrier": {
                "type": "boolean"
              },
              "locationDetail": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "locationId": {
                "type": "long"
              },
              "locationName": {
                "type": "text",
                "analyzer": "final",
                "fielddata": true
              },
              "locationType": {
                "type": "text",
                "analyzer": "final",
                "fielddata": true
              },
              "lotId": {
                "type": "long"
              },
              "mobileCarrierCode": {
                "type": "text",
                "analyzer": "final",
                "fielddata": true
              },
              "mobileCarrierId": {
                "type": "long"
              },
              "ownerCode": {
                "type": "text",
                "analyzer": "final"
              },
              "requested": {
                "type": "double"
              },
              "reserved": {
                "type": "double"
              },
              "storeLocationId": {
                "type": "long"
              },
              "thicknessCode": {
                "type": "text",
                "analyzer": "final"
              },
              "weldedMark": {
                "type": "text",
                "analyzer": "final"
              }
            }
          },
          "docNo": {
            "type": "long"
          },
          "hasStock": {
            "type": "boolean"
          },
          "id": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "identifier": {
            "type": "text",
            "boost": 1.5,
            "analyzer": "final"
          },
          "inventoryItemType": {
            "properties": {
              "name": {
                "type": "text",
                "analyzer": "final",
                "fielddata": true
              }
            }
          },
          "mobileCarrierId": {
            "type": "long"
          },
          "name": {
            "type": "text",
            "boost": 1.5,
            "analyzer": "final"
          },
          "objectNumber": {
            "type": "text",
            "boost": 1.5,
            "analyzer": "final"
          },
          "quantity": {
            "type": "double"
          },
          "reference": {
            "type": "text",
            "boost": 1.5,
            "analyzer": "final"
          },
          "searchBrand": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "searchExtSerNo": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "searchIndentifier": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "searchName": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "searchObjectNo": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "searchReference": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "sortNumber": {
            "type": "long"
          },
          "stockUnit": {
            "type": "text",
            "boost": 1.5,
            "analyzer": "final"
          }
        }
      }
    },
    "settings": {
      "index": {
        "number_of_shards": "3",
        "provided_name": "inventoryitems",
        "creation_date": "1539253308319",
        "analysis": {
          "analyzer": {
            "final": {
              "filter": [
                "lowercase"
              ],
              "type": "custom",
              "tokenizer": "keyword"
            }
          }
        },
        "number_of_replicas": "1",
        "uuid": "Kb5KuYEiR5GQqgBPVYjJfA",
        "version": {
          "created": "5050299"
        }
      }
    }
  }
}

【问题讨论】:

  • 这将有助于查看您的索引设置和映射。请用您从curl -XGET localhost:9200/yourindex得到的信息更新您的问题
  • @Val:我们在 windows 环境中托管弹性搜索,我认为 curl 在 windows 上不存在?
  • Powershell ;-) 否则来自 Kibana GET you-index
  • @Val 已添加,谢谢! :)
  • @Schoof 用于 curl,您可以使用适用于 Linux 的 Windows 子系统 (WSL)。如果您想在 PowerShell 中获得类似体验,请查看 gist.github.com/russcam/7fbdbaa37e3dcb3fd3ee0aa54af3708c

标签: elasticsearch nest


【解决方案1】:

答案很简单:在您的映射中,您的字段名为 searchIndentifier,而在您的查询中,您使用的字段名为 searchIdentifier,但它并不存在 ;-)

【讨论】:

  • 嗯,这确实解释了一部分!如果我现在搜索 00917751,我会得到结果。搜索 917751 不会给出结果。有什么想法吗? :) 当不使用这些字段时,也会出现同样的问题。所以这个问题其实已经解决了。
  • 这个其他线程会给你一些想法:stackoverflow.com/questions/30666371/…(提示:使用 ngrams)
  • 我不确定我是否理解该建议。是否建议使用多个查询? (我对此提出了一个单独的问题:stackoverflow.com/questions/52765963/…
  • 不,如果您想有效地搜索部分输入(前缀、中缀、后缀使用 ngram 搜索),则需要在索引时以不同方式分析数据
  • 我添加了 nGram 分词器。但这对我的查询没有任何影响:jsonblob.com/5756a2f2-cdf4-11e8-a72c-0b145c754dd1 仍然找到相同的结果。
猜你喜欢
  • 2016-07-28
  • 1970-01-01
  • 1970-01-01
  • 2021-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-16
相关资源
最近更新 更多