mosuka / blast

Blast is a full text search and indexing server, written in Go, built on top of Bleve.
Apache License 2.0
1.07k stars 76 forks source link

Search with prefix #103

Closed nadilas closed 5 years ago

nadilas commented 5 years ago

Hi,

I was wondering if I’m doing anything wrong, but other than doing a match query I can’t seem to perform a prefix query at all. According to the bleve documentation I should be able to do that by using “prefix”: “searchterm”. However it doesn’t yield any results. Do you have an example search query for prefix?

Thanks!

mosuka commented 5 years ago

Hi @nadilas ,

See the following search request. You can search for documents that contain the term search in the text_en field.

{
  "query": {
    "query": "+text_en:search"
  },
  "size": 10,
  "from": 0,
  "fields": [
    "*"
  ],
  "sort": [
    "-_score"
  ]
}
nadilas commented 5 years ago

Hi @mosuka ,

thanks, is that a direct alternative to the http://blevesearch.com/docs/Query/#prefix ?

mosuka commented 5 years ago

Blast uses Bleve SearchRequest converted to JSON directly. For more details, please see the following: https://github.com/blevesearch/bleve/blob/75cdaca51a2ad20227868a152dfb599981b93623/search.go#L269 https://blevesearch.com/docs/Query-String-Query/

Thanks,

nadilas commented 5 years ago

Right, I found that. however, when I set "query" to a json instance of a PrefixQuery https://github.com/blevesearch/bleve/blob/9b6e89951d4e9207356a833a7d99577cfe249208/search/query/prefix.go it doesn't work.

mosuka commented 5 years ago

Sorry, I misunderstood your question. Please see the following JSON. You can search for documents that contain the terms that start with s in the title_en field.

{
  "query": {
    "prefix": "s",
    "field": "title_en"
  },
  "size": 10,
  "from": 0,
  "fields": [
    "*"
  ],
  "sort": [
    "-_score"
  ]
}

For example.

$ ./bin/blast indexer search --grpc-address=:5000 --file=./example/wiki_search_request_prefix.json
{
  "status": {
    "total": 1,
    "failed": 0,
    "successful": 1
  },
  "request": {
    "query": {
      "prefix": "searc",
      "field": "title_en"
    },
    "size": 10,
    "from": 0,
    "highlight": null,
    "fields": [
      "*"
    ],
    "facets": null,
    "explain": false,
    "sort": [
      "-_score"
    ],
    "includeLocations": false
  },
  "hits": [
    {
      "index": "/tmp/blast/indexer1/index",
      "id": "enwiki_1",
      "score": 2.2461071413554166,
      "sort": [
        "_score"
      ],
      "fields": {
        "_type": "enwiki",
        "text_en": "A search engine is an information retrieval system designed to help find information stored on a computer system. The search results are usually presented in a list and are commonly called hits. Search engines help to minimize the time required to find information and the amount of information which must be consulted, akin to other techniques for managing information overload. The most public, visible form of a search engine is a Web search engine which searches for information on the World Wide Web.",
        "timestamp": "2018-07-04T05:41:00Z",
        "title_en": "Search engine (computing)"
      }
    }
  ],
  "total_hits": 1,
  "max_score": 2.2461071413554166,
  "took": 65056,
  "facets": {}
}
nadilas commented 5 years ago

Okay, I'm trying to do that, but I want to search through all fields and do not restrict it to a specific one. Can "fields" accept a "*" or do I need to list all the fields where I want to search?

mosuka commented 5 years ago

Bleve has a special field named _all by default. If you specified "include_in_all": true to the field in your index mapping, the field value will be copy to _all field. You can specify "field": "_all" in the search request.

{
  "query": {
    "prefix": "s",
    "field": "_all"
  },
  "size": 10,
  "from": 0,
  "fields": [
    "*"
  ],
  "sort": [
    "-_score"
  ]
}